38 lines
854 B
TypeScript
38 lines
854 B
TypeScript
import { FastifyReply, FastifyRequest } from "fastify";
|
|
import mongoose from "mongoose";
|
|
|
|
export function errorHandler(
|
|
error: any,
|
|
req: FastifyRequest,
|
|
res: FastifyReply
|
|
) {
|
|
if (process.env.DEV) {
|
|
console.dir(error, { depth: null });
|
|
}
|
|
|
|
if (error.validation) {
|
|
const errMsg = {
|
|
type: "validation_error",
|
|
path: error.validation[0].instancePath,
|
|
context: error.validationContext,
|
|
msg: error.validation[0].message,
|
|
params: error.validation[0].params,
|
|
};
|
|
|
|
return res.code(400).send(errMsg);
|
|
}
|
|
|
|
if (error instanceof mongoose.mongo.MongoServerError) {
|
|
if (error.code === 11000) {
|
|
return res.code(400).send({
|
|
type: "duplicate_key",
|
|
context: "body",
|
|
msg: "value already exists",
|
|
params: error.keyValue,
|
|
});
|
|
}
|
|
}
|
|
|
|
return res.code(500).send();
|
|
}
|