Repository navigation
Commit 4bdb6f5
fix(contract): reconcile mid-stream errors in response validation plugin (#2380)
## Problem
`ResponseValidationLinkPlugin` reconciles `ORPCError`s against the
contract's `errorMap` with a `try { return await next() } catch { ...
}`. That only catches errors thrown before `next()` resolves. When the
output is an async iterator, `next()` resolves as soon as the stream
opens, so errors thrown while the stream is being read never go through
`reconcileORPCError`.
This applies even when the stream has an output schema. The
`asyncIteratorObject` schema wraps the stream, but only to validate
yielded values. Errors pass through it unchanged.
Example: a contract with `outputSchemas:
[asyncIteratorObject(z.number())]` and error map `NOT_FOUND: { data:
z.object({ value: z.string().transform(v => parseInt(v)) }) }`. The
stream yields `1`, then throws `NOT_FOUND { value: '123' }`:
| | `defined` | `data` |
|-----------------------|-----------|--------------------|
| Error returned directly | `true` | `{ value: 123 }` |
| Error thrown mid-stream (before this PR) | `false` | `{ value: '123'
}` |
So `isDefinedError` / `safe()` treat an error the contract defines as
undefined, and `error.data` doesn't match its static type.
## Fix
When `next()` returns an async iterator, the plugin now wraps it with
`wrapAsyncIteratorPreservingEventMeta(output, { mapError: reconcileError
})`, inside `override(...)`. This is the same pattern
`packages/server/src/procedure-client.ts` already uses for server-side
outputs. Errors thrown before the stream starts are reconciled through
the same `reconcileError` helper as before.
## Tests
Added to `packages/contract/src/plugins/response-validation.test.ts`:
- A mid-stream `ORPCError` is reconciled against the procedure's error
map, and only when that item is read.
- The error's event meta (`id`, `retry`) is kept after reconciliation.
- Errors that aren't `ORPCError` pass through without reconciliation.
- With the real `reconcileORPCError`, a mid-stream error comes out with
`defined === true` and transformed `data`.
The first, second and fourth tests fail without the fix. The existing
tests in this file now share a `mockResponse` helper for their mock
setup.
`pnpm vitest run packages/contract`, `pnpm type:check` and `pnpm lint`
pass.
---
_Generated by [Claude
Code](https://claude.ai/code/session_01VVaw7PzHC3szJidv5pjRfn)_
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 0e38bd8 commit 4bdb6f5
2 files changed
Lines changed: 134 additions & 79 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
40 | 41 | | |
41 | 42 | | |
42 | 43 | | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
43 | 50 | | |
44 | 51 | | |
45 | 52 | | |
46 | 53 | | |
47 | 54 | | |
48 | 55 | | |
| 56 | + | |
49 | 57 | | |
50 | 58 | | |
51 | 59 | | |
| |||
66 | 74 | | |
67 | 75 | | |
68 | 76 | | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
69 | 83 | | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
| 84 | + | |
85 | 85 | | |
86 | 86 | | |
87 | 87 | | |
| |||
90 | 90 | | |
91 | 91 | | |
92 | 92 | | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
| 93 | + | |
108 | 94 | | |
109 | 95 | | |
110 | 96 | | |
| |||
113 | 99 | | |
114 | 100 | | |
115 | 101 | | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
| 102 | + | |
131 | 103 | | |
132 | 104 | | |
133 | 105 | | |
| |||
150 | 122 | | |
151 | 123 | | |
152 | 124 | | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | 125 | | |
166 | 126 | | |
167 | 127 | | |
168 | | - | |
| 128 | + | |
169 | 129 | | |
170 | 130 | | |
171 | 131 | | |
| |||
175 | 135 | | |
176 | 136 | | |
177 | 137 | | |
178 | | - | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | 138 | | |
191 | 139 | | |
| 140 | + | |
| 141 | + | |
192 | 142 | | |
193 | 143 | | |
194 | 144 | | |
195 | 145 | | |
196 | 146 | | |
197 | 147 | | |
198 | 148 | | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
199 | 234 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | | - | |
5 | | - | |
| 5 | + | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
28 | 29 | | |
29 | 30 | | |
30 | 31 | | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
| 32 | + | |
35 | 33 | | |
36 | | - | |
| 34 | + | |
37 | 35 | | |
38 | 36 | | |
39 | | - | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
40 | 60 | | |
41 | 61 | | |
42 | 62 | | |
| |||
0 commit comments