@ganintegrity/gan-error
Version:
an extendable ES6 Error with support for HTTP errors
40 lines (33 loc) • 906 B
JavaScript
class GanError extends Error {
constructor(message) {
let originalError;
let data;
if (message instanceof Error) {
originalError = message;
message = originalError.message;
} else if (typeof message === 'object') {
data = message;
if (typeof data.message === 'string') {
message = data.message;
} else {
message = undefined;
}
}
super(message);
this.name = this.constructor.name;
if (typeof Error.captureStackTrace === 'function') {
Error.captureStackTrace(this, this.constructor);
} else {
this.stack = new Error(this.message).stack;
}
if (originalError) {
this.originalError = originalError;
if (!this.status && !!originalError.status) {
this.status = originalError.status;
}
} else if (data) {
this.data = data;
}
}
}
module.exports = GanError;