Skip to content

Commit 6e40c00

Browse files
authored
docs: Customizing Error Response Format (#726)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Documentation** * Added a new guide explaining how to customize error response formats in oRPC OpenAPI handlers. * Updated the sidebar navigation to include "Customizing Error Response" as the first item in the "Advanced" section. * Improved the RPCHandler lifecycle documentation by clarifying the request/response encoding steps and detailing success and failure handling in the sequence diagram. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 9903230 commit 6e40c00

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

apps/content/.vitepress/config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,9 @@ export default withMermaid(defineConfig({
230230
text: 'Advanced',
231231
collapsed: true,
232232
items: [
233-
{ text: 'Redirect Response', link: '/docs/openapi/advanced/redirect-response' },
233+
{ text: 'Customizing Error Response', link: '/docs/openapi/advanced/customizing-error-response' },
234234
{ text: 'OpenAPI JSON Serializer', link: '/docs/openapi/advanced/openapi-json-serializer' },
235+
{ text: 'Redirect Response', link: '/docs/openapi/advanced/redirect-response' },
235236
],
236237
},
237238
],
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
title: Customizing Error Response Format
3+
description: Learn how to customize the error response format in oRPC OpenAPI handlers to match your application's requirements and improve client compatibility.
4+
---
5+
6+
# Customizing Error Response Format
7+
8+
If you need to customize the error response format to match your application's requirements, you can use [`rootInterceptors`](/docs/rpc-handler#lifecycle) in the handler.
9+
10+
::: warning
11+
Avoid combining this with [Type‑Safe Error Handling](/docs/error-handling#type‐safe-error-handling), as the [OpenAPI Specification Generator](/docs/openapi/openapi-specification) does not yet support custom error response formats.
12+
:::
13+
14+
```ts
15+
import { isORPCErrorJson, isORPCErrorStatus } from '@orpc/client'
16+
17+
const handler = new OpenAPIHandler(router, {
18+
rootInterceptors: [
19+
async ({ next }) => {
20+
const result = await next()
21+
22+
if (
23+
result.matched
24+
&& isORPCErrorStatus(result.response.status)
25+
&& isORPCErrorJson(result.response.body)
26+
) {
27+
return {
28+
...result,
29+
response: {
30+
...result.response,
31+
body: {
32+
...result.response.body,
33+
message: 'custom error shape',
34+
},
35+
},
36+
}
37+
}
38+
39+
return result
40+
},
41+
],
42+
})
43+
```

apps/content/docs/rpc-handler.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ sequenceDiagram
112112
113113
Note over A1: adaptorInterceptors
114114
A1 ->> P3: request
115+
P3 ->> P3: Convert
115116
Note over P3: rootInterceptors
116117
P3 ->> P4: standard request
117118
Note over P4: interceptors
@@ -123,8 +124,11 @@ sequenceDiagram
123124
P4 ->> P5: Input, Signal, LastEventId,...
124125
Note over P5: clientInterceptors
125126
P5 ->> P5: Handle
126-
P5 ->> P4: Error/Output
127-
P4 ->> P4: Encode Error/Output
127+
P5 ->> P4: if success
128+
P4 ->> P4: Encode output
129+
P5 ->> P4: if failed
130+
Note over P4: end interceptors
131+
P4 ->> P4: Encode error
128132
P4 ->> P3: standard response
129133
P3 ->> A1: response
130134
```

0 commit comments

Comments
 (0)