@lifi/rpc-wrapper
Version:
LI.FI rpc-wrapper
57 lines (56 loc) • 1.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CustomError = exports.CustomErrorJsonSchema = exports.jsonifyError = void 0;
const typebox_1 = require("@sinclair/typebox");
/**
* Converts an error into a json object
*
* @param error - Error to convert
* @returns An error json
*/
const jsonifyError = (error) => {
if (error.toJson && typeof error.toJson === 'function') {
return error.toJson();
}
return {
message: error.message,
type: error.name,
context: {},
stack: error.stack,
};
};
exports.jsonifyError = jsonifyError;
exports.CustomErrorJsonSchema = typebox_1.Type.Object({
message: typebox_1.Type.String(),
context: typebox_1.Type.Any(),
type: typebox_1.Type.String(),
stack: typebox_1.Type.Optional(typebox_1.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
*/
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);
}
}
exports.CustomError = CustomError;