-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.ts
More file actions
59 lines (53 loc) · 1.83 KB
/
errors.ts
File metadata and controls
59 lines (53 loc) · 1.83 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
export interface StructuredError {
code: string;
where: string;
expected: string;
actual: string;
fixHint: string;
}
export class RuntimeError extends Error {
readonly structured: StructuredError;
constructor(structured: StructuredError, options?: { cause?: unknown }) {
super(formatStructuredError(structured), options as ErrorOptions | undefined);
this.name = 'RuntimeError';
this.structured = structured;
}
}
export function isStructuredError(value: unknown): value is { structured: StructuredError } {
if (typeof value !== 'object' || value === null) return false;
const s = (value as { structured?: unknown }).structured;
if (typeof s !== 'object' || s === null) return false;
const r = s as Record<string, unknown>;
return (
typeof r.code === 'string' &&
typeof r.where === 'string' &&
typeof r.expected === 'string' &&
typeof r.actual === 'string' &&
typeof r.fixHint === 'string'
);
}
export function getStructured(value: unknown): StructuredError | null {
return isStructuredError(value) ? value.structured : null;
}
export function formatStructuredError(s: StructuredError): string {
return `[${s.code}] ${s.where}: expected ${s.expected}, got ${s.actual} — ${s.fixHint}`;
}
export const ErrorCodes = {
validation: {
missingField: 'validation.missing_field',
invalidField: 'validation.invalid_field',
duplicateId: 'validation.duplicate_id',
},
action: {
timeout: 'action.timeout',
memoryExceeded: 'action.memory_exceeded',
handlerThrew: 'action.handler_threw',
},
plugin: {
swapRejected: 'plugin.swap_rejected',
},
} as const;
export type ErrorCode =
| (typeof ErrorCodes.validation)[keyof typeof ErrorCodes.validation]
| (typeof ErrorCodes.action)[keyof typeof ErrorCodes.action]
| (typeof ErrorCodes.plugin)[keyof typeof ErrorCodes.plugin];