Initial Commit

This commit is contained in:
2024-12-19 13:54:15 +05:30
commit 970a972b11
17 changed files with 1487 additions and 0 deletions

36
src/utils/errors.ts Normal file
View File

@@ -0,0 +1,36 @@
import { FastifyError, 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",
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();
}