reworking errors
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
import {
|
||||
InferSchemaType,
|
||||
Schema,
|
||||
SchemaValidationError,
|
||||
ValidationErrorDetail,
|
||||
StringSchema,
|
||||
z,
|
||||
} from "@shared/utils/validator.ts";
|
||||
|
||||
@ -12,71 +12,67 @@ export class ErrorBase extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
export class QueryExecutionError extends ErrorBase {
|
||||
public readonly type = "QueryExecutionError";
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
export class NoAdminEntryError extends ErrorBase {
|
||||
public readonly type = "NoAdminEntry";
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
export class FailedToReadFileError extends ErrorBase {
|
||||
public readonly type = "FailedToReadFileError";
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
export class InvalidSyntaxError extends ErrorBase {
|
||||
public readonly type = "InvalidSyntax";
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
export class InvalidPathError extends ErrorBase {
|
||||
public readonly type = "InvalidPath";
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
export class AdminPasswordNotSetError extends ErrorBase {
|
||||
public readonly type = "AdminPasswordNotSetError";
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
export class RequestValidationError extends SchemaValidationError {
|
||||
public readonly type = "RequestValidationError";
|
||||
constructor(
|
||||
input: unknown,
|
||||
detail: ValidationErrorDetail,
|
||||
export function createErrorSchema<
|
||||
T extends string,
|
||||
I extends Schema<any> = StringSchema,
|
||||
>(
|
||||
type: T,
|
||||
info?: I,
|
||||
) {
|
||||
super(input, detail);
|
||||
}
|
||||
return z.obj({
|
||||
type: z.literal(type),
|
||||
info: info ?? z.string(),
|
||||
});
|
||||
}
|
||||
|
||||
export class ResponseValidationError extends SchemaValidationError {
|
||||
public readonly type = "ResponseValidationError";
|
||||
constructor(
|
||||
input: unknown,
|
||||
detail: ValidationErrorDetail,
|
||||
) {
|
||||
super(input, detail);
|
||||
}
|
||||
}
|
||||
const queryExecutionErrorSchema = createErrorSchema("QueryExecutionError");
|
||||
type QueryExecutionError = InferSchemaType<typeof queryExecutionErrorSchema>;
|
||||
|
||||
export class FailedToParseRequestAsJSON extends ErrorBase {
|
||||
public readonly type = "FailedToParseRequestAsJSON";
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
const noAdminEntryErrorSchema = createErrorSchema("noAdminEntryError");
|
||||
type NoAdminEntryError = InferSchemaType<typeof noAdminEntryErrorSchema>;
|
||||
|
||||
const failedToReadFileErrorSchema = createErrorSchema("failedToReadFileError");
|
||||
type failedToReadFileErrorSchema = InferSchemaType<
|
||||
typeof failedToReadFileErrorSchema
|
||||
>;
|
||||
|
||||
const failedToReadFileErrorSchemaSchema = createErrorSchema(
|
||||
"FailedToReadFileErrorSchema",
|
||||
);
|
||||
type FailedToReadFileErrorSchema = InferSchemaType<
|
||||
typeof failedToReadFileErrorSchemaSchema
|
||||
>;
|
||||
|
||||
const invalidSyntaxErrorSchema = createErrorSchema("InvalidSyntaxError");
|
||||
type InvalidSyntaxError = InferSchemaType<typeof invalidSyntaxErrorSchema>;
|
||||
|
||||
const invalidPathErrorSchema = createErrorSchema("InvalidPathError");
|
||||
type InvalidPathError = InferSchemaType<typeof invalidPathErrorSchema>;
|
||||
|
||||
const adminPasswordNotSetErrorSchema = createErrorSchema(
|
||||
"AdminPasswordNotSetError",
|
||||
);
|
||||
type AdminPasswordNotSetError = InferSchemaType<
|
||||
typeof adminPasswordNotSetErrorSchema
|
||||
>;
|
||||
|
||||
const requestValidationErrorSchema = createErrorSchema(
|
||||
"RequestValidationError",
|
||||
);
|
||||
type RequestValidationError = InferSchemaType<
|
||||
typeof requestValidationErrorSchema
|
||||
>;
|
||||
|
||||
const responseValidationErrorSchema = createErrorSchema(
|
||||
"ResponseValidationError",
|
||||
);
|
||||
type ResponseValidationError = InferSchemaType<
|
||||
typeof responseValidationErrorSchema
|
||||
>;
|
||||
|
||||
const failedToParseRequestAsJSONErrorSchema = createErrorSchema(
|
||||
"FailedToParseRequestAsJSONError",
|
||||
);
|
||||
type FailedToParseRequestAsJSONError = InferSchemaType<
|
||||
typeof failedToParseRequestAsJSONErrorSchema
|
||||
>;
|
||||
|
||||
@ -1,19 +0,0 @@
|
||||
class ParseError extends Error {
|
||||
type = "ParseError";
|
||||
|
||||
public trace: NestedArray<string> = [];
|
||||
|
||||
constructor(
|
||||
public input: any,
|
||||
trace: NestedArray<string> | string,
|
||||
public readonly msg: string,
|
||||
) {
|
||||
super(msg);
|
||||
}
|
||||
}
|
||||
|
||||
type NestedArray<T> = T | NestedArray<T>[];
|
||||
|
||||
export interface Schema<T> {
|
||||
parse(input: unknown): Result<T, ParseError>;
|
||||
}
|
||||
@ -220,7 +220,7 @@ export abstract class BaseSchema<T> implements Schema<T> {
|
||||
}
|
||||
}
|
||||
|
||||
class StringSchema extends BaseSchema<string> {
|
||||
export class StringSchema extends BaseSchema<string> {
|
||||
private static readonly emailRegex =
|
||||
/^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i; // https://stackoverflow.com/questions/46155/how-can-i-validate-an-email-address-in-javascript
|
||||
|
||||
|
||||
1
test1.ts
1
test1.ts
@ -132,7 +132,6 @@ export abstract class PrimitiveSchema<T> extends BaseSchema<T> {
|
||||
}
|
||||
}
|
||||
|
||||
// Example: StringSchema with Improved Error Handling
|
||||
export class StringSchema extends PrimitiveSchema<string> {
|
||||
private static readonly emailRegex =
|
||||
/^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
|
||||
|
||||
Reference in New Issue
Block a user