-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day29.js
37 lines (30 loc) · 903 Bytes
/
Day29.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Import necessary modules
const express = require('express');
const app = express();
function errorHandler(err, req, res, next) {
console.error(err.stack);
if (err instanceof CustomError) {
// Handle custom errors with specific responses
return res.status(err.statusCode).json({ error: err.message });
} else {
return res.status(500).json({ error: 'Internal Server Error' });
}
}
app.use(errorHandler);
const port = 3000;
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
class CustomError extends Error {
constructor(message, statusCode) {
super(message);
this.name = this.constructor.name;
this.statusCode = statusCode || 500;
Error.captureStackTrace(this, this.constructor);
}
}
app.get('/example', (req, res, next) => {
// Simulate an error
const err = new CustomError('Custom Error Message', 400);
next(err);
});