0f7af70b90
Updated Docker configuration, refactored middleware for improved error handling, and restructured account, order, plan, profile, and support modules, including their routes, services, and validations. Enhanced email processing queues and utilities for token generation, pagination, and response management to streamline the application architecture and enhance maintainability.
48 lines
1.3 KiB
JavaScript
48 lines
1.3 KiB
JavaScript
import { ZodError } from "zod";
|
|
import { configs } from "../configs/index.js";
|
|
import handleZodError from "../errors/zodError.js";
|
|
import { AppError } from "../utils/app_error.js";
|
|
const globalErrorHandler = (err, req, res, next) => {
|
|
let statusCode = 500;
|
|
let message = "Something went wrong!";
|
|
let errorSources = [
|
|
{
|
|
path: "",
|
|
message: "Something went wrong",
|
|
},
|
|
];
|
|
if (err instanceof ZodError) {
|
|
const simplifiedError = handleZodError(err);
|
|
statusCode = simplifiedError?.statusCode;
|
|
message = simplifiedError?.message;
|
|
errorSources = simplifiedError?.errorSources;
|
|
}
|
|
else if (err instanceof AppError) {
|
|
statusCode = err?.statusCode;
|
|
message = err.message;
|
|
errorSources = [
|
|
{
|
|
path: "",
|
|
message: err?.message,
|
|
},
|
|
];
|
|
}
|
|
else if (err instanceof Error) {
|
|
message = err.message;
|
|
errorSources = [
|
|
{
|
|
path: "",
|
|
message: err?.message,
|
|
},
|
|
];
|
|
}
|
|
res.status(statusCode).json({
|
|
success: false,
|
|
message,
|
|
errorSources,
|
|
err,
|
|
stack: configs.env === "development" ? err?.stack : null,
|
|
});
|
|
};
|
|
export default globalErrorHandler;
|