refactor(errors): add HttpError class and extend existing errors from it

This commit is contained in:
Panagiotis Papadopoulos 2025-03-07 22:47:03 +01:00
parent 39d45dc11b
commit 2c91f6e7bc
3 changed files with 24 additions and 8 deletions

13
src/errors/http_error.ts Normal file
View File

@ -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;

View File

@ -1,10 +1,12 @@
class NotFoundError extends Error { import HttpError from "./http_error.js";
statusCode: number;
class NotFoundError extends HttpError {
constructor(message: string) { constructor(message: string) {
super(message); super(message, 404);
this.name = "NotFoundError"; this.name = "NotFoundError";
this.statusCode = 404;
} }
} }
export default NotFoundError; export default NotFoundError;

View File

@ -1,9 +1,10 @@
class ValidationError extends Error { import HttpError from "./http_error.js";
statusCode: number;
class ValidationError extends HttpError {
constructor(message: string) { constructor(message: string) {
super(message) super(message, 400)
this.name = "ValidationError"; this.name = "ValidationError";
this.statusCode = 400;
} }
} }