From bed7e42a80de62fb436069d6281f025ed3b227fe Mon Sep 17 00:00:00 2001 From: Pawel Pograniczny Date: Wed, 3 Apr 2024 13:31:00 +0200 Subject: [PATCH] feat: modify errors response according to standard --- shared/errors/app.error.ts | 2 +- shared/errors/http.error.ts | 21 +++++++++++++-------- shared/errors/not-found.error.ts | 5 +++-- shared/middleware/zod-validator.ts | 9 ++++++++- 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/shared/errors/app.error.ts b/shared/errors/app.error.ts index 946bcf0..34f06bb 100644 --- a/shared/errors/app.error.ts +++ b/shared/errors/app.error.ts @@ -1,5 +1,5 @@ export class AppError extends Error { - public constructor(message: string) { + public constructor(message?: string) { super(message); Object.setPrototypeOf(this, AppError.prototype); diff --git a/shared/errors/http.error.ts b/shared/errors/http.error.ts index 8ed0921..4b48da0 100644 --- a/shared/errors/http.error.ts +++ b/shared/errors/http.error.ts @@ -1,10 +1,10 @@ -import { getReasonPhrase } from "http-status-codes"; +import { StatusCodes, getReasonPhrase } from "http-status-codes"; import { AppError } from "./app.error"; export class HttpError extends AppError { public constructor( - message: string, public status: number, + message?: string, parent?: Error | null, ) { super(message); @@ -13,12 +13,17 @@ export class HttpError extends AppError { // eslint-disable-next-line no-nested-ternary const stack = process.env.STAGE === "staging" ? (parent ? parent.stack : this.stack) : undefined; - const error = { - statusCode: status, - error: getReasonPhrase(status), - description, - stack, - }; + let error = {}; + + if (status === StatusCodes.BAD_REQUEST) { + error = { errors: description, stack }; + } else { + error = { + code: `error.${getReasonPhrase(status).toLowerCase().split(" ").join(".")}`, + message: description, + stack, + }; + } this.message = JSON.stringify(error); diff --git a/shared/errors/not-found.error.ts b/shared/errors/not-found.error.ts index fb1cd6b..e81cbcb 100644 --- a/shared/errors/not-found.error.ts +++ b/shared/errors/not-found.error.ts @@ -1,8 +1,9 @@ +import { StatusCodes } from "http-status-codes"; import { HttpError } from "./http.error"; export class NotFoundError extends HttpError { - public constructor(message: string) { - super(message, 404); + public constructor(message?: string) { + super(StatusCodes.NOT_FOUND, message); Object.setPrototypeOf(this, NotFoundError.prototype); } diff --git a/shared/middleware/zod-validator.ts b/shared/middleware/zod-validator.ts index e9efea3..403b5b8 100644 --- a/shared/middleware/zod-validator.ts +++ b/shared/middleware/zod-validator.ts @@ -11,8 +11,15 @@ export const zodValidator = (schema: T): Required { + const key = issue.path.join("."); + const errorType = `validation.${issue.code}`; + validationErrors[key] = validationErrors[key] || []; + validationErrors[key].push(errorType); + }); // @ts-ignore - throw new HttpError(parserResult.error.issues, StatusCodes.BAD_REQUEST); + throw new HttpError(StatusCodes.BAD_REQUEST, validationErrors); } event.body = parserResult.data?.body ?? event.body;