@opra/common
Version:
Opra common package
71 lines (70 loc) • 2.28 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpraException = void 0;
const objects_1 = require("@jsopen/objects");
const index_js_1 = require("../i18n/index.js");
/**
* Defines the base Opra exception, which is handled by the default Exceptions Handler.
*/
class OpraException extends Error {
constructor(issue, cause) {
super('Unknown error');
this.severity = 'error';
cause = cause || (issue instanceof Error ? issue : undefined);
if (issue instanceof Error)
cause = issue;
// noinspection SuspiciousTypeOfGuard
if (cause && cause instanceof Error) {
this.cause = cause;
if (cause.stack)
this.stack = cause.stack;
}
if (typeof issue === 'string')
this.initString(issue);
else if (issue instanceof Error)
this.initError(issue);
else
this.init(issue);
this.message = this.message || this.constructor.name;
}
toString() {
return index_js_1.i18n.deep(this.message);
}
toJSON() {
const env = process.env.NODE_ENV;
return (0, objects_1.omitUndefined)({
message: this.message,
severity: this.severity,
system: this.system,
code: this.code,
details: this.details,
stack: env === 'dev' || env === 'development' || env === 'test'
? this.stack?.split('\n')
: undefined,
}, true);
}
init(issue) {
this.message = issue?.message || this.constructor.name;
this.severity = issue?.severity || 'error';
if (issue) {
this.system = issue.system;
this.code = issue.code;
this.details = issue.details;
}
}
initString(issue) {
this.init({
message: String(issue || '') || this.constructor.name,
severity: 'error',
code: this.constructor.name,
});
}
initError(issue) {
this.init({
message: issue.message,
severity: issue.severity || 'error',
code: issue.code || issue.constructor.name,
});
}
}
exports.OpraException = OpraException;