Skip to content

Build your personal knowledge base with Trilium Notes

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

Panagiotis Papadopoulos2c91f6e7bc20
diff --git a/src/errors/http_error.ts b/src/errors/http_error.ts
new file mode 100644
index 0000000000..2ab806d8bd
--- /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 ae97a6ac57..44f718a2c8 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 35eb5897de..25cdd509ef 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;
}
}