@fdm-monster/server
Version:
FDM Monster is a bulk OctoPrint manager to set up, configure and monitor 3D printers. Our aim is to provide extremely optimized websocket performance and reliability.
106 lines (105 loc) • 3.25 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "exceptionFilter", {
enumerable: true,
get: function() {
return exceptionFilter;
}
});
const _runtimeexceptions = require("../exceptions/runtime.exceptions");
const _serverconstants = require("../server.constants");
const _typeorm = require("typeorm");
const _faileddependencyexception = require("../exceptions/failed-dependency.exception");
function exceptionFilter(err, _req, res, next) {
const isTest = process.env.NODE_ENV === _serverconstants.AppConstants.defaultTestEnv;
if (!isTest) {
console.error("[API Exception Handler]", err.stack || err?.response?.data);
}
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 _runtimeexceptions.AuthenticationError) {
res.status(401).send({
error: err.message,
reasonCode: err.reasonCode
});
return;
}
if (err instanceof _runtimeexceptions.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 _runtimeexceptions.ForbiddenError) {
res.status(403).send({
error: err.message
});
return;
}
if (err instanceof _runtimeexceptions.NotFoundException || err instanceof _typeorm.EntityNotFoundError) {
res.status(404).send({
error: err.message
});
return;
}
if (err instanceof _runtimeexceptions.BadRequestException) {
res.status(400).send({
error: err.message
});
return;
}
if (err instanceof _runtimeexceptions.ValidationException) {
res.status(400).send({
error: "API could not accept this input",
type: err.name,
errors: err.errors
});
return;
}
if (err instanceof _faileddependencyexception.FailedDependencyException) {
res.status(424).send({
error: err.message,
serviceCode: err.serviceCode,
type: err.name
});
return;
}
if (err instanceof _runtimeexceptions.InternalServerException) {
res.status(500).send({
error: err.message,
type: err.name,
stack: err.stack
});
return;
}
if (err instanceof _runtimeexceptions.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();
}
//# sourceMappingURL=exception.filter.js.map