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