@lifi/rpc-wrapper
Version:
LI.FI rpc-wrapper
52 lines (51 loc) • 1.61 kB
JavaScript
import { Type } from '@sinclair/typebox';
/**
* Converts an error into a json object
*
* @param error - Error to convert
* @returns An error json
*/
export const jsonifyError = (error) => {
if (error.toJson && typeof error.toJson === 'function') {
return error.toJson();
}
return {
message: error.message,
type: error.name,
context: {},
stack: error.stack,
};
};
export const CustomErrorJsonSchema = Type.Object({
message: Type.String(),
context: Type.Any(),
type: Type.String(),
stack: Type.Optional(Type.String()),
});
/**
* @classdesc The error class used throughout this repo. Defines a context object in addition to the standard message and name fields. The context can hold any information in json form that is relevant to the error
*
* Is also able to be hydrated from a json
*/
export class CustomError extends Error {
constructor(msg, context = {}, type = CustomError.name, level = 'error') {
super(msg);
this.msg = msg;
this.context = context;
this.type = type;
this.level = level;
this.isCustomError = true;
}
toJson() {
return {
message: this.msg,
context: this.context,
type: this.type,
stack: this.stack,
};
}
static fromJson(json) {
var _a, _b, _c;
return new CustomError(json.message, (_a = json.context) !== null && _a !== void 0 ? _a : {}, (_c = (_b = json.type) !== null && _b !== void 0 ? _b : json.name) !== null && _c !== void 0 ? _c : CustomError.name);
}
}