Skip to content

Commit 1df6f2b

Browse files
dinwwwhclaude
andauthored
fix(shared): infer unknown instead of ThrowableError when next has no typed error (#2464)
## Problem `onError`, `onFinish`, `onAsyncIteratorObjectError` and `onReadableStreamError` inferred the error type with: ```ts ReturnType<TOptions['next']> extends PromiseWithError<any, infer E> ? E : ThrowableError ``` `PromiseWithError<T, E>` is `Promise<T> & { __error?: { type: E } }`. `__error` is optional, so a plain `Promise` already matches with `E = unknown`. The `ThrowableError` fallback was only reached when `next()` didn't return a promise, which made the result inconsistent: a plain promise gave `unknown`, but a non-promise return gave `Error`. ## Fix Add an exported `InferNextError<TOptions>` type and use it in all four helpers. It infers `E` from `PromiseWithError` when `next()` carries a typed error, and gives `unknown` otherwise. There is no longer a `ThrowableError` fallback. - Typed `PromiseWithError` errors (e.g. `clientInterceptors`) behave as before. - Plain-promise `next()` (handler `interceptors` and `routingInterceptors`) gives `unknown`, as before. - Non-promise `next()` now gives `unknown` instead of `ThrowableError`. - `onAsyncIteratorObjectError` and `onReadableStreamError` still union the inferred error with `ThrowableError`, so with no typed error they give `unknown`. ## Tests - `packages/shared/src/interceptor.test-d.ts`: new cases for a plain-promise `next()` and a non-promise `next()`. The non-promise case fails on `main`, where the error was `Error`. - `packages/server/src/adapters/fetch/rpc-handler.test-d.ts` (new): pins `unknown` for `onError` and `onFinish` in `RPCHandler` `interceptors` and `routingInterceptors`. `pnpm check` and `pnpm test` pass. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01VVaw7P6j7fKqfLdPENbs1h --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 850fec8 commit 1df6f2b

3 files changed

Lines changed: 89 additions & 21 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { onError, onFinish } from '@orpc/shared'
2+
3+
import { os } from '../../builder'
4+
import { RPCHandler } from './rpc-handler'
5+
6+
const router = {
7+
ping: os.handler(() => 'pong'),
8+
}
9+
10+
it('infers the error type of onError and onFinish in handler interceptors', () => {
11+
const _handler = new RPCHandler(router, {
12+
routingInterceptors: [
13+
onError((error) => {
14+
expectTypeOf(error).toEqualTypeOf<unknown>()
15+
}),
16+
],
17+
interceptors: [
18+
onError((error) => {
19+
expectTypeOf(error).toEqualTypeOf<unknown>()
20+
}),
21+
onFinish(([error, , isSuccess]) => {
22+
if (!isSuccess) {
23+
expectTypeOf(error).toEqualTypeOf<unknown>()
24+
} else {
25+
expectTypeOf(error).toEqualTypeOf<null>()
26+
}
27+
}),
28+
],
29+
})
30+
})

‎packages/shared/src/interceptor.test-d.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,51 @@ it('onStart, onSuccess, onError, onFinish, onAsyncIteratorObjectError, onReadabl
7676
}),
7777
)
7878
})
79+
80+
it('infers unknown error when next returns a plain Promise', () => {
81+
const interceptors: Interceptor<{ foo: string }, Promise<'success'>>[] = []
82+
83+
interceptors.push(
84+
onError((error) => {
85+
expectTypeOf(error).toEqualTypeOf<unknown>()
86+
}),
87+
)
88+
89+
interceptors.push(
90+
onFinish(([error, data, isSuccess]) => {
91+
if (!isSuccess) {
92+
expectTypeOf(error).toEqualTypeOf<unknown>()
93+
expectTypeOf(data).toEqualTypeOf<undefined>()
94+
} else {
95+
expectTypeOf(error).toEqualTypeOf<null>()
96+
expectTypeOf(data).toEqualTypeOf<'success'>()
97+
}
98+
}),
99+
)
100+
101+
interceptors.push(
102+
onAsyncIteratorObjectError((error) => {
103+
expectTypeOf(error).toEqualTypeOf<unknown>()
104+
}),
105+
)
106+
107+
interceptors.push(
108+
onReadableStreamError((error) => {
109+
expectTypeOf(error).toEqualTypeOf<unknown>()
110+
}),
111+
)
112+
})
113+
114+
it('infers unknown error when next does not return a promise', () => {
115+
type Options = { next: () => 'success' }
116+
117+
onError<Options, []>((error) => {
118+
expectTypeOf(error).toEqualTypeOf<unknown>()
119+
})
120+
121+
onFinish<Options, []>(([error, , isSuccess]) => {
122+
if (!isSuccess) {
123+
expectTypeOf(error).toEqualTypeOf<unknown>()
124+
}
125+
})
126+
})

‎packages/shared/src/interceptor.ts‎

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ export type Interceptor<TOptions extends InterceptableOptions, TResult> = (
1919
options: InterceptorOptions<TOptions, TResult>,
2020
) => TResult
2121

22+
/**
23+
* Infers the error type of `next()` from `PromiseWithError`, or `unknown` when
24+
* `next()` doesn't carry a typed error (e.g. it returns a plain promise).
25+
*/
26+
export type InferNextError<TOptions extends { next: () => any }> =
27+
ReturnType<TOptions['next']> extends PromiseWithError<any, infer E> ? E : unknown
28+
2229
/**
2330
* Can used for interceptors or middlewares
2431
*/
@@ -58,13 +65,7 @@ export function onSuccess<TOptions extends { next: () => any }, TRest extends an
5865
*/
5966
export function onError<TOptions extends { next: () => any }, TRest extends any[]>(
6067
callback: NoInfer<
61-
(
62-
error: ReturnType<TOptions['next']> extends PromiseWithError<any, infer E>
63-
? E
64-
: ThrowableError,
65-
options: TOptions,
66-
...rest: TRest
67-
) => Promisable<void>
68+
(error: InferNextError<TOptions>, options: TOptions, ...rest: TRest) => Promisable<void>
6869
>,
6970
): (options: TOptions, ...rest: TRest) => Promise<Awaited<ReturnType<TOptions['next']>>> {
7071
return async (options, ...rest) => {
@@ -87,10 +88,7 @@ export type OnFinishState<TResult, TError> =
8788
export function onFinish<TOptions extends { next: () => any }, TRest extends any[]>(
8889
callback: NoInfer<
8990
(
90-
state: OnFinishState<
91-
Awaited<ReturnType<TOptions['next']>>,
92-
ReturnType<TOptions['next']> extends PromiseWithError<any, infer E> ? E : ThrowableError
93-
>,
91+
state: OnFinishState<Awaited<ReturnType<TOptions['next']>>, InferNextError<TOptions>>,
9492
options: TOptions,
9593
...rest: TRest
9694
) => Promisable<void>
@@ -127,11 +125,7 @@ export function onAsyncIteratorObjectError<
127125
>(
128126
callback: NoInfer<
129127
(
130-
error:
131-
| ThrowableError
132-
| (ReturnType<TOptions['next']> extends PromiseWithError<any, infer E>
133-
? E
134-
: ThrowableError),
128+
error: ThrowableError | InferNextError<TOptions>,
135129
options: TOptions,
136130
...rest: TRest
137131
) => Promisable<void>
@@ -175,11 +169,7 @@ export function onAsyncIteratorObjectError<
175169
export function onReadableStreamError<TOptions extends { next: () => any }, TRest extends any[]>(
176170
callback: NoInfer<
177171
(
178-
error:
179-
| ThrowableError
180-
| (ReturnType<TOptions['next']> extends PromiseWithError<any, infer E>
181-
? E
182-
: ThrowableError),
172+
error: ThrowableError | InferNextError<TOptions>,
183173
options: TOptions,
184174
...rest: TRest
185175
) => Promisable<void>

0 commit comments

Comments
 (0)