From af8744ef2a02b5c17820892b77e90e0637b5767a Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Tue, 6 Jan 2026 11:31:13 +0200 Subject: [PATCH] chore(core): integrate errors --- apps/server/src/errors/forbidden_error.ts | 12 ----- apps/server/src/errors/http_error.ts | 13 ------ apps/server/src/errors/not_found_error.ts | 12 ----- apps/server/src/errors/open_id_error.ts | 9 ---- apps/server/src/errors/validation_error.ts | 12 ----- apps/server/src/routes/api/export.ts | 18 ++++---- apps/server/src/routes/api/tree.ts | 9 ++-- apps/server/src/routes/error_handlers.ts | 5 +- apps/server/src/routes/route_api.ts | 3 +- .../trilium-core/src/becca/becca-interface.ts | 2 +- packages/trilium-core/src/errors.ts | 46 +++++++++++++++++++ packages/trilium-core/src/index.ts | 1 + .../trilium-core}/src/services/blob.ts | 2 +- 13 files changed, 64 insertions(+), 80 deletions(-) delete mode 100644 apps/server/src/errors/forbidden_error.ts delete mode 100644 apps/server/src/errors/http_error.ts delete mode 100644 apps/server/src/errors/not_found_error.ts delete mode 100644 apps/server/src/errors/open_id_error.ts delete mode 100644 apps/server/src/errors/validation_error.ts create mode 100644 packages/trilium-core/src/errors.ts rename {apps/server => packages/trilium-core}/src/services/blob.ts (97%) diff --git a/apps/server/src/errors/forbidden_error.ts b/apps/server/src/errors/forbidden_error.ts deleted file mode 100644 index 3e62665b0..000000000 --- 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 2ab806d8b..000000000 --- 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 44f718a2c..000000000 --- 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 0206a17f3..000000000 --- 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 25cdd509e..000000000 --- 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 944eee841..fc328b444 100644 --- a/apps/server/src/routes/api/export.ts +++ b/apps/server/src/routes/api/export.ts @@ -1,14 +1,12 @@ -"use strict"; - -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 log from "../../services/log.js"; -import NotFoundError from "../../errors/not_found_error.js"; +import { NotFoundError, ValidationError } from "@triliumnext/core"; import type { Request, Response } from "express"; -import ValidationError from "../../errors/validation_error.js"; + +import becca from "../../becca/becca.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 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 b9621d5d0..6c1c3c684 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) { const noteIds = new Set(_noteIds); diff --git a/apps/server/src/routes/error_handlers.ts b/apps/server/src/routes/error_handlers.ts index 05b05f6a4..146d28fbe 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 6f3326074..d2629e526 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 a70a07e1e..8c8099cd4 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 000000000..f2127f280 --- /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 f38fc9692..6e0c44f98 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 2f123cb03..912ed6416 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";