Initial Commit
This commit is contained in:
36
src/utils/errors.ts
Normal file
36
src/utils/errors.ts
Normal 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();
|
||||
}
|
||||
9
src/utils/id.ts
Normal file
9
src/utils/id.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { init } from "@paralleldrive/cuid2";
|
||||
|
||||
const id = init({
|
||||
length: 15,
|
||||
});
|
||||
|
||||
export function generateId(perfix?: string) {
|
||||
return perfix ? perfix + id() : id();
|
||||
}
|
||||
Reference in New Issue
Block a user