@bitblit/ratchet-epsilon-common
Version:
Tiny adapter to simplify building API gateway Lambda APIS
94 lines • 2.96 kB
JavaScript
import { StringRatchet } from '@bitblit/ratchet-common/lang/string-ratchet';
export class EpsilonBackgroundProcessError extends Error {
static EPSILON_BACKGROUND_PROCESS_ERROR_FLAG_KEY = '__epsilonBackgroundProcessErrorFlag';
_errors;
_detailErrorCode;
_details;
_requestId;
_wrappedError;
constructor(...errors) {
super(EpsilonBackgroundProcessError.combineErrorStringsWithDefault(errors));
Object.setPrototypeOf(this, EpsilonBackgroundProcessError.prototype);
this._errors = errors;
this[EpsilonBackgroundProcessError.EPSILON_BACKGROUND_PROCESS_ERROR_FLAG_KEY] = true;
}
static combineErrorStringsWithDefault(errors, defMessage = 'Internal Server Error') {
return errors && errors.length > 0 ? errors.join(',') : defMessage;
}
setFormattedErrorMessage(format, ...input) {
const msg = StringRatchet.format(format, ...input);
this.errors = [msg];
}
withFormattedErrorMessage(format, ...input) {
this.setFormattedErrorMessage(format, ...input);
return this;
}
withErrors(errors) {
this.errors = errors;
return this;
}
withDetailErrorCode(detailErrorCode) {
this._detailErrorCode = detailErrorCode;
return this;
}
withDetails(details) {
this._details = details;
return this;
}
withRequestId(requestId) {
this._requestId = requestId;
return this;
}
withWrappedError(err) {
this._wrappedError = err;
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[EpsilonBackgroundProcessError.EPSILON_BACKGROUND_PROCESS_ERROR_FLAG_KEY] === 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;
}
}
//# sourceMappingURL=epsilon-background-process-error.js.map