-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwithContractErrorHandler.ts
More file actions
60 lines (56 loc) · 2.02 KB
/
withContractErrorHandler.ts
File metadata and controls
60 lines (56 loc) · 2.02 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
60
import type { IRequest } from 'itty-router';
/**
* Contract-aware error handler middleware
*
* Handles validation errors from schema validation and falls back to itty-router's
* default error handler for other errors. This middleware should be used as the
* `catch` handler in the router configuration.
*
* @typeParam RequestType - The request type (extends IRequest)
* @typeParam Args - Additional arguments passed to handlers
*
* @returns An error handler function that handles validation errors and other errors
*
* @example
* ```typescript
* const router = Router({
* catch: withContractErrorHandler(),
* });
* ```
*/
export function withContractErrorHandler<
RequestType extends IRequest = IRequest,
Args extends any[] = any[],
>(): (err: unknown, request: RequestType, ...args: Args) => Response {
return (err: unknown, _request: RequestType, ..._args: Args): Response => {
// Handle validation errors with issues array
if (err instanceof Error && 'issues' in err) {
const issues = (err as Error & { issues: unknown }).issues;
return new Response(
JSON.stringify({
error: 'Validation failed',
details: Array.isArray(issues) ? issues : [issues],
}),
{ status: 400, headers: { 'content-type': 'application/json' } }
);
}
// Handle other errors - ensure all errors conform to { error: string, details: [...] }
const errorMessage = err instanceof Error ? err.message : 'Internal server error';
const statusCode =
err && typeof err === 'object' && 'status' in err ? (err as any).status : 500;
// Format error message as a details array for consistency with validation errors
// Details array contains objects with message property (and optionally other fields)
const details = [
{
message: errorMessage,
},
];
return new Response(
JSON.stringify({
error: errorMessage,
details,
}),
{ status: statusCode, headers: { 'content-type': 'application/json' } }
);
};
}