diff --git a/backend/server.js b/backend/server.js index ded15b90ca54c2222c30a077a5b5f21dce3cfbb4..192d054cff1d243624dd5983cd5c7d7853c57b8c 100644 --- a/backend/server.js +++ b/backend/server.js @@ -2,10 +2,10 @@ const express = require("express"); const logger = require("morgan"); const cors = require("cors"); const mongoose = require("mongoose"); -const jsonErrorHandler = require("./services/jsonErrorHandler"); const indexRouter = require("./routes/index"); const usersRouter = require("./routes/users"); const routeNotFoundJsonHandler = require("./services/routeNotFoundJsonHandler"); +const jsonErrorHandler = require("./services/jsonErrorHandler"); mongoose.connect(process.env.MONGO_DB_URL, { useNewUrlParser: true, diff --git a/backend/services/jsonErrorHandler.js b/backend/services/jsonErrorHandler.js index 0a379a251d431f68ff786361424090dda9f7c6d1..0a69a05c1887978f2e157a72675d2db1aaa42072 100644 --- a/backend/services/jsonErrorHandler.js +++ b/backend/services/jsonErrorHandler.js @@ -1,22 +1,16 @@ // Custom error handler to send unhandled errors in JSON instead of HTML. // The error-handling middleware is a special type of Express middleware // that needs to have four arguments as opposed to a regular middleware. -// eslint-disable-next-line no-unused-vars const jsonErrorHandler = function (error, req, res, next) { - console.error(error); - if (res.headersSent) return next(error); - - if (process.env.NODE_ENV === "development") { - return res.status(500).json({ - message: error.toString(), - stackTrace: error.stack, - }); - } else { - // Hide error details in production to avoid security issues - return res.status(500).json({ - message: "Internal server error", - }); + if (!res.headersSent) { + if (process.env.NODE_ENV === "development") { + res.status(500).json({ message: error.toString() }); + } else { + // Hide error details in production to avoid security issues + res.status(500).json({ message: "Internal server error" }); + } } + next(error); }; module.exports = jsonErrorHandler;