Commit 37d8836
fix(client): preserve subclass prototype chain in cloneORPCError (#1801)
`cloneORPCError` rebuilt errors with `new ORPCError(...)`, which
stripped custom subclass prototypes, so `error instanceof CustomError`
failed after error reconciliation. The clone is now constructed as a
real `ORPCError` whose prototype is swapped to the original's, so
subclass instances stay `instanceof` their class while the clone remains
a native `Error`.
Supersedes #1799, thanks @Wadiou for the report and the initial
approach.
## Fixes
- Cloned errors are `instanceof` their subclass, and custom properties
are copied with their exact descriptors.
- Unlike the `Object.create` approach in #1799, the clone keeps native
`Error` semantics: `message` / `stack` / `cause` stay non-enumerable, so
`{ ...error }` and key iteration do not expose stack traces, and
`structuredClone` / `postMessage` still round-trip.
- Signature widened to `<T extends AnyORPCError>(error: T): T`, backward
compatible.
## Testing
- New tests cover subclass prototype preservation and clone shape /
native-error semantics; the shape test fails under the #1799 approach.
- All client, contract, server, and json-schema tests pass (1157), `tsc`
clean.
---------
Co-authored-by: Wadoud <wadiouyt@gmail.com>1 parent 722f8df commit 37d8836
2 files changed
Lines changed: 42 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
234 | 234 | | |
235 | 235 | | |
236 | 236 | | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
237 | 267 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
54 | 54 | | |
55 | 55 | | |
56 | 56 | | |
57 | | - | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
58 | 66 | | |
59 | | - | |
60 | 67 | | |
61 | 68 | | |
62 | 69 | | |
63 | 70 | | |
64 | 71 | | |
| 72 | + | |
| 73 | + | |
65 | 74 | | |
66 | | - | |
67 | | - | |
68 | 75 | | |
69 | | - | |
| 76 | + | |
70 | 77 | | |
0 commit comments