@@ -223,38 +223,41 @@ function parseJSONRPCMessage<T extends JSONValue>(
223
223
* Polykey errors are handled by their inbuilt `toJSON` method , so this only
224
224
* serialises other errors
225
225
*/
226
+ const createReplacer = ( ) => {
227
+ let withinErrorKey = false ;
226
228
227
- let withinErrorKey = false ;
228
-
229
- const replacer = ( key : string , value : any ) => {
230
- if ( key === 'error' ) {
231
- withinErrorKey = true ;
232
- }
233
- if ( withinErrorKey && key !== 'code' && key !== 'message' ) {
234
- if ( value instanceof ErrorRPC ) {
235
- withinErrorKey = false ;
236
- return {
237
- code : value . code ,
238
- message : value . description ,
239
- 'data?' : value . data ,
240
- type : value . constructor . name ,
241
- } ;
229
+ return ( key : string , value : any ) => {
230
+ if ( key === 'error' ) {
231
+ withinErrorKey = true ;
242
232
}
243
- if ( value instanceof AggregateError ) {
244
- withinErrorKey = false ;
245
- return {
246
- type : value . constructor . name ,
247
- data : {
248
- errors : value . errors ,
249
- message : value . message ,
250
- stack : value . stack ,
251
- } ,
252
- } ;
233
+ if ( withinErrorKey && key !== 'code' && key !== 'message' ) {
234
+ if ( value instanceof ErrorRPC ) {
235
+ withinErrorKey = false ;
236
+ return {
237
+ code : value . code ,
238
+ message : value . description ,
239
+ 'data?' : value . data ,
240
+ type : value . constructor . name ,
241
+ } ;
242
+ }
243
+ if ( value instanceof AggregateError ) {
244
+ withinErrorKey = false ;
245
+ return {
246
+ type : value . constructor . name ,
247
+ data : {
248
+ errors : value . errors ,
249
+ message : value . message ,
250
+ stack : value . stack ,
251
+ } ,
252
+ } ;
253
+ }
253
254
}
254
- }
255
- return value ;
255
+ return value ;
256
+ } ;
256
257
} ;
257
258
259
+ const replacer = createReplacer ( ) ;
260
+
258
261
/**
259
262
* Serializes Error instances into RPC errors
260
263
* Use this on the sending side to send exceptions
0 commit comments