Registry Mirror
Skip to content

File tree

‎packages/server/src/adapters/standard/handler.test.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ describe('standardHandler', () => {
214214
})
215215
})
216216

217-
it('on decode error', async () => {
217+
it('on decode error, converts to BAD_REQUEST before interceptors see it', async () => {
218218
matcher.match.mockResolvedValue({
219219
path: ['ping'],
220220
procedure: ping,
@@ -254,6 +254,9 @@ describe('standardHandler', () => {
254254
return true
255255
})
256256

257+
// The interceptor must observe the same BAD_REQUEST error the client gets, not the raw decode failure.
258+
await expect(interceptor.mock.results[0]!.value).rejects.toBe(codec.encodeError.mock.calls[0]![0])
259+
257260
expect(interceptor).toHaveBeenCalledOnce()
258261
expect(interceptor).toHaveBeenCalledWith({
259262
request,

‎packages/server/src/adapters/standard/handler.ts‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export class StandardHandler<T extends Context> {
8383
return runWithSpan(
8484
{ name: `${request.method} ${request.url.pathname}` },
8585
async (span) => {
86-
let step: 'decode_input' | 'call_procedure' | undefined
86+
let step: 'call_procedure' | undefined
8787

8888
try {
8989
return await intercept(
@@ -113,12 +113,22 @@ export class StandardHandler<T extends Context> {
113113
span?.setAttribute('rpc.system', ORPC_NAME)
114114
span?.setAttribute('rpc.method', match.path.join('.'))
115115

116-
step = 'decode_input'
117-
let input = await runWithSpan(
118-
{ name: 'decode_input' },
119-
() => this.codec.decode(request, match.params, match.procedure),
120-
)
121-
step = undefined
116+
let input: unknown
117+
try {
118+
input = await runWithSpan(
119+
{ name: 'decode_input' },
120+
() => this.codec.decode(request, match.params, match.procedure),
121+
)
122+
}
123+
catch (e) {
124+
// Convert here so interceptors (e.g. the rethrow filter) see the same error the client gets.
125+
throw e instanceof ORPCError
126+
? e
127+
: new ORPCError('BAD_REQUEST', {
128+
message: `Malformed request. Ensure the request body is properly formatted and the 'Content-Type' header is set correctly.`,
129+
cause: e,
130+
})
131+
}
122132

123133
if (isAsyncIteratorObject(input)) {
124134
input = asyncIteratorWithSpan(
@@ -161,12 +171,7 @@ export class StandardHandler<T extends Context> {
161171
setSpanError(span, e)
162172
}
163173

164-
const error = step === 'decode_input' && !(e instanceof ORPCError)
165-
? new ORPCError('BAD_REQUEST', {
166-
message: `Malformed request. Ensure the request body is properly formatted and the 'Content-Type' header is set correctly.`,
167-
cause: e,
168-
})
169-
: toORPCError(e)
174+
const error = toORPCError(e)
170175

171176
const response = this.codec.encodeError(error)
172177

‎packages/server/src/plugins/rethrow.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,41 @@ describe('rethrowHandlerPlugin', () => {
193193
})
194194
})
195195
})
196+
197+
describe('rethrowHandlerPlugin with malformed requests', () => {
198+
const procedureHandler = vi.fn()
199+
const filter = vi.fn((error: unknown) => !(error instanceof ORPCError))
200+
const handler = new RPCHandler({
201+
ping: os.handler(procedureHandler),
202+
}, {
203+
strictGetMethodPluginEnabled: false,
204+
plugins: [new RethrowHandlerPlugin({ filter })],
205+
})
206+
207+
const post = (body: string) => new Request('http://localhost/ping', {
208+
method: 'POST',
209+
headers: { 'content-type': 'application/json' },
210+
body,
211+
})
212+
213+
it.each([
214+
['body', () => post('{bad')],
215+
['query', () => new Request('http://localhost/ping?data=%7Bbad')],
216+
])('passes %s decode failures to the filter as BAD_REQUEST and does not rethrow them', async (_, buildRequest) => {
217+
const { response } = await handler.handle(buildRequest())
218+
219+
expect(response?.status).toBe(400)
220+
expect(procedureHandler).not.toHaveBeenCalled()
221+
expect(filter).toHaveBeenCalledOnce()
222+
expect(filter.mock.calls[0]![0]).toSatisfy(
223+
(error: unknown) => error instanceof ORPCError && error.code === 'BAD_REQUEST',
224+
)
225+
})
226+
227+
it('still rethrows procedure errors', async () => {
228+
const error = new TypeError('__TEST__')
229+
procedureHandler.mockRejectedValueOnce(error)
230+
231+
await expect(handler.handle(post('{}'))).rejects.toBe(error)
232+
})
233+
})

0 commit comments

Comments
 (0)