@othree.io/excuses
Version:
Excuses
27 lines • 1.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseHttpError = void 0;
const { match } = require('@othree.io/cerillo');
const unauthorized_error_1 = require("./unauthorized-error");
const not_found_error_1 = require("./not-found-error");
const duplicate_error_1 = require("./duplicate-error");
const unexpected_error_1 = require("./unexpected-error");
const validation_error_1 = require("./validation-error");
/**
* Parses HTTP error code and optionally a message into a corresponding custom Error instance.
*
* @param {number} httpErrorCode - The HTTP status code.
* @param {string} [message] - Optional error message.
* @returns {Error} - Returns an instance of a custom Error based on the HTTP status code.
*/
const parseHttpError = (httpErrorCode, message) => {
return match(httpErrorCode)
.when(_ => _ === 400).then(_ => new validation_error_1.ValidationError(undefined, message))
.when(_ => _ === 401 || _ === 403).then(_ => new unauthorized_error_1.UnauthorizedError())
.when(_ => _ === 404).then(_ => new not_found_error_1.NotFoundError(undefined, undefined, message))
.when(_ => _ === 409).then(_ => new duplicate_error_1.DuplicateError(undefined, undefined, message))
.default(_ => new unexpected_error_1.UnexpectedError(undefined, message))
.get();
};
exports.parseHttpError = parseHttpError;
//# sourceMappingURL=http-error-parser.js.map