Skip to content

Commit 05db93f

Browse files
Merge pull request #1053 from BitGo/DX-1641-update-readme
docs: update README.md with information regarding new error formatters
2 parents 872e93c + d4902a7 commit 05db93f

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

packages/typed-express-router/README.md

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,19 @@ logging.
7676

7777
```ts
7878
const typedRouter = createRouter(MyApi, {
79-
onDecodeError: (errs, req, res) => {
79+
decodeErrorFormatter: (errs, req) => {
8080
// Format `errs` however you want
81-
res.send(400).json({ message: 'Bad request' }).end();
81+
return { message: 'Bad request' };
8282
},
83-
onEncodeError: (err, req, res) => {
83+
getDecodeErrorStatusCode: (errs, req) => {
84+
return 400;
85+
},
86+
encodeErrorFormatter: (err, req) => {
87+
return { message: 'Internal server error' };
88+
},
89+
getEncodeErrorStatusCode: (err, req) => {
8490
// Ideally won't happen unless type safety is violated, so it's a 500
85-
res.send(500).json({ message: 'Internal server error' }).end();
91+
return 500;
8692
},
8793
afterEncodedResponseSent: (status, payload, req, res) => {
8894
// Perform side effects or other things, `res` should be ended by this point
@@ -92,17 +98,18 @@ const typedRouter = createRouter(MyApi, {
9298

9399
// Override the decode error handler on one route
94100
typedRouter.get('hello.world', [HelloWorldHandler], {
95-
onDecodeError: customHelloDecodeErrorHandler,
101+
decodeErrorFormatter: customHelloDecodeErrorFormatter,
96102
});
97103
```
98104

99105
### Unchecked routes
100106

101107
If you need custom behavior on decode errors that is more involved than just sending an
102108
error response, then the unchecked variant of the router functions can be used. They do
103-
not fail and call `onDecodeError` when a request is invalid. Instead, they will still
104-
populate `req.decoded`, except this time it'll contain the
105-
`Either<Errors, DecodedRequest>` type for route handlers to inspect.
109+
not fail and send a http response using `decodeErrorFormatter` and
110+
`getDecodeErrorStatusCode` when a request is invalid. Instead, they will still populate
111+
`req.decoded`, except this time it'll contain the `Either<Errors, DecodedRequest>` type
112+
for route handlers to inspect.
106113

107114
```ts
108115
// Just a normal express route

0 commit comments

Comments
 (0)