@bitblit/epsilon
Version:
Tiny adapter to simplify building API gateway Lambda APIS
96 lines • 3.27 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EpsilonBackgroundProcessError = void 0;
const util_1 = __importDefault(require("util"));
class EpsilonBackgroundProcessError extends Error {
constructor(...errors) {
super(EpsilonBackgroundProcessError.combineErrorStringsWithDefault(errors));
Object.setPrototypeOf(this, EpsilonBackgroundProcessError.prototype);
this._errors = errors;
this[EpsilonBackgroundProcessError.EPSILON_BACKGROUND_PROCESS_ERROR_FLAG_KEY] = true; // Just used to tell if one has been wrapped
}
static combineErrorStringsWithDefault(errors, defMessage = 'Internal Server Error') {
return errors && errors.length > 0 ? errors.join(',') : defMessage;
}
setFormattedErrorMessage(format, ...input) {
const msg = util_1.default.format(format, ...input);
this.errors = [msg];
}
withFormattedErrorMessage(format, ...input) {
this.setFormattedErrorMessage(format, ...input);
return this;
}
withErrors(errors) {
this.errors = errors; // Call setter
return this;
}
withDetailErrorCode(detailErrorCode) {
this._detailErrorCode = detailErrorCode; // Call setter
return this;
}
withDetails(details) {
this._details = details; // Call setter
return this;
}
withRequestId(requestId) {
this._requestId = requestId; // Call setter
return this;
}
withWrappedError(err) {
this._wrappedError = err; // Call setter
return this;
}
isWrappedError() {
return !!this._wrappedError;
}
static wrapError(err) {
let rval = null;
if (EpsilonBackgroundProcessError.objectIsEpsilonBackgroundProcessError(err)) {
rval = err;
}
else {
rval = new EpsilonBackgroundProcessError(err.message).withWrappedError(err);
}
return rval;
}
static objectIsEpsilonBackgroundProcessError(obj) {
return obj && obj['__epsilonHttpErrorFlag'] === true;
}
set errors(value) {
this._errors = value || ['Internal Server Error'];
this.message = EpsilonBackgroundProcessError.combineErrorStringsWithDefault(this._errors);
}
get errors() {
return this._errors;
}
set detailErrorCode(value) {
this._detailErrorCode = value;
}
get detailErrorCode() {
return this._detailErrorCode;
}
set details(value) {
this._details = value;
}
get details() {
return this._details;
}
set requestId(value) {
this._requestId = value || 'MISSING';
}
get requestId() {
return this._requestId;
}
set wrappedError(value) {
this._wrappedError = value;
}
get wrappedError() {
return this._wrappedError;
}
}
exports.EpsilonBackgroundProcessError = EpsilonBackgroundProcessError;
EpsilonBackgroundProcessError.EPSILON_BACKGROUND_PROCESS_ERROR_FLAG_KEY = '__epsilonBackgroundProcessErrorFlag';
//# sourceMappingURL=epsilon-background-process-error.js.map