From 2c91f6e7bc207a1b83a4291c60e54f4a177b3fb2 Mon Sep 17 00:00:00 2001 From: Panagiotis Papadopoulos Date: Fri, 7 Mar 2025 22:47:03 +0100 Subject: [PATCH] refactor(errors): add HttpError class and extend existing errors from it --- src/errors/http_error.ts | 13 +++++++++++++ src/errors/not_found_error.ts | 10 ++++++---- src/errors/validation_error.ts | 9 +++++---- 3 files changed, 24 insertions(+), 8 deletions(-) create mode 100644 src/errors/http_error.ts diff --git a/src/errors/http_error.ts b/src/errors/http_error.ts new file mode 100644 index 000000000..2ab806d8b --- /dev/null +++ b/src/errors/http_error.ts @@ -0,0 +1,13 @@ +class HttpError extends Error { + + statusCode: number; + + constructor(message: string, statusCode: number) { + super(message); + this.name = "HttpError"; + this.statusCode = statusCode; + } + +} + +export default HttpError; \ No newline at end of file diff --git a/src/errors/not_found_error.ts b/src/errors/not_found_error.ts index ae97a6ac5..44f718a2c 100644 --- a/src/errors/not_found_error.ts +++ b/src/errors/not_found_error.ts @@ -1,10 +1,12 @@ -class NotFoundError extends Error { - statusCode: number; +import HttpError from "./http_error.js"; + +class NotFoundError extends HttpError { + constructor(message: string) { - super(message); + super(message, 404); this.name = "NotFoundError"; - this.statusCode = 404; } + } export default NotFoundError; diff --git a/src/errors/validation_error.ts b/src/errors/validation_error.ts index 35eb5897d..25cdd509e 100644 --- a/src/errors/validation_error.ts +++ b/src/errors/validation_error.ts @@ -1,9 +1,10 @@ -class ValidationError extends Error { - statusCode: number; +import HttpError from "./http_error.js"; + +class ValidationError extends HttpError { + constructor(message: string) { - super(message) + super(message, 400) this.name = "ValidationError"; - this.statusCode = 400; } }