UNPKG

prisma-error-formatter

Version:

A flexible and customizable Prisma error formatter to simplify and unify error handling in Prisma Client applications.

100 lines (99 loc) 3.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseHttpException = parseHttpException; exports.isNestJsHttpException = isNestJsHttpException; exports.getExceptionType = getExceptionType; const common_1 = require("@nestjs/common"); function parseHttpException(exception) { const response = exception.getResponse(); const status = exception.getStatus(); const message = exception.message; if (typeof response === "string") { return [ { path: getPathForStatus(status), message: response || message, }, ]; } if (typeof response === "object" && response !== null) { const res = response; if (Array.isArray(res.message)) { return res.message.map((msg) => { if (typeof msg === "string") { return { path: res.path || getPathForStatus(status), message: msg, }; } if (typeof msg === "object" && msg !== null) { const msgObj = msg; return { path: msgObj.path || msgObj.property || getPathForStatus(status), message: msgObj.message || JSON.stringify(msg), }; } return { path: getPathForStatus(status), message: String(msg), }; }); } if (res.message && typeof res.message === "string") { return [ { path: res.path || getPathForStatus(status), message: res.message, }, ]; } if (res.error && typeof res.error === "string") { return [ { path: getPathForStatus(status), message: res.error, }, ]; } } return [ { path: getPathForStatus(status), message: message || "HTTP error occurred", }, ]; } function getPathForStatus(status) { switch (status) { case 400: return "request"; case 401: return "authentication"; case 403: return "authorization"; case 404: return "resource"; case 409: return "conflict"; default: return "request"; } } function isNestJsHttpException(exception) { return exception instanceof common_1.HttpException; } function getExceptionType(exception) { if (exception instanceof common_1.BadRequestException) return "BadRequestException"; if (exception instanceof common_1.UnauthorizedException) return "UnauthorizedException"; if (exception instanceof common_1.ForbiddenException) return "ForbiddenException"; if (exception instanceof common_1.NotFoundException) return "NotFoundException"; if (exception instanceof common_1.ConflictException) return "ConflictException"; if (exception instanceof common_1.HttpException) return "HttpException"; return "Unknown"; }