Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
chore(share): use safeExtractMessageAndStackFromError to get rid of "any" in try/catch blocks
Panagiotis Papadopoulosbdc829198cc8
diff --git a/src/share/routes.spec.ts b/src/share/routes.spec.tsindex 86e1927bdd..5072bb2970 100644--- a/src/share/routes.spec.ts+++ b/src/share/routes.spec.ts@@ -2,6 +2,7 @@ import { beforeAll, beforeEach, describe, expect, it } from "vitest";import supertest from "supertest";import { initializeTranslations } from "../services/i18n.js";import type { Application, Request, Response, NextFunction } from "express";+import { safeExtractMessageAndStackFromError } from "../services/utils.js";let app: Application;@@ -11,8 +12,9 @@ describe("Share API test", () => {beforeAll(async () => {initializeTranslations();app = (await import("../app.js")).default;- app.use((err: any, req: Request, res: Response, next: NextFunction) => {- if (err.message.includes("Cannot set headers after they are sent to the client")) {+ app.use((err: unknown, req: Request, res: Response, next: NextFunction) => {+ const [errMessage] = safeExtractMessageAndStackFromError(err)+ if (errMessage.includes("Cannot set headers after they are sent to the client")) {cannotSetHeadersCount++;}diff --git a/src/share/routes.ts b/src/share/routes.tsindex 3c6db87d54..db7e62f40b 100644--- a/src/share/routes.ts+++ b/src/share/routes.ts@@ -15,7 +15,7 @@ import log from "../services/log.js";import type SNote from "./shaca/entities/snote.js";import type SBranch from "./shaca/entities/sbranch.js";import type SAttachment from "./shaca/entities/sattachment.js";-import utils from "../services/utils.js";+import utils, { safeExtractMessageAndStackFromError } from "../services/utils.js";import options from "../services/options.js";function getSharedSubTreeRoot(note: SNote): { note?: SNote; branch?: SBranch } {@@ -183,8 +183,9 @@ function register(router: Router) {res.send(ejsResult);useDefaultView = false; // Rendering went okay, don't use default view}- } catch (e: any) {- log.error(`Rendering user provided share template (${templateId}) threw exception ${e.message} with stacktrace: ${e.stack}`);+ } catch (e: unknown) {+ const [errMessage, errStack] = safeExtractMessageAndStackFromError(e);+ log.error(`Rendering user provided share template (${templateId}) threw exception ${errMessage} with stacktrace: ${errStack}`);}}}