Skip to content

Build your personal knowledge base with Trilium Notes

chore(core): integrate errors

Elian Doranaf8744ef2a02
diff --git a/apps/server/src/errors/forbidden_error.ts b/apps/server/src/errors/forbidden_error.ts
deleted file mode 100644
index 3e62665b07..0000000000
--- a/apps/server/src/errors/forbidden_error.ts
+++ /dev/null
@@ -1,12 +0,0 @@
-import HttpError from "./http_error.js";
-
-class ForbiddenError extends HttpError {
-
- constructor(message: string) {
- super(message, 403);
- this.name = "ForbiddenError";
- }
-
-}
-
-export default ForbiddenError;
\ No newline at end of file
diff --git a/apps/server/src/errors/http_error.ts b/apps/server/src/errors/http_error.ts
deleted file mode 100644
index 2ab806d8bd..0000000000
--- a/apps/server/src/errors/http_error.ts
+++ /dev/null
@@ -1,13 +0,0 @@
-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/apps/server/src/errors/not_found_error.ts b/apps/server/src/errors/not_found_error.ts
deleted file mode 100644
index 44f718a2c8..0000000000
--- a/apps/server/src/errors/not_found_error.ts
+++ /dev/null
@@ -1,12 +0,0 @@
-import HttpError from "./http_error.js";
-
-class NotFoundError extends HttpError {
-
- constructor(message: string) {
- super(message, 404);
- this.name = "NotFoundError";
- }
-
-}
-
-export default NotFoundError;
diff --git a/apps/server/src/errors/open_id_error.ts b/apps/server/src/errors/open_id_error.ts
deleted file mode 100644
index 0206a17f34..0000000000
--- a/apps/server/src/errors/open_id_error.ts
+++ /dev/null
@@ -1,9 +0,0 @@
-class OpenIdError {
- message: string;
-
- constructor(message: string) {
- this.message = message;
- }
-}
-
-export default OpenIdError;
\ No newline at end of file
diff --git a/apps/server/src/errors/validation_error.ts b/apps/server/src/errors/validation_error.ts
deleted file mode 100644
index 25cdd509ef..0000000000
--- a/apps/server/src/errors/validation_error.ts
+++ /dev/null
@@ -1,12 +0,0 @@
-import HttpError from "./http_error.js";
-
-class ValidationError extends HttpError {
-
- constructor(message: string) {
- super(message, 400)
- this.name = "ValidationError";
- }
-
-}
-
-export default ValidationError;
diff --git a/apps/server/src/routes/api/export.ts b/apps/server/src/routes/api/export.ts
index 944eee841c..fc328b444b 100644
--- a/apps/server/src/routes/api/export.ts
+++ b/apps/server/src/routes/api/export.ts
@@ -1,14 +1,12 @@
-"use strict";
+import { NotFoundError, ValidationError } from "@triliumnext/core";
+import type { Request, Response } from "express";
-import zipExportService from "../../services/export/zip.js";
-import singleExportService from "../../services/export/single.js";
-import opmlExportService from "../../services/export/opml.js";
import becca from "../../becca/becca.js";
-import TaskContext from "../../services/task_context.js";
+import opmlExportService from "../../services/export/opml.js";
+import singleExportService from "../../services/export/single.js";
+import zipExportService from "../../services/export/zip.js";
import log from "../../services/log.js";
-import NotFoundError from "../../errors/not_found_error.js";
-import type { Request, Response } from "express";
-import ValidationError from "../../errors/validation_error.js";
+import TaskContext from "../../services/task_context.js";
import { safeExtractMessageAndStackFromError } from "../../services/utils.js";
function exportBranch(req: Request, res: Response) {
diff --git a/apps/server/src/routes/api/tree.ts b/apps/server/src/routes/api/tree.ts
index b9621d5d02..6c1c3c6844 100644
--- a/apps/server/src/routes/api/tree.ts
+++ b/apps/server/src/routes/api/tree.ts
@@ -1,11 +1,10 @@
-"use strict";
+import type { AttributeRow, BranchRow, NoteRow } from "@triliumnext/commons";
+import { NotFoundError } from "@triliumnext/core";
+import type { Request } from "express";
import becca from "../../becca/becca.js";
-import log from "../../services/log.js";
-import NotFoundError from "../../errors/not_found_error.js";
-import type { Request } from "express";
import type BNote from "../../becca/entities/bnote.js";
-import type { AttributeRow, BranchRow, NoteRow } from "@triliumnext/commons";
+import log from "../../services/log.js";
function getNotesAndBranchesAndAttributes(_noteIds: string[] | Set<string>) {
const noteIds = new Set(_noteIds);
diff --git a/apps/server/src/routes/error_handlers.ts b/apps/server/src/routes/error_handlers.ts
index 05b05f6a4a..146d28fbee 100644
--- a/apps/server/src/routes/error_handlers.ts
+++ b/apps/server/src/routes/error_handlers.ts
@@ -1,8 +1,7 @@
+import { ForbiddenError, HttpError, NotFoundError } from "@triliumnext/core";
import type { Application, NextFunction, Request, Response } from "express";
+
import log from "../services/log.js";
-import NotFoundError from "../errors/not_found_error.js";
-import ForbiddenError from "../errors/forbidden_error.js";
-import HttpError from "../errors/http_error.js";
function register(app: Application) {
diff --git a/apps/server/src/routes/route_api.ts b/apps/server/src/routes/route_api.ts
index 6f33260740..d2629e5267 100644
--- a/apps/server/src/routes/route_api.ts
+++ b/apps/server/src/routes/route_api.ts
@@ -1,10 +1,9 @@
+import { NotFoundError, ValidationError } from "@triliumnext/core";
import express, { type RequestHandler } from "express";
import multer from "multer";
import AbstractBeccaEntity from "../becca/entities/abstract_becca_entity.js";
import { namespace } from "../cls_provider.js";
-import NotFoundError from "../errors/not_found_error.js";
-import ValidationError from "../errors/validation_error.js";
import auth from "../services/auth.js";
import cls from "../services/cls.js";
import entityChangesService from "../services/entity_changes.js";
diff --git a/packages/trilium-core/src/becca/becca-interface.ts b/packages/trilium-core/src/becca/becca-interface.ts
index a70a07e1e7..8c8099cd44 100644
--- a/packages/trilium-core/src/becca/becca-interface.ts
+++ b/packages/trilium-core/src/becca/becca-interface.ts
@@ -1,5 +1,5 @@
import NoteSet from "../services/search/note_set.js";
-import NotFoundError from "../errors/not_found_error.js";
+import { NotFoundError } from "../errors.js";
import type BOption from "./entities/boption.js";
import type BNote from "./entities/bnote.js";
import type BEtapiToken from "./entities/betapi_token.js";
diff --git a/packages/trilium-core/src/errors.ts b/packages/trilium-core/src/errors.ts
new file mode 100644
index 0000000000..f2127f280c
--- /dev/null
+++ b/packages/trilium-core/src/errors.ts
@@ -0,0 +1,46 @@
+export class HttpError extends Error {
+
+ statusCode: number;
+
+ constructor(message: string, statusCode: number) {
+ super(message);
+ this.name = "HttpError";
+ this.statusCode = statusCode;
+ }
+
+}
+
+export class NotFoundError extends HttpError {
+
+ constructor(message: string) {
+ super(message, 404);
+ this.name = "NotFoundError";
+ }
+
+}
+
+export class ForbiddenError extends HttpError {
+
+ constructor(message: string) {
+ super(message, 403);
+ this.name = "ForbiddenError";
+ }
+
+}
+
+export class OpenIdError {
+ message: string;
+
+ constructor(message: string) {
+ this.message = message;
+ }
+}
+
+export class ValidationError extends HttpError {
+
+ constructor(message: string) {
+ super(message, 400)
+ this.name = "ValidationError";
+ }
+
+}
diff --git a/packages/trilium-core/src/index.ts b/packages/trilium-core/src/index.ts
index f38fc96929..6e0c44f983 100644
--- a/packages/trilium-core/src/index.ts
+++ b/packages/trilium-core/src/index.ts
@@ -12,6 +12,7 @@ export * as binary_utils from "./services/utils/binary";
export { default as date_utils } from "./services/utils/date";
export { default as events } from "./services/events";
export { getContext, type ExecutionContext } from "./services/context";
+export * from "./errors";
export type { CryptoProvider } from "./services/encryption/crypto";
export function initializeCore({ dbConfig, executionContext, crypto }: {
diff --git a/apps/server/src/services/blob.ts b/packages/trilium-core/src/services/blob.ts
similarity index 97%
rename from apps/server/src/services/blob.ts
rename to packages/trilium-core/src/services/blob.ts
index 2f123cb03f..912ed64164 100644
--- a/apps/server/src/services/blob.ts
+++ b/packages/trilium-core/src/services/blob.ts
@@ -1,7 +1,7 @@
import { binary_utils } from "@triliumnext/core";
import becca from "../becca/becca.js";
-import NotFoundError from "../errors/not_found_error.js";
+import { NotFoundError } from "../errors";
import type { Blob } from "./blob-interface.js";
import protectedSessionService from "./protected_session.js";
import { hash } from "./utils.js";