Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions packages/server/src/adapters/standard/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,73 @@ describe('standardHandler', () => {
})
})

it('on decode error', async () => {
matcher.match.mockResolvedValue({
path: ['ping'],
procedure: ping,
params: { id: '__id__' },
})

const error = new Error('Something bad')
codec.decode.mockRejectedValueOnce(error)
const client = vi.fn()
vi.mocked(createProcedureClient).mockReturnValueOnce(client)

codec.decode.mockReturnValueOnce('__input__')

codec.encodeError.mockReturnValueOnce(response)

const result = await handler.handle(request, {
context: { db: 'postgres' },
prefix: '/api/v1',
})

expect(result).toEqual({ matched: true, response })

expect(matcher.match).toHaveBeenCalledOnce()
expect(matcher.match).toHaveBeenCalledWith('GET', '/users/1')

expect(createProcedureClient).toHaveBeenCalledOnce()
expect(createProcedureClient).toHaveBeenCalledWith(ping, {
context: { db: 'postgres' },
path: ['ping'],
})

expect(codec.decode).toHaveBeenCalledOnce()
expect(codec.decode).toHaveBeenCalledWith(request, { id: '__id__' }, ping)

expect(client).not.toHaveBeenCalledOnce()
expect(codec.encode).not.toBeCalled()

expect(codec.encodeError).toHaveBeenCalledOnce()
expect(codec.encodeError.mock.calls[0]![0]).toSatisfy((e: any) => {
expect(e).toBeInstanceOf(ORPCError)
expect(e.code).toEqual('BAD_REQUEST')
expect(e.message).toEqual(
`Malformed request. Ensure the request body is properly formatted and the 'Content-Type' header is set correctly.`,
)
expect(e.cause).toEqual(error)

return true
})

expect(interceptor).toHaveBeenCalledOnce()
expect(interceptor).toHaveBeenCalledWith({
request,
next: expect.any(Function),
context: { db: 'postgres' },
prefix: '/api/v1',
})

expect(interceptorRoot).toHaveBeenCalledOnce()
expect(interceptorRoot).toHaveBeenCalledWith({
request,
next: expect.any(Function),
context: { db: 'postgres' },
prefix: '/api/v1',
})
})

it('work without context and prefix', async () => {
matcher.match.mockResolvedValue({
path: ['ping'],
Expand Down
13 changes: 11 additions & 2 deletions packages/server/src/adapters/standard/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { Plugin } from '../../plugins'
import type { CreateProcedureClientOptions } from '../../procedure-client'
import type { Router } from '../../router'
import type { StandardCodec, StandardMatcher } from './types'
import { toORPCError } from '@orpc/contract'
import { ORPCError, toORPCError } from '@orpc/contract'
import { intercept, trim } from '@orpc/shared'
import { CompositePlugin } from '../../plugins'
import { createProcedureClient } from '../../procedure-client'
Expand Down Expand Up @@ -64,6 +64,8 @@ export class StandardHandler<T extends Context> {
context: options?.context ?? {} as T, // context is optional only when all fields are optional so we can safely force it to have a context
},
async (interceptorOptions) => {
let isDecoding = false

try {
return await intercept(
this.options.interceptors ?? [],
Expand All @@ -88,7 +90,9 @@ export class StandardHandler<T extends Context> {

const client = createProcedureClient(match.procedure, clientOptions)

isDecoding = true
const input = await this.codec.decode(request, match.params, match.procedure)
isDecoding = false

const output = await client(input, { signal: request.signal })

Expand All @@ -102,7 +106,12 @@ export class StandardHandler<T extends Context> {
)
}
catch (e) {
const error = toORPCError(e)
const error = isDecoding
? new ORPCError('BAD_REQUEST', {
message: `Malformed request. Ensure the request body is properly formatted and the 'Content-Type' header is set correctly.`,
cause: e,
})
: toORPCError(e)

const response = this.codec.encodeError(error)

Expand Down