-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix error serialization for rpc calls (#1278)
- Loading branch information
1 parent
cc98a1e
commit e886145
Showing
7 changed files
with
83 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"apollo-client-devtools": patch | ||
--- | ||
|
||
Fix issue with error serialization when sending an error back through the message passing system. Unfortunately the raw error instance was lost in this process. This fix retains the error message when sending error messages in rpc calls. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const RPC_MESSAGE_TIMEOUT = "RPC_MESSAGE_TIMEOUT"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const errorConstructors = [ | ||
EvalError, | ||
RangeError, | ||
ReferenceError, | ||
SyntaxError, | ||
TypeError, | ||
URIError, | ||
].reduce( | ||
(memo, constructor) => memo.set(constructor.name, constructor), | ||
new Map<string, ErrorConstructor>() | ||
); | ||
|
||
export function serializeError(error: unknown) { | ||
return error instanceof Error | ||
? { name: error.name, message: error.message, stack: error.stack } | ||
: { message: String(error) }; | ||
} | ||
|
||
export function deserializeError({ | ||
name, | ||
message, | ||
stack, | ||
}: { | ||
name?: string; | ||
message: string; | ||
stack?: string; | ||
}) { | ||
const ErrorClass = name ? errorConstructors.get(name) ?? Error : Error; | ||
const error = new ErrorClass(message); | ||
|
||
if (name && error.name !== name) { | ||
error.name = name; | ||
} | ||
|
||
if (stack) { | ||
error.stack = stack; | ||
} | ||
|
||
return error; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
{ | ||
"compilerOptions": { | ||
"allowJs": true, | ||
"sourceMap": true, | ||
"noImplicitAny": true, | ||
"strictNullChecks": true, | ||
|