diff --git a/apps/api/src/desktop/updates.ts b/apps/api/src/desktop/updates.ts index 88adeb6..15833e6 100644 --- a/apps/api/src/desktop/updates.ts +++ b/apps/api/src/desktop/updates.ts @@ -165,7 +165,13 @@ export async function registerDesktopRoutes(app: FastifyInstance, deps: { releas app.get("/v1/desktop/download/:releaseId/:name", async (req, reply) => { const { releaseId, name } = z.object({ releaseId: z.coerce.number().int().positive(), name: z.string().min(1).max(200) }).parse(req.params); - const file = releases ? await releases.file(releaseId, name) : null; + let file: { body: Readable; size: string | null } | null; + try { + file = releases ? await releases.file(releaseId, name) : null; + } catch (err) { + req.log.warn({ err }, "desktop download failed"); + return reply.code(503).send({ error: "download_unavailable" }); + } if (!file) return reply.code(404).send({ error: "not_found" }); if (file.size) reply.header("content-length", file.size); return reply.header("content-type", "application/octet-stream").header("content-disposition", `attachment; filename="${name.replace(/[^\w.-]+/g, "_")}"`).send(file.body); diff --git a/apps/api/test/desktop-updates.test.ts b/apps/api/test/desktop-updates.test.ts index a44bd54..8c401be 100644 --- a/apps/api/test/desktop-updates.test.ts +++ b/apps/api/test/desktop-updates.test.ts @@ -1,5 +1,6 @@ +import Fastify from "fastify"; import { describe, expect, it } from "vitest"; -import { DesktopReleases, newer } from "../src/desktop/updates.js"; +import { DesktopReleases, newer, registerDesktopRoutes } from "../src/desktop/updates.js"; const API = "https://git.example/api/v1/repos/o/r"; @@ -70,4 +71,16 @@ describe("desktop updates", () => { expect(await r.updateFor("darwin", "aarch64", "0.3.2")).toMatchObject({ version: "0.3.4", url: "/v1/desktop/download/11/PhotoReport.app.tar.gz" }); expect(await r.file(11, "PhotoReport.app.tar.gz")).not.toBeNull(); }); + + it("answers 503 instead of crashing the route when Gitea is unreachable (BIT-31961)", async () => { + const releases = new DesktopReleases({ api: API, token: "t" }, async () => { + throw new TypeError("fetch failed"); + }); + const app = Fastify({ logger: false }); + await registerDesktopRoutes(app, { releases, publicApiUrl: "https://app.example" }); + + const res = await app.inject({ method: "GET", url: "/v1/desktop/download/7/PhotoReport_universal.app.tar.gz" }); + expect(res.statusCode).toBe(503); + expect(res.json()).toEqual({ error: "download_unavailable" }); + }); });