@@ -76,13 +76,19 @@ logging.
76
76
77
77
``` ts
78
78
const typedRouter = createRouter (MyApi , {
79
- onDecodeError : (errs , req , res ) => {
79
+ decodeErrorFormatter : (errs , req ) => {
80
80
// Format `errs` however you want
81
- res . send ( 400 ). json ( { message: ' Bad request' }). end () ;
81
+ return { message: ' Bad request' };
82
82
},
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 ) => {
84
90
// 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 ;
86
92
},
87
93
afterEncodedResponseSent : (status , payload , req , res ) => {
88
94
// Perform side effects or other things, `res` should be ended by this point
@@ -92,17 +98,18 @@ const typedRouter = createRouter(MyApi, {
92
98
93
99
// Override the decode error handler on one route
94
100
typedRouter .get (' hello.world' , [HelloWorldHandler ], {
95
- onDecodeError: customHelloDecodeErrorHandler ,
101
+ decodeErrorFormatter: customHelloDecodeErrorFormatter ,
96
102
});
97
103
```
98
104
99
105
### Unchecked routes
100
106
101
107
If you need custom behavior on decode errors that is more involved than just sending an
102
108
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.
106
113
107
114
``` ts
108
115
// Just a normal express route
0 commit comments