|
1 | 1 | export enum ErrorCodes { |
2 | 2 | MONARCH_ERROR = "MONARCH_ERROR", |
3 | | - PARSE_ERROR = "PARSE_ERROR", |
4 | 3 | VALIDATION_ERROR = "VALIDATION_ERROR", |
5 | 4 | } |
6 | 5 |
|
7 | 6 | export class MonarchError extends Error { |
8 | | - public code: (typeof ErrorCodes)[keyof typeof ErrorCodes]; |
9 | | - public originalError?: Error; |
10 | | - |
11 | 7 | constructor( |
12 | 8 | message: string, |
13 | | - code: (typeof ErrorCodes)[keyof typeof ErrorCodes] = ErrorCodes.MONARCH_ERROR, |
14 | | - originalError?: Error, |
| 9 | + public code: ErrorCodes = ErrorCodes.MONARCH_ERROR, |
| 10 | + public cause?: Error, |
15 | 11 | ) { |
16 | 12 | super(message); |
17 | 13 | this.name = this.constructor.name; |
18 | 14 | this.code = code; |
19 | | - this.originalError = originalError; |
| 15 | + this.cause = cause; |
20 | 16 |
|
21 | | - if (!!originalError && originalError.stack) { |
22 | | - this.stack = `${this.stack}\nCaused by: ${originalError.stack}`; |
| 17 | + if (!!cause && cause.stack) { |
| 18 | + this.stack = `${this.stack}\nCaused by: ${cause.stack}`; |
23 | 19 | } else if (Error.captureStackTrace) { |
24 | 20 | Error.captureStackTrace(this, this.constructor); |
25 | 21 | } |
26 | 22 | } |
27 | 23 | } |
28 | 24 |
|
29 | | -export class MonarchParseError extends MonarchError { |
30 | | - public fieldPath?: (string | number)[]; |
| 25 | +export class FieldError extends Error { |
31 | 26 | constructor( |
32 | 27 | message: string, |
33 | | - fieldPath?: (string | number)[], |
34 | | - originalError?: Error, |
| 28 | + public fieldPath?: (string | number)[], |
35 | 29 | ) { |
36 | | - super(message, ErrorCodes.PARSE_ERROR, originalError); |
37 | | - this.fieldPath = normalizeFieldPath(fieldPath); |
| 30 | + super(message); |
| 31 | + this.fieldPath = fieldPath ?? []; |
38 | 32 | } |
39 | 33 | } |
40 | 34 |
|
41 | 35 | export class MonarchValidationError extends MonarchError { |
42 | 36 | constructor( |
43 | 37 | message: string, |
44 | 38 | public fieldPath: string, |
45 | | - originalError?: Error, |
| 39 | + cause?: Error, |
46 | 40 | ) { |
47 | 41 | super( |
48 | | - formatValidationMessage(fieldPath, message), |
| 42 | + `Validation error: '${fieldPath}' ${message}`, |
49 | 43 | ErrorCodes.VALIDATION_ERROR, |
50 | | - originalError, |
| 44 | + cause, |
51 | 45 | ); |
52 | 46 | } |
53 | 47 | } |
54 | 48 |
|
55 | | -export function normalizeFieldPath( |
56 | | - fieldPath?: string | (string | number)[], |
57 | | -): (string | number)[] { |
58 | | - if (typeof fieldPath === "string") { |
59 | | - return fieldPath.split("."); |
60 | | - } |
61 | | - if (Array.isArray(fieldPath)) { |
62 | | - return fieldPath; |
63 | | - } |
64 | | - return []; |
65 | | -} |
66 | | - |
67 | | -export function formatErrorPath( |
68 | | - path: string | number | (string | number)[], |
69 | | -): string { |
70 | | - return Array.isArray(path) ? path.join(".") : String(path); |
71 | | -} |
72 | | - |
73 | 49 | export function formatValidationPath(pathSegment: { |
74 | 50 | schema: string; |
75 | 51 | field: string; |
76 | 52 | path?: (string | number)[]; |
77 | 53 | }): string { |
78 | 54 | const { schema, field, path = [] } = pathSegment; |
79 | | - return formatErrorPath([schema, field, ...path]); |
80 | | -} |
81 | | - |
82 | | -export function formatValidationMessage( |
83 | | - fieldPath: string, |
84 | | - message: string, |
85 | | -): string { |
86 | | - return `Validation error: '${fieldPath}' ${message}`; |
| 55 | + return [schema, field, ...path].join("."); |
87 | 56 | } |
0 commit comments