Skip to content

Commit

Permalink
Issue #35: added globalErrorHandling
Browse files Browse the repository at this point in the history
  • Loading branch information
Jainil2004 committed Jan 28, 2024
1 parent fc3290b commit 72e1b8a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const cors = require("cors");
const dotenv = require("dotenv");
const dbConnect = require("./config/connect.js");
const Routes = require("./routes/index.js");
const globalErrorHandlingMiddleware = require("./middlewares/globalErrorHandlingMiddleware.js");

const app = express();

Expand All @@ -17,6 +18,10 @@ dotenv.config();
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
/**
* Global error handling middleware (issue #35)
*/
app.use(globalErrorHandlingMiddleware);

/**
* Routes
Expand Down
14 changes: 14 additions & 0 deletions server/middlewares/globalErrorHandlingMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// related issue: #35

const globalErrorHandlingMiddleware = (err, req, res, next) => {
console.error(err.stack);

// send the error response
res.status(500).json({
error: 'Internal Server Error',
message: 'internal Server issue! Connection unsuccessful!',
});
};

module.exports = globalErrorHandlingMiddleware;

0 comments on commit 72e1b8a

Please sign in to comment.