46 lines
1.2 KiB
TypeScript
Raw Normal View History

2024-08-15 12:08:45 +02:00
import { type InferIssue } from "valibot";
2024-08-15 12:08:15 +02:00
type ValidationError = {
2024-08-15 12:08:45 +02:00
expected: string;
received: string;
message: string;
2024-09-13 19:45:59 +02:00
path: string | undefined;
2024-08-15 12:08:45 +02:00
};
2024-08-15 12:08:15 +02:00
/**
2024-08-15 12:08:48 +02:00
* The CodexError contains a message and 3 optionals properties:
* `code`: The (http) code error when it comes from a request
* `errors`: A {ValidationError} array when it comes from an object validation process
* `stack`: The error stack when the CodexError results from a error thrown
2024-08-15 12:08:15 +02:00
*/
2024-09-27 17:35:50 +02:00
export class CodexError extends Error {
code: number | null;
errors: ValidationError[] | null;
sourceStack: string | null;
constructor(
message: string,
{ code, errors, sourceStack }: CodexErrorProps = {}
) {
super(message);
this.code = code || null;
this.errors = errors || null;
this.sourceStack = sourceStack || null;
}
}
type CodexErrorProps = {
code?: number | null;
errors?: ValidationError[] | null;
sourceStack?: string | null;
2024-08-15 12:08:48 +02:00
};
2024-08-15 12:08:15 +02:00
2024-08-15 12:08:45 +02:00
export const CodexValibotIssuesMap = (issues: InferIssue<any>[]) =>
issues.map((i) => ({
expected: i.expected,
received: i.received,
message: i.message,
2024-08-15 12:08:51 +02:00
path: i.path?.map((item: { key: string }) => item.key).join("."),
2024-08-15 12:08:45 +02:00
}));