Skip to content

Commit 91bb0e0

Browse files
committed
fix: Converted global state for fromError to handle it internally.
*Related: #10 Reviewed-by: @tegefaulkes [ci skip]
1 parent 2d92d6d commit 91bb0e0

File tree

1 file changed

+30
-27
lines changed

1 file changed

+30
-27
lines changed

src/utils/utils.ts

+30-27
Original file line numberDiff line numberDiff line change
@@ -223,38 +223,41 @@ function parseJSONRPCMessage<T extends JSONValue>(
223223
* Polykey errors are handled by their inbuilt `toJSON` method , so this only
224224
* serialises other errors
225225
*/
226+
const createReplacer = () => {
227+
let withinErrorKey = false;
226228

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;
242232
}
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+
}
253254
}
254-
}
255-
return value;
255+
return value;
256+
};
256257
};
257258

259+
const replacer = createReplacer();
260+
258261
/**
259262
* Serializes Error instances into RPC errors
260263
* Use this on the sending side to send exceptions

0 commit comments

Comments
 (0)