diff --git a/server/src/lib/errors.ts b/server/src/lib/errors.ts index 233ef9d..bf51d8f 100644 --- a/server/src/lib/errors.ts +++ b/server/src/lib/errors.ts @@ -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 function createErrorSchema< + T extends string, + I extends Schema = StringSchema, +>( + type: T, + info?: I, +) { + return z.obj({ + type: z.literal(type), + info: info ?? z.string(), + }); } -export class NoAdminEntryError extends ErrorBase { - public readonly type = "NoAdminEntry"; - constructor(message: string) { - super(message); - } -} +const queryExecutionErrorSchema = createErrorSchema("QueryExecutionError"); +type QueryExecutionError = InferSchemaType; -export class FailedToReadFileError extends ErrorBase { - public readonly type = "FailedToReadFileError"; - constructor(message: string) { - super(message); - } -} +const noAdminEntryErrorSchema = createErrorSchema("noAdminEntryError"); +type NoAdminEntryError = InferSchemaType; -export class InvalidSyntaxError extends ErrorBase { - public readonly type = "InvalidSyntax"; - constructor(message: string) { - super(message); - } -} +const failedToReadFileErrorSchema = createErrorSchema("failedToReadFileError"); +type failedToReadFileErrorSchema = InferSchemaType< + typeof failedToReadFileErrorSchema +>; -export class InvalidPathError extends ErrorBase { - public readonly type = "InvalidPath"; - constructor(message: string) { - super(message); - } -} +const failedToReadFileErrorSchemaSchema = createErrorSchema( + "FailedToReadFileErrorSchema", +); +type FailedToReadFileErrorSchema = InferSchemaType< + typeof failedToReadFileErrorSchemaSchema +>; -export class AdminPasswordNotSetError extends ErrorBase { - public readonly type = "AdminPasswordNotSetError"; - constructor(message: string) { - super(message); - } -} +const invalidSyntaxErrorSchema = createErrorSchema("InvalidSyntaxError"); +type InvalidSyntaxError = InferSchemaType; -export class RequestValidationError extends SchemaValidationError { - public readonly type = "RequestValidationError"; - constructor( - input: unknown, - detail: ValidationErrorDetail, - ) { - super(input, detail); - } -} +const invalidPathErrorSchema = createErrorSchema("InvalidPathError"); +type InvalidPathError = InferSchemaType; -export class ResponseValidationError extends SchemaValidationError { - public readonly type = "ResponseValidationError"; - constructor( - input: unknown, - detail: ValidationErrorDetail, - ) { - super(input, detail); - } -} +const adminPasswordNotSetErrorSchema = createErrorSchema( + "AdminPasswordNotSetError", +); +type AdminPasswordNotSetError = InferSchemaType< + typeof adminPasswordNotSetErrorSchema +>; -export class FailedToParseRequestAsJSON extends ErrorBase { - public readonly type = "FailedToParseRequestAsJSON"; - constructor(message: string) { - super(message); - } -} +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 +>; diff --git a/server/src/lib/test1.ts b/server/src/lib/test1.ts deleted file mode 100644 index 4b76884..0000000 --- a/server/src/lib/test1.ts +++ /dev/null @@ -1,19 +0,0 @@ -class ParseError extends Error { - type = "ParseError"; - - public trace: NestedArray = []; - - constructor( - public input: any, - trace: NestedArray | string, - public readonly msg: string, - ) { - super(msg); - } -} - -type NestedArray = T | NestedArray[]; - -export interface Schema { - parse(input: unknown): Result; -} diff --git a/shared/utils/validator.ts b/shared/utils/validator.ts index 1f914f4..81ae29c 100644 --- a/shared/utils/validator.ts +++ b/shared/utils/validator.ts @@ -220,7 +220,7 @@ export abstract class BaseSchema implements Schema { } } -class StringSchema extends BaseSchema { +export class StringSchema extends BaseSchema { private static readonly emailRegex = /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i; // https://stackoverflow.com/questions/46155/how-can-i-validate-an-email-address-in-javascript diff --git a/test1.ts b/test1.ts index dae100e..d8fa3eb 100644 --- a/test1.ts +++ b/test1.ts @@ -132,7 +132,6 @@ export abstract class PrimitiveSchema extends BaseSchema { } } -// Example: StringSchema with Improved Error Handling export class StringSchema extends PrimitiveSchema { private static readonly emailRegex = /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;