Skip to content

Build your personal knowledge base with Trilium Notes

chore: address requested changes

Elian Doran1465c20e9df1
diff --git a/apps/server/src/etapi/notes.ts b/apps/server/src/etapi/notes.ts
index cd0db083a9..303e293b4a 100644
--- a/apps/server/src/etapi/notes.ts
+++ b/apps/server/src/etapi/notes.ts
@@ -173,12 +173,12 @@ function register(router: Router) {
const note = eu.getAndCheckNote(req.params.noteId);
const taskContext = new TaskContext("no-progress-reporting", "importNotes", null);
- void zipImportService.importZip(taskContext, req.body, note).then((importedNote) => {
+ zipImportService.importZip(taskContext, req.body, note).then((importedNote) => {
res.status(201).json({
note: mappers.mapNoteToPojo(importedNote),
branch: mappers.mapBranchToPojo(importedNote.getParentBranches()[0])
});
- }); // we need better error handling here, async errors won't be properly processed.
+ }).catch(next); // forward async import errors to the ETAPI error handler
});
eu.route<{ noteId: string }>(router, "post", "/etapi/notes/:noteId/revision", (req, res, next) => {
diff --git a/apps/server/src/main.ts b/apps/server/src/main.ts
index e203789efb..ae405abbac 100644
--- a/apps/server/src/main.ts
+++ b/apps/server/src/main.ts
@@ -96,4 +96,7 @@ async function startApplication() {
}
}
-void startApplication();
+startApplication().catch((err) => {
+ console.error("Fatal error during Trilium server startup:", err);
+ process.exit(1);
+});
diff --git a/apps/server/src/services/ws_messaging_provider.ts b/apps/server/src/services/ws_messaging_provider.ts
index e44d44af27..28f2bb74ef 100644
--- a/apps/server/src/services/ws_messaging_provider.ts
+++ b/apps/server/src/services/ws_messaging_provider.ts
@@ -50,10 +50,16 @@ export default class WebSocketMessagingProvider implements MessagingProvider {
ws.on("message", (messageJson) => {
void (async () => {
- const message = JSON.parse(messageJson as any);
-
- if (this.clientMessageHandler) {
- await this.clientMessageHandler(id, message);
+ try {
+ const message = JSON.parse(messageJson as any);
+
+ if (this.clientMessageHandler) {
+ await this.clientMessageHandler(id, message);
+ }
+ } catch (e) {
+ // A malformed message (invalid JSON) or a failing handler must not
+ // crash the process via an unhandled rejection on this floating promise.
+ console.error("Failed to process websocket message:", e);
}
})();
});
diff --git a/packages/trilium-core/src/routes/api/recent_changes.spec.ts b/packages/trilium-core/src/routes/api/recent_changes.spec.ts
index 1c9457b85f..dbe6d58192 100644
--- a/packages/trilium-core/src/routes/api/recent_changes.spec.ts
+++ b/packages/trilium-core/src/routes/api/recent_changes.spec.ts
@@ -36,6 +36,8 @@ describe("Recent changes API (core)", () => {
const res = await api.get<RecentChangeRow[]>("/api/recent-changes/root");
expect(res.status).toBe(200);
+ // Guard against a vacuous pass: the demo fixture has many recent changes.
+ expect(res.body.length).toBeGreaterThan(1);
for (let i = 1; i < res.body.length; i++) {
expect(res.body[i - 1].utcDate >= res.body[i].utcDate).toBe(true);
}
diff --git a/packages/trilium-core/src/routes/api/similar_notes.spec.ts b/packages/trilium-core/src/routes/api/similar_notes.spec.ts
index bf3c7ea6b9..f70837bbf8 100644
--- a/packages/trilium-core/src/routes/api/similar_notes.spec.ts
+++ b/packages/trilium-core/src/routes/api/similar_notes.spec.ts
@@ -33,7 +33,7 @@ describe("Similar Notes API (core)", () => {
it("finds a freshly created note that shares content with another", async () => {
const shared = "alphabet bicycle continuous determination elephant fountain";
- await createTextNote(api, {
+ const first = await createTextNote(api, {
title: "Similar source",
content: `<p>${shared}</p>`
});
@@ -45,6 +45,8 @@ describe("Similar Notes API (core)", () => {
const res = await api.get<SimilarNote[]>(`/api/similar-notes/${noteId}`);
expect(res.status).toBe(200);
expect(Array.isArray(res.body)).toBe(true);
+ // The sibling sharing the same content is actually found (not a vacuous pass).
+ expect(res.body.some((similar) => similar.noteId === first.noteId)).toBe(true);
// The note never lists itself as similar.
expect(res.body.every((similar) => similar.noteId !== noteId)).toBe(true);
});
diff --git a/packages/trilium-core/src/test/api_fixtures.ts b/packages/trilium-core/src/test/api_fixtures.ts
index 593f9d244a..370a348785 100644
--- a/packages/trilium-core/src/test/api_fixtures.ts
+++ b/packages/trilium-core/src/test/api_fixtures.ts
@@ -25,6 +25,11 @@ export async function createTextNote(
{ body: { title, type: "text", content } }
);
+ if (res.status !== 200) {
+ // Surface the real failure instead of a cryptic "cannot read 'noteId' of undefined".
+ throw new Error(`createTextNote failed: expected 200, got ${res.status} – ${JSON.stringify(res.body)}`);
+ }
+
return {
noteId: res.body.note.noteId,
branchId: res.body.branch.branchId
diff --git a/packages/trilium-core/src/test/api_tester.ts b/packages/trilium-core/src/test/api_tester.ts
index 7ffc4e1438..dc99a65f90 100644
--- a/packages/trilium-core/src/test/api_tester.ts
+++ b/packages/trilium-core/src/test/api_tester.ts
@@ -157,6 +157,7 @@ class MockResponse extends Writable {
setHeader(name: string, value: string) { this.headers[name] = value; return this; }
removeHeader(name: string) { delete this.headers[name]; return this; }
send(body: unknown) { this.used = true; this.hasSendBody = true; this.sendBody = body; return this; }
+ json(body: unknown) { return this.send(body); }
sendStatus(code: number) { this.used = true; this.statusCode = code; return this; }
override _write(chunk: Buffer | Uint8Array | string, _encoding: BufferEncoding, callback: (error?: Error | null) => void) {
@@ -230,9 +231,13 @@ export class CoreApiTester {
return mockRes.snapshot();
}
if (resultHandler) {
- const capture: CaptureResponse = { setHeader() {} };
+ const captureHeaders: Record<string, string> = {};
+ const capture: CaptureResponse = {
+ setHeader(name, value) { captureHeaders[name] = value; }
+ };
resultHandler(req, capture, result);
- return capture.captured ?? formatApiResult(result);
+ const base = capture.captured ?? formatApiResult(result);
+ return { ...base, headers: { ...captureHeaders, ...base.headers } };
}
return formatApiResult(result);
});