Created list of all possible api errors and made them throwable in routes code

This commit is contained in:
AlTech98
2021-08-31 17:56:38 +02:00
parent fb439c5717
commit 7baa2fbe52
3 changed files with 256 additions and 66 deletions
+6
View File
@@ -1,6 +1,7 @@
import { NextFunction, Request, Response } from "express";
import { HTTPError } from "lambert-server";
import { FieldError } from "../util/instanceOf";
import {ApiError} from "../util/ApiError";
// TODO: update with new body/typorm validation
export function ErrorHandler(error: Error, req: Request, res: Response, next: NextFunction) {
@@ -13,6 +14,11 @@ 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 ApiError) {
code = error.code;
message = error.message;
httpcode = error.httpStatus;
}
else if (error instanceof FieldError) {
code = Number(error.code);
message = error.message;