Skip to content

Commit

Permalink
feat: modify errors response according to standard
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelTshDev committed Apr 3, 2024
1 parent 8268540 commit bed7e42
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
2 changes: 1 addition & 1 deletion shared/errors/app.error.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export class AppError extends Error {
public constructor(message: string) {
public constructor(message?: string) {
super(message);

Object.setPrototypeOf(this, AppError.prototype);
Expand Down
21 changes: 13 additions & 8 deletions shared/errors/http.error.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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);

Expand Down
5 changes: 3 additions & 2 deletions shared/errors/not-found.error.ts
Original file line number Diff line number Diff line change
@@ -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);
}
Expand Down
9 changes: 8 additions & 1 deletion shared/middleware/zod-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@ export const zodValidator = <T extends ZodTypeAny>(schema: T): Required<Pick<Mid
const parserResult = schema.safeParse(event);

if (!parserResult.success) {
const validationErrors: { [key: string]: string[] } = {};
parserResult.error.issues.forEach((issue) => {
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;
Expand Down

0 comments on commit bed7e42

Please sign in to comment.