summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-08-31 17:54:57 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-08-31 17:54:57 +0200
commit3def279c5a0f23a2a62097979e96a6c9b78b5db9 (patch)
tree9a438e8046019cbbd3d00f5a4606d3581cb9e216
parent:sparkles: delete _ids from entities (diff)
downloadserver-3def279c5a0f23a2a62097979e96a6c9b78b5db9.tar.xz
:sparkles: typeorm error handler
-rw-r--r--api/src/middlewares/ErrorHandler.ts11
1 files changed, 8 insertions, 3 deletions
diff --git a/api/src/middlewares/ErrorHandler.ts b/api/src/middlewares/ErrorHandler.ts
index 0ed37bb4..e1ab592c 100644
--- a/api/src/middlewares/ErrorHandler.ts
+++ b/api/src/middlewares/ErrorHandler.ts
@@ -1,5 +1,6 @@
 import { NextFunction, Request, Response } from "express";
 import { HTTPError } from "lambert-server";
+import { EntityNotFoundError } from "typeorm";
 import { FieldError } from "../util/instanceOf";
 
 // TODO: update with new body/typorm validation
@@ -13,12 +14,18 @@ export function ErrorHandler(error: Error, req: Request, res: Response, next: Ne
 		let errors = undefined;
 
 		if (error instanceof HTTPError && error.code) code = httpcode = error.code;
-		else if (error instanceof FieldError) {
+		else if (error instanceof EntityNotFoundError) {
+			message = `${(error as any).stringifyTarget} can not be found`;
+			code = 404;
+		} else if (error instanceof FieldError) {
 			code = Number(error.code);
 			message = error.message;
 			errors = error.errors;
 		} else {
+			console.error(`[Error] ${code} ${req.url}`, errors || error, "body:", req.body);
+
 			if (req.server?.options?.production) {
+				// don't expose internal errors to the user, instead human errors should be thrown as HTTPError
 				message = "Internal Server Error";
 			}
 			code = httpcode = 500;
@@ -26,8 +33,6 @@ export function ErrorHandler(error: Error, req: Request, res: Response, next: Ne
 
 		if (httpcode > 511) httpcode = 400;
 
-		console.error(`[Error] ${code} ${req.url}`, errors || error, "body:", req.body);
-
 		res.status(httpcode).json({ code: code, message, errors });
 	} catch (error) {
 		console.error(`[Internal Server Error] 500`, error);