import { ZodError } from "zod";

export default async (err, req, res, next) => {
    const error = {
        httpStatusCode: err?.status || 500,
        message: err.message,
    };

    if (err instanceof ZodError) {
        [error.httpStatusCode, error.message] = [400, err.errors[0].message];
    }

    if (err.name === "SequelizeUniqueConstraintError") {
        [error.httpStatusCode, error.message] = [409, err.errors[0].message];
    }

    res.status(error?.httpStatusCode || 500).json({
        success: false,
        message: error.message,
    });

    next();
};
