All files / src/common/exceptions appExceptions.js

100% Statements 20/20
77.27% Branches 17/22
100% Functions 4/4
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51  4x                     2x 2x     2x 2x 2x   2x         10x 10x 10x 10x     10x 10x 10x 10x 10x 10x       10x       10x 10x          
// see: https://datatracker.ietf.org/doc/html/rfc7231#section-6
const httpReasons = {
    400: "BadRequest",
    401: "Unauthorized",
    403: "Forbidden",
    404: "NotFound",
    405: "MethodNotAllowed",
    418: "ImATeapot", // Important!
    500: "InternalServerError",
};
 
class ModelException extends Error {
    layer = "Model";
    isOperational = true;
 
    constructor({ isOperational, name, message } = {}) {
        name = name ?? "Model Exception";
        message = message ?? "";
        super(name, {cause: message});
 
        this.isOperational = isOperational ?? this.isOperational;
    }
}
 
class HttpException extends Error {
    layer = "Controller";
    isOperational = true;
    httpMessage = ""; // For communicating custom messages to the client
    httpReason = ""; // Reason for the statusCode
 
    constructor(status, httpMessage, { isOperational, name, message } = {}) {
        name = name ?? "Http Exception";
        message = message ?? "";
        super(name, {cause: message});
        this.httpMessage = httpMessage ?? this.httpMessage;
        this.isOperational = isOperational ?? this.isOperational;
        this.statusCode = typeof status === "number" && typeof httpReasons[status] == "string" ? status : 500;
    }
 
    get statusCode() {
        return this._statusCode;
    }
 
    set statusCode(status) {
        this._statusCode = status;
        this.httpReason = httpReasons[status] ?? this.httpReason;
    }
}
 
export { ModelException, HttpException };