UNPKG

@fdm-monster/server

Version:

FDM Monster is a bulk OctoPrint, Klipper, PrusaLink and BambuLab manager to set up, configure and monitor 3D printers. Our aim is to provide neat overview over your farm.

113 lines (112 loc) 3.26 kB
import { AuthenticationError, AuthorizationError, BadRequestException, ConflictException, ExternalServiceError, ForbiddenError, InternalServerException, NotFoundException, ValidationException } from "../exceptions/runtime.exceptions.js"; import { AppConstants } from "../server.constants.js"; import { FailedDependencyException } from "../exceptions/failed-dependency.exception.js"; import { EntityNotFoundError } from "typeorm"; //#region src/middleware/exception.filter.ts var ExceptionFilter = class ExceptionFilter { logger; constructor(loggerFactory) { this.logger = loggerFactory(ExceptionFilter.name); } handle(err, _req, res, next) { if (!(process.env.NODE_ENV === AppConstants.defaultTestEnv)) this.logger.error("API Exception occurred", { message: err.message, stack: err.stack || err?.response?.data, url: _req?.url, method: _req?.method, userAgent: _req?.headers?.["user-agent"], errorType: err.constructor.name }); if (err.isAxiosError) { const code = err.response?.status || 500; res.status(code).send({ error: "External API call failed", type: "axios-error", data: err.response?.data?._readableState ? null : err.response?.data }); return; } if (err instanceof AuthenticationError) { res.status(401).send({ error: err.message, reasonCode: err.reasonCode }); return; } if (err instanceof AuthorizationError) { const permissions = err.permissions; const roles = err.roles; const error = err.message || "You lack permission to this resource"; const reason = err.reason; res.status(403).send({ error, reason, permissions, roles }); return; } if (err instanceof ForbiddenError) { res.status(403).send({ error: err.message }); return; } if (err instanceof NotFoundException || err instanceof EntityNotFoundError) { res.status(404).send({ error: err.message }); return; } if (err instanceof BadRequestException) { res.status(400).send({ error: err.message }); return; } if (err instanceof ConflictException) { res.status(409).send({ error: err.message, existingResourceId: err.existingResourceId }); return; } if (err instanceof ValidationException) { res.status(400).send({ error: "API could not accept this input", type: err.name, errors: err.errors }); return; } if (err instanceof FailedDependencyException) { res.status(424).send({ error: err.message, serviceCode: err.serviceCode, type: err.name }); return; } if (err instanceof InternalServerException) { res.status(500).send({ error: err.message, type: err.name, stack: err.stack }); return; } if (err instanceof ExternalServiceError) { res.status(500).send(err.error); return; } if (err) { res.status(500).send({ error: "Server experienced an internal error", type: err.name, stack: err.stack }); return; } next(); } }; function exceptionFilter(_err, _req, _res, _next) { throw new Error("Use ExceptionFilter class instead of exceptionFilter function. Please inject ExceptionFilter and use .handle method."); } //#endregion export { ExceptionFilter, exceptionFilter }; //# sourceMappingURL=exception.filter.js.map