Skip to content

Build your personal knowledge base with Trilium Notes

fix(desktop): reject the express-app promise when server startup fails

Elian Doran936233ae64b2
diff --git a/apps/desktop/src/main.spec.ts b/apps/desktop/src/main.spec.ts
index 948497d26c..ce35aca079 100644
--- a/apps/desktop/src/main.spec.ts
+++ b/apps/desktop/src/main.spec.ts
@@ -459,6 +459,26 @@ describe("app event handlers", () => {
expect(h.createMainWindow).not.toHaveBeenCalled();
});
+ it("rejects the express-app promise but still creates the window when the server fails to start", async () => {
+ h.isDbInitialized = true;
+ h.startServer = () => Promise.reject(new Error("server exploded"));
+ const { main } = await importMain();
+ const mainPromise = main();
+
+ const ready = h.appOn.get("ready");
+ expect(ready).toBeDefined();
+ await ready?.();
+ expect(h.createMainWindow).toHaveBeenCalledTimes(1);
+
+ // main() itself fails, and the promise handed to the protocol
+ // handler rejects so pending renderer requests fail instead of hanging.
+ // (lastCall: the protocol mock's call history survives vi.resetModules.)
+ await expect(mainPromise).rejects.toThrow("server exploded");
+ const { setupTriliumAppProtocol } = await import("./protocol.js");
+ const appPromise = vi.mocked(setupTriliumAppProtocol).mock.lastCall?.[0];
+ await expect(appPromise).rejects.toThrow("server exploded");
+ });
+
it("creates the main window while the server is still starting", async () => {
h.isDbInitialized = true;
// The server never finishes starting; the window is gated on core
diff --git a/apps/desktop/src/main.ts b/apps/desktop/src/main.ts
index fcb5a5ba1a..7999fa7f2b 100644
--- a/apps/desktop/src/main.ts
+++ b/apps/desktop/src/main.ts
@@ -222,11 +222,21 @@ export async function main() {
markStartupMetric("core-initialized");
coreInitializedPromise.resolve();
- const startTriliumServer = (await import("@triliumnext/server/src/www.js")).default;
- const expressApp = await startTriliumServer();
- markStartupMetric("server-started");
-
- expressAppPromise.resolve(expressApp);
+ try {
+ const startTriliumServer = (await import("@triliumnext/server/src/www.js")).default;
+ const expressApp = await startTriliumServer();
+ markStartupMetric("server-started");
+
+ expressAppPromise.resolve(expressApp);
+ } catch (err) {
+ // The window may already be up and loading trilium-app:// — fail its
+ // requests with a 500 instead of leaving them awaiting a server that
+ // will never come up. The no-op catch marks the deferred itself as
+ // handled (each protocol request awaits it separately).
+ expressAppPromise.reject(err instanceof Error ? err : new Error(String(err)));
+ expressAppPromise.catch(() => {});
+ throw err;
+ }
}
/**
diff --git a/apps/desktop/src/protocol.spec.ts b/apps/desktop/src/protocol.spec.ts
index d1af4f0a45..8b489ac804 100644
--- a/apps/desktop/src/protocol.spec.ts
+++ b/apps/desktop/src/protocol.spec.ts
@@ -254,6 +254,23 @@ describe("trilium-app protocol dispatcher", () => {
expect(await response.json()).toEqual({ echo: { hello: "world" } });
});
+ it("returns a 500 when the promised Express app rejects (server failed to start)", async () => {
+ electronMock.handle.mockReset();
+ let rejectApp: (err: Error) => void = () => {};
+ setupTriliumAppProtocol(new Promise((_res, rej) => { rejectApp = rej; }));
+ await Promise.resolve(); // let whenReady().then(...) run
+
+ const handler = electronMock.handle.mock.calls[0][1] as (req: Request) => Promise<Response>;
+ const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
+ const responsePromise = handler(new Request("trilium-app://app/anything"));
+ rejectApp(new Error("server never started"));
+ const response = await responsePromise;
+ errorSpy.mockRestore();
+
+ expect(response.status).toBe(500);
+ expect(await response.text()).toBe("Internal Server Error");
+ });
+
it("rejects when Express forwards an unhandled error to next()", async () => {
const app = express();
app.get("/boom", (_req, _res, next) => next(new Error("downstream failure")));