Skip to content

Commit b592061

Browse files
authored
refactor(client, contract, json-schema)!: make ORPCError code and data readonly and fix the name brand (#2523)
Pre-stable cleanup of the `ORPCError` surface. ## What changed - **Name brand:** the type brand on `ORPCError#name` was spelled `__branch`; it is now `__brand`. It was introduced once in the v2 commit with a JSDoc that calls it "type branding", so this is a typo fix that shows up in hover types. - **Readonly `code` and `data`:** both are now `readonly`, like `defined`, so a defined error cannot be changed in place after it was matched against the error map. This is intentionally not documented. - **`COMMON_ERROR_STATUS_MAP`:** declared `as const`, so its values are literal types and writes are a type error. It is not frozen at runtime. ## Breaking - Assigning `error.code` or `error.data` no longer type-checks. - `COMMON_ERROR_STATUS_MAP` values are literal types instead of `number`, and assigning to its keys no longer type-checks. ## Notes for review - The two internal writers of `data` (`reconcileORPCError` in `@orpc/contract` and the smart coercion link plugin in `@orpc/json-schema`) set it on a fresh clone through a `Writable` cast, the same pattern already used for `defined`. - `ORPCError` still has no `defined` constructor option, and `cloneORPCError` is unchanged. - No docs or migration notes changed.
1 parent da02454 commit b592061

6 files changed

Lines changed: 36 additions & 25 deletions

File tree

‎packages/client/src/error-utils.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ describe('cloneORPCError', () => {
214214
const cloned = cloneORPCError(original)
215215

216216
// Modifying cloned data doesn't affect original
217-
cloned.data = 2
217+
;(cloned.data as any) = 2
218218

219219
expect(original.data).toBe(1)
220220
expect(cloned.data).toBe(2)

‎packages/client/src/error.test-d.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,21 @@ describe('ORPCError', () => {
2828
const _error23: ORPCError<'CODE', 'required'> = new ORPCError('CODE', { data: 'invalid' })
2929
})
3030

31-
it('not allow write .defined property', () => {
32-
const error = new ORPCError('CODE')
31+
it('not allow write .defined, .code and .data properties', () => {
32+
const error = new ORPCError('CODE', { data: 1 })
3333
// @ts-expect-error - not allow write
3434
error.defined = true as any
35+
// @ts-expect-error - not allow write
36+
error.code = 'CODE'
37+
// @ts-expect-error - not allow write
38+
error.data = 2
39+
})
40+
41+
it('toJSON returns a writable shape', () => {
42+
const json = new ORPCError('CODE', { data: 1 }).toJSON()
43+
json.defined = true
44+
json.code = 'CODE'
45+
json.message = 'message'
46+
json.data = 2
3547
})
3648
})

‎packages/client/src/error.test.ts‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,9 @@ describe('oRPCError', () => {
2222
expect(error.message).toBe('Bad Gateway')
2323
})
2424

25-
it('can force write .defined', () => {
26-
const error = new ORPCError('BAD_GATEWAY')
27-
28-
expect(error.defined).toBe(false)
29-
30-
;(error.defined as any) = true
31-
32-
expect(error.defined).toBe(true)
25+
it('ignores a defined option', () => {
26+
// @ts-expect-error - defined is not an option
27+
expect(new ORPCError('BAD_GATEWAY', { defined: true }).defined).toBe(false)
3328
})
3429

3530
it('.toJSON', () => {

‎packages/client/src/error.ts‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export const COMMON_ERROR_STATUS_MAP = {
3333
BAD_GATEWAY: 502,
3434
SERVICE_UNAVAILABLE: 503,
3535
GATEWAY_TIMEOUT: 504,
36-
}
36+
} as const
3737

3838
/**
3939
* Error codes accepted by `ORPCError`: common HTTP codes plus any string.
@@ -90,19 +90,19 @@ export class ORPCError<TCode extends ORPCErrorCode, TData> extends Error {
9090

9191
/**
9292
* @remarks
93-
* **Note**: The `__branch` property is used for type branding, helping TypeScript distinguish
93+
* **Note**: The `__brand` property is used for type branding, helping TypeScript distinguish
9494
* an `ORPCError` instance from plain objects with a similar structure.
9595
*/
96-
override readonly name = 'ORPCError' as 'ORPCError' & { __branch: 'ORPCError' }
96+
override readonly name = 'ORPCError' as 'ORPCError' & { __brand: 'ORPCError' }
9797

9898
/**
9999
* Indicates whether the error matches a definition in the procedure's `.errors` map,
100100
* which makes its type inferable on the client.
101101
*/
102102
readonly defined: boolean = false
103103

104-
code: TCode
105-
data: TData
104+
readonly code: TCode
105+
readonly data: TData
106106

107107
constructor(code: TCode, ...rest: MaybeOptionalOptions<ORPCErrorOptions<TData>>) {
108108
const options = resolveMaybeOptionalOptions(rest)
@@ -151,14 +151,14 @@ export class ORPCError<TCode extends ORPCErrorCode, TData> extends Error {
151151
}
152152
}
153153

154-
export interface ORPCErrorJSON<TCode extends string, TData> extends Pick<
155-
ORPCError<TCode, TData>,
156-
'code' | 'message' | 'data'
157-
> {
158-
/**
159-
* remove readonly
160-
*/
154+
/**
155+
* Plain serialized form of an `ORPCError`. Unlike the class, its fields are writable.
156+
*/
157+
export interface ORPCErrorJSON<TCode extends string, TData> {
161158
defined: boolean
159+
code: TCode
160+
message: string
161+
data: TData
162162
}
163163

164164
export type AnyORPCError = ORPCError<any, any>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export async function reconcileORPCError(
6868

6969
const cloned = cloneORPCError(error)
7070

71-
cloned.data = validated.value
71+
;(cloned.data as Writable<typeof cloned.data>) = validated.value
7272
;(cloned.defined as Writable<typeof cloned.defined>) = true
7373

7474
return cloned

‎packages/json-schema/src/smart-coercion-link-plugin.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { cloneORPCError, ORPCError } from '@orpc/client'
33
import type { StandardLinkOptions, StandardLinkPlugin } from '@orpc/client/standard'
44
import type { AnySchema, ErrorMap, RouterContract } from '@orpc/contract'
55
import { getProcedureContractOrThrow } from '@orpc/contract'
6+
import type { Writable } from '@orpc/shared'
67
import { getOwn, toArray } from '@orpc/shared'
78

89
import type { JsonSchemaCoercerOptions } from './coercer'
@@ -76,7 +77,10 @@ export class SmartCoercionLinkPlugin<T extends ClientContext> implements Standar
7677
}
7778

7879
const cloned = cloneORPCError(error)
79-
cloned.data = this.coerceValue([dataSchema], cloned.data)
80+
;(cloned.data as Writable<typeof cloned.data>) = this.coerceValue(
81+
[dataSchema],
82+
cloned.data,
83+
)
8084
throw cloned
8185
}
8286
},

0 commit comments

Comments
 (0)