Skip to content

Commit

Permalink
fix: Converted global state for fromError to handle it internally.
Browse files Browse the repository at this point in the history
*Related: #10
Reviewed-by: @tegefaulkes
[ci skip]
  • Loading branch information
addievo committed Sep 22, 2023
1 parent 2d92d6d commit 91bb0e0
Showing 1 changed file with 30 additions and 27 deletions.
57 changes: 30 additions & 27 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,38 +223,41 @@ function parseJSONRPCMessage<T extends JSONValue>(
* Polykey errors are handled by their inbuilt `toJSON` method , so this only
* serialises other errors
*/
const createReplacer = () => {
let withinErrorKey = false;

let withinErrorKey = false;

const replacer = (key: string, value: any) => {
if (key === 'error') {
withinErrorKey = true;
}
if (withinErrorKey && key !== 'code' && key !== 'message') {
if (value instanceof ErrorRPC) {
withinErrorKey = false;
return {
code: value.code,
message: value.description,
'data?': value.data,
type: value.constructor.name,
};
return (key: string, value: any) => {
if (key === 'error') {
withinErrorKey = true;
}
if (value instanceof AggregateError) {
withinErrorKey = false;
return {
type: value.constructor.name,
data: {
errors: value.errors,
message: value.message,
stack: value.stack,
},
};
if (withinErrorKey && key !== 'code' && key !== 'message') {
if (value instanceof ErrorRPC) {
withinErrorKey = false;
return {
code: value.code,
message: value.description,
'data?': value.data,
type: value.constructor.name,
};
}
if (value instanceof AggregateError) {
withinErrorKey = false;
return {
type: value.constructor.name,
data: {
errors: value.errors,
message: value.message,
stack: value.stack,
},
};
}
}
}
return value;
return value;
};
};

const replacer = createReplacer();

/**
* Serializes Error instances into RPC errors
* Use this on the sending side to send exceptions
Expand Down

0 comments on commit 91bb0e0

Please sign in to comment.