Repository navigation
Commit 2d3fe87
authored
fix(server): convert decode failures to BAD_REQUEST before interceptors see them (#2517)
## Problem
`RethrowHandlerPlugin` runs as the outermost handler interceptor.
`StandardHandler` ran `decodeInput` inside that chain, but it turned
non-`ORPCError` decode failures into `ORPCError('BAD_REQUEST')` only in
its catch block, after the chain had already thrown. So the rethrow
`filter` got the raw `SyntaxError` or `TypeError` from parsing the body
or query.
With the filter the docs recommend, `!(error instanceof ORPCError)`,
every malformed request was rethrown, and Express or Nest answered
**500** instead of 400. Any anonymous client could trigger it:
- `POST /ping` with `content-type: application/json` and body `{bad`
- An RPC GET with `?data=%7Bbad`
- A corrupt gzip body sent through `RequestCompressionHandlerPlugin` (a
`TypeError`)
## Fix
The BAD_REQUEST conversion now wraps the `decodeInput` call inside the
innermost interceptor function. Every interceptor, the rethrow filter
included, now sees the same error the client gets, with the original
parse error kept as `cause`. Procedure errors are untouched and are
still rethrown as documented.
This is a change in the shared handler, not the plugin, so it covers
every codec (RPC, OpenAPI, Nest, MCP). It also covers `decodeInput`
replacements that interceptors pass through `next()`.
## Behavior changes for reviewers
- An interceptor now sees `ORPCError('BAD_REQUEST')` for a malformed
request instead of the raw `SyntaxError`/`TypeError`. As a result,
`PinoHandlerPlugin` logs these at `warn` instead of `error`.
- If an interceptor catches a decode error and throws a different
non-ORPCError, the client now gets 500 instead of 400. That case is a
bug in the interceptor.
- An interceptor that calls `options.decodeInput()` itself, before
`next()`, still gets the raw error. No interceptor in the repo does
this.
## Tests
- `rethrow.test.ts` (RPC): malformed body and query both return 400. The
filter receives a `BAD_REQUEST` ORPCError. A procedure `TypeError` is
still rethrown.
- `openapi-handler.test.ts`: a malformed body returns 400 under
`RethrowHandlerPlugin`.
- `handler.test.ts`: interceptors receive the same `BAD_REQUEST` error
that gets encoded.
All of these fail on `main`. `pnpm check` passes. In `pnpm exec vitest
run`, the only failure is the openapi-inference tsconfig test that also
fails on `main`.1 parent 77da9d6 commit 2d3fe87
4 files changed
Lines changed: 82 additions & 12 deletions
File tree
- packages
- openapi/src/adapters/fetch
- server/src
- adapters/standard
- plugins
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
114 | 115 | | |
115 | 116 | | |
116 | 117 | | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
380 | 380 | | |
381 | 381 | | |
382 | 382 | | |
383 | | - | |
| 383 | + | |
384 | 384 | | |
| 385 | + | |
| 386 | + | |
385 | 387 | | |
386 | 388 | | |
387 | 389 | | |
| |||
399 | 401 | | |
400 | 402 | | |
401 | 403 | | |
| 404 | + | |
402 | 405 | | |
403 | 406 | | |
404 | 407 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
138 | 138 | | |
139 | 139 | | |
140 | 140 | | |
141 | | - | |
| 141 | + | |
142 | 142 | | |
143 | 143 | | |
144 | 144 | | |
| |||
172 | 172 | | |
173 | 173 | | |
174 | 174 | | |
175 | | - | |
176 | | - | |
177 | 175 | | |
178 | 176 | | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
179 | 190 | | |
180 | 191 | | |
181 | 192 | | |
| |||
220 | 231 | | |
221 | 232 | | |
222 | 233 | | |
223 | | - | |
224 | | - | |
225 | | - | |
226 | | - | |
227 | | - | |
228 | | - | |
229 | | - | |
| 234 | + | |
230 | 235 | | |
231 | 236 | | |
232 | 237 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
102 | 102 | | |
103 | 103 | | |
104 | 104 | | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
0 commit comments