Skip to content

Commit 656d06e

Browse files
committed
docs: add example of handleErrors
1 parent c106dcf commit 656d06e

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ The `defineRoute` function is used to define route handlers in a type-safe and s
5454
| hasFormData | `boolean` | Is the request body a [FormData](https://developer.mozilla.org/en-US/docs/Web/API/FormData) |
5555
| action | (source: [ActionSource](#action-source)) => Promise<[Response](https://developer.mozilla.org/en-US/docs/Web/API/Response)> | Function handling the request, receiving pathParams, queryParams, and requestBody. |
5656
| responses | Record<`number`, [ResponseDefinition](#response-definition)> | Object defining possible responses, each with a description and optional content schema. |
57+
| handleErrors | (errorType: string, issues?: [ZodIssues](https://zod.dev/ERROR_HANDLING?id=zodissue)[]) => Response | `(Optional)` Custom error handler can be provided to replace the default behavior. [See below](#example) |
5758

5859
### Action Source
5960

@@ -98,6 +99,21 @@ export const { GET } = defineRoute({
9899
200: { description: "User details retrieved successfully", content: UserDTO },
99100
404: { description: "User not found" },
100101
},
102+
// optional 👇👇👇
103+
handleErrors: (errorType, issues) => {
104+
console.log(issues);
105+
switch (errorType) {
106+
"PARSE_FORM_DATA":
107+
"PARSE_REQUEST_BODY":
108+
"PARSE_SEARCH_PARAMS":
109+
return new Response(null, { status: 400 });
110+
"PARSE_PATH_PARAMS":
111+
return new Response(null, { status: 404 });
112+
"UNNECESSARY_PATH_PARAMS":
113+
"UNKNOWN_ERROR":
114+
return new Response(null, { status: 500 });
115+
}
116+
},
101117
});
102118
```
103119

0 commit comments

Comments
 (0)