2018-09-11 09:56:00 +00:00
|
|
|
const constructors = new Map([
|
|
|
|
[Error.name, Error],
|
|
|
|
[EvalError.name, EvalError],
|
|
|
|
[RangeError.name, RangeError],
|
|
|
|
[ReferenceError.name, ReferenceError],
|
|
|
|
[SyntaxError.name, SyntaxError],
|
|
|
|
[TypeError.name, TypeError],
|
|
|
|
[URIError.name, URIError]
|
|
|
|
])
|
|
|
|
|
2019-07-17 17:23:52 +00:00
|
|
|
export function deserialize (error: Electron.SerializedError): Electron.ErrorWithCause {
|
2018-09-26 05:44:55 +00:00
|
|
|
if (error && error.__ELECTRON_SERIALIZED_ERROR__ && constructors.has(error.name)) {
|
2018-09-11 09:56:00 +00:00
|
|
|
const constructor = constructors.get(error.name)
|
2019-07-17 17:23:52 +00:00
|
|
|
const deserializedError = new constructor!(error.message) as Electron.ErrorWithCause
|
2018-09-11 09:56:00 +00:00
|
|
|
deserializedError.stack = error.stack
|
2018-09-26 05:44:55 +00:00
|
|
|
deserializedError.from = error.from
|
|
|
|
deserializedError.cause = exports.deserialize(error.cause)
|
2018-09-11 09:56:00 +00:00
|
|
|
return deserializedError
|
|
|
|
}
|
|
|
|
return error
|
|
|
|
}
|
|
|
|
|
2019-07-17 17:23:52 +00:00
|
|
|
export function serialize (error: Electron.ErrorWithCause): Electron.SerializedError {
|
2018-09-11 09:56:00 +00:00
|
|
|
if (error instanceof Error) {
|
|
|
|
// Errors get lost, because: JSON.stringify(new Error('Message')) === {}
|
|
|
|
// Take the serializable properties and construct a generic object
|
|
|
|
return {
|
|
|
|
message: error.message,
|
|
|
|
stack: error.stack,
|
|
|
|
name: error.name,
|
2019-07-17 17:23:52 +00:00
|
|
|
from: process.type as Electron.ProcessType,
|
2018-09-26 05:44:55 +00:00
|
|
|
cause: exports.serialize(error.cause),
|
2018-09-11 09:56:00 +00:00
|
|
|
__ELECTRON_SERIALIZED_ERROR__: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return error
|
|
|
|
}
|