UNPKG

@bitblit/ratchet-common

Version:

Common tools for general use

134 lines 4.05 kB
import { NumberRatchet } from '../lang/number-ratchet.js'; import { StringRatchet } from '../lang/string-ratchet.js'; export class RestfulApiHttpError extends Error { static RATCHET_RESTFUL_API_HTTP_ERROR_FLAG_KEY = '__ratchetRestfulApiHttpErrorFlag'; _httpStatusCode = 500; _errors; _detailErrorCode; _endUserErrors; _details; _requestId; _wrappedError; constructor(...errors) { super(RestfulApiHttpError.combineErrorStringsWithDefault(errors)); Object.setPrototypeOf(this, RestfulApiHttpError.prototype); this._errors = errors; this[RestfulApiHttpError.RATCHET_RESTFUL_API_HTTP_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; } withHttpStatusCode(httpStatusCode) { this.httpStatusCode = httpStatusCode; return this; } withErrors(errors) { this.errors = errors; return this; } withDetailErrorCode(detailErrorCode) { this._detailErrorCode = detailErrorCode; return this; } withEndUserErrors(endUserErrors) { this._endUserErrors = endUserErrors; 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 (RestfulApiHttpError.objectIsRestfulApiHttpError(err)) { rval = err; } else { rval = new RestfulApiHttpError(err.message).withWrappedError(err).withHttpStatusCode(500); } return rval; } static objectIsRestfulApiHttpError(obj) { return obj && obj[RestfulApiHttpError.RATCHET_RESTFUL_API_HTTP_ERROR_FLAG_KEY] === true; } get httpStatusCode() { return this._httpStatusCode; } set httpStatusCode(value) { this._httpStatusCode = value || 500; } get errors() { return this._errors; } set errors(value) { this._errors = value || ['Internal Server Error']; this.message = RestfulApiHttpError.combineErrorStringsWithDefault(this._errors); } get detailErrorCode() { return this._detailErrorCode; } set detailErrorCode(value) { this._detailErrorCode = value; } get endUserErrors() { return this._endUserErrors; } set endUserErrors(value) { this._endUserErrors = value; } get details() { return this._details; } set details(value) { this._details = value; } get requestId() { return this._requestId; } set requestId(value) { this._requestId = value || 'MISSING'; } get wrappedError() { return this._wrappedError; } set wrappedError(value) { this._wrappedError = value; } static errorIsX0x(errIn, xClass) { let rval = false; if (errIn && RestfulApiHttpError.objectIsRestfulApiHttpError(errIn)) { const err = errIn; const val = NumberRatchet.safeNumber(err.httpStatusCode); const bot = xClass * 100; const top = bot + 99; rval = val >= bot && val <= top; } return rval; } static errorIs40x(err) { return RestfulApiHttpError.errorIsX0x(err, 4); } static errorIs50x(err) { return RestfulApiHttpError.errorIsX0x(err, 5); } } //# sourceMappingURL=restful-api-http-error.js.map