@lodestar/beacon-node
Version:
A Typescript implementation of the beacon chain
43 lines • 1.8 kB
JavaScript
import { REQUEST_ERROR_CLASS_NAME, RESPONSE_ERROR_CLASS_NAME, RequestError, ResponseError } from "@lodestar/reqresp";
import { LodestarError } from "@lodestar/utils";
/**
* Structured clone does not work with Error objects.
* For LodestarError, we want to specify the LodestarErrorObject with className so that we can deserialize later.
*/
export function toThreadBoundaryError(error) {
if (error instanceof LodestarError) {
return { error: null, object: error.toObject() };
}
// note that non-clonable errors will be deserialized as a generic Error object
return { error, object: null };
}
/**
* Only RequestError and ResponseError pass through thread boundaries.
* If we pass more errors in the future through thread boundaries, we need to add them here.
*/
const fromObjectFnRegistry = new Map([
[RESPONSE_ERROR_CLASS_NAME, ResponseError.fromObject],
[REQUEST_ERROR_CLASS_NAME, RequestError.fromObject],
]);
/**
* If error is LodestarError, deserialize it from the LodestarErrorObject.
* Else use the generic Error object.
*/
export function fromThreadBoundaryError(error) {
if (error.error) {
// this is always a generic Error object
return error.error;
}
let clonedError;
const fromObjectFn = fromObjectFnRegistry.get(error.object.className);
if (fromObjectFn) {
clonedError = fromObjectFn(error.object);
}
else {
// should not happen as a LodestarError class should implement "fromObject" method and register it
// try our best to clone the error with the same stack trace
clonedError = new LodestarError({ code: "UNKNOWN_ERROR_CLASS" }, `Unknown error class ${error.object.className}`, error.object.stack);
}
return clonedError;
}
//# sourceMappingURL=error.js.map