-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patherrors.ts
More file actions
45 lines (39 loc) · 1.16 KB
/
errors.ts
File metadata and controls
45 lines (39 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { type InferIssue } from "valibot";
type ValidationError = {
expected: string;
received: string;
message: string;
path: string | undefined;
};
/**
* 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
*/
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;
};
export const CodexValibotIssuesMap = (issues: InferIssue<any>[]) =>
issues.map((i) => ({
expected: i.expected,
received: i.received,
message: i.message,
path: i.path?.map((item: { key: string }) => item.key).join("."),
}));