mirror of
https://github.com/detleph/server.git
synced 2026-09-04 16:46:04 +02:00
Add basic error handling
+ Add express-async-errors package to handle async errors + Add ForwaradableError class (Base class for all project-specific errors) + Add defaultErrorHandler middleware which handles errors and sends info to the client
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
import { PrismaClientUnknownRequestError } from "@prisma/client/runtime";
|
||||
import { NextFunction, Request, Response } from "express";
|
||||
import ForwardableError from "./ForwardableError";
|
||||
|
||||
const env = process.env.NODE_ENV || "production";
|
||||
|
||||
export default function defaultErrorHandler(err: any, req: Request, res: Response, next: NextFunction) {
|
||||
if (ForwardableError.isForwardableError(err)) {
|
||||
return res.status(err.status).json({
|
||||
type: "error",
|
||||
payload: {
|
||||
message: err.message,
|
||||
...(env === "development"
|
||||
? {
|
||||
stack: err.stack,
|
||||
}
|
||||
: {}),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (err instanceof PrismaClientUnknownRequestError) {
|
||||
return res.status(404).json({
|
||||
type: "error",
|
||||
payload: {
|
||||
message: "An unknown error occured. This could be due to malformed IDs",
|
||||
...(env === "development"
|
||||
? {
|
||||
prisma: err.message,
|
||||
stack: err.stack,
|
||||
}
|
||||
: {}),
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user