-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy patherror.js
More file actions
44 lines (36 loc) · 1.17 KB
/
Copy patherror.js
File metadata and controls
44 lines (36 loc) · 1.17 KB
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
38
39
40
41
42
43
44
// class ErrorHandler extends Error{
// constructor(message, statusCode){
// super(message);
// this.statusCode =statusCode;
// }
// }
// export const errorMiddleware = (err, req, res, next) => {
// err.messsage = err.message || "Internal Server Error!";
// err.statusCode = err.statusCode || 500;
// return res.status(err.statusCode).json({
// success: false,
// message: err.message,
// });
// };
// export default ErrorHandler;
class ErrorHandler extends Error {
constructor(message, statusCode) {
super(message);
this.statusCode = statusCode;
// Capture the stack trace for debugging
Error.captureStackTrace(this, this.constructor);
}
}
export const errorMiddleware = (err, req, res, next) => {
// Ensure that error properties exist
err.message = err.message || "Internal Server Error!";
err.statusCode = err.statusCode || 500;
// Log the error for debugging (optional)
console.error("Error:", err);
// Return the error response
return res.status(err.statusCode).json({
success: false,
message: err.message,
});
};
export default ErrorHandler;