Registry Mirror
Skip to content

File tree

‎packages/contract/src/schema-utils.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,27 @@ it('toPublicSchemaIssue', () => {
5454
).toEqual({ message: 'a', path: [{ key: 'tags' }, 1] })
5555
expect(toPublicSchemaIssue({ message: 'b' })).toEqual({ message: 'b' })
5656
})
57+
58+
describe('toPublicSchemaIssue cuts the path at a non-property key', () => {
59+
it.each([[{ key: [[1]] }], [{ key: { a: 1 } }], [{ key: null }], [null], [[1]]])(
60+
'segment: %j',
61+
(segment) => {
62+
expect(
63+
toPublicSchemaIssue({ message: 'a', path: ['a', { key: 'b' }, segment as any, 'c'] }),
64+
).toEqual({ message: 'a', path: ['a', { key: 'b' }] })
65+
},
66+
)
67+
68+
it('valibot map and set keys', async () => {
69+
const schema = v.object({ map: v.map(v.string(), v.number()), set: v.set(v.string()) })
70+
const { issues } = await schema['~standard'].validate({
71+
map: new Map([[[1], 1]]),
72+
set: new Set([1]),
73+
})
74+
75+
expect(issues?.map(toPublicSchemaIssue)).toEqual([
76+
{ message: expect.any(String), path: [{ key: 'map' }] },
77+
{ message: expect.any(String), path: [{ key: 'set' }] },
78+
])
79+
})
80+
})

‎packages/contract/src/schema-utils.ts‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,21 @@ export function isSchemaIssue(issue: unknown): issue is SchemaIssue {
6868
* so it never echoes the input back to the client.
6969
*/
7070
export function toPublicSchemaIssue(issue: SchemaIssue): SchemaIssue {
71-
return {
72-
message: issue.message,
73-
path: issue.path?.map((segment) =>
74-
typeof segment === 'object' ? { key: segment.key } : segment,
75-
),
71+
if (issue.path === undefined) {
72+
return { message: issue.message }
73+
}
74+
75+
const path: (PropertyKey | { key: PropertyKey })[] = []
76+
77+
for (const segment of issue.path) {
78+
if (isPropertyKey(segment)) {
79+
path.push(segment)
80+
} else if (isNonPrimitive(segment) && isPropertyKey(segment.key)) {
81+
path.push({ key: segment.key })
82+
} else {
83+
break
84+
}
7685
}
86+
87+
return { message: issue.message, path }
7788
}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,34 @@ describe('standardHandler', () => {
549549
expect(error).toBeInstanceOf(ORPCError)
550550
expect(error.code).toBe('INTERNAL_SERVER_ERROR')
551551
})
552+
553+
it('falls back to INTERNAL_SERVER_ERROR when encoding the error fails', async () => {
554+
const encodingError = new RangeError('Maximum call stack size exceeded')
555+
client.mockRejectedValue(new ORPCError('BAD_REQUEST'))
556+
557+
codec.resolveProcedure.mockResolvedValue(makeResolved())
558+
codec.encodeError
559+
.mockRejectedValueOnce(encodingError)
560+
.mockResolvedValueOnce({ status: 500, headers: {}, body: 'internal' })
561+
562+
const result = await handler.handle(makeRequest(), OPTIONS)
563+
564+
expect(result).toEqual({
565+
matched: true,
566+
response: { status: 500, headers: {}, body: 'internal' },
567+
})
568+
expect(codec.encodeError).toHaveBeenCalledTimes(2)
569+
expect(codec.encodeError).toHaveBeenLastCalledWith(
570+
expect.objectContaining({
571+
code: 'INTERNAL_SERVER_ERROR',
572+
data: undefined,
573+
cause: encodingError,
574+
}),
575+
expect.anything(),
576+
['ping'],
577+
OPTIONS,
578+
)
579+
})
552580
})
553581

554582
describe('plugins', () => {

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,22 @@ export class StandardHandler<T extends Context> {
247247
? error
248248
: await reconcileORPCError(procedure['~orpc'].errorMap, error)
249249

250-
const response = await this.codec.encodeError(reconciled, procedure, path, {
251-
context,
252-
prefix,
253-
})
250+
let response: StandardResponse
251+
252+
try {
253+
response = await this.codec.encodeError(reconciled, procedure, path, {
254+
context,
255+
prefix,
256+
})
257+
} catch (cause) {
258+
recordSpanError(span, cause)
259+
response = await this.codec.encodeError(
260+
new ORPCError('INTERNAL_SERVER_ERROR', { cause }),
261+
procedure,
262+
path,
263+
{ context, prefix },
264+
)
265+
}
254266

255267
return { matched: true, response }
256268
}

0 commit comments

Comments
 (0)