diff --git a/packages/appkit/src/plugins/files/plugin.ts b/packages/appkit/src/plugins/files/plugin.ts index e745b347..9344af85 100644 --- a/packages/appkit/src/plugins/files/plugin.ts +++ b/packages/appkit/src/plugins/files/plugin.ts @@ -1,3 +1,4 @@ +import { STATUS_CODES } from "node:http"; import { Readable } from "node:stream"; import { ApiError } from "@databricks/sdk-experimental"; import type express from "express"; @@ -424,6 +425,13 @@ export class FilesPlugin extends Plugin { res.status(500).json({ error: fallbackMessage, plugin: this.name }); } + private _sendStatusError(res: express.Response, status: number): void { + res.status(status).json({ + error: STATUS_CODES[status] ?? "Unknown Error", + plugin: this.name, + }); + } + private async _handleList( req: express.Request, res: express.Response, @@ -446,9 +454,7 @@ export class FilesPlugin extends Plugin { ); if (!result.ok) { - res - .status(result.status) - .json({ error: "List failed", plugin: this.name }); + this._sendStatusError(res, result.status); return; } res.json(result.data); @@ -484,9 +490,7 @@ export class FilesPlugin extends Plugin { ); if (!result.ok) { - res - .status(result.status) - .json({ error: "Read failed", plugin: this.name }); + this._sendStatusError(res, result.status); return; } res.type("text/plain").send(result.data); @@ -550,9 +554,7 @@ export class FilesPlugin extends Plugin { }, settings); if (!response.ok) { - res - .status(response.status) - .json({ error: `${label} failed`, plugin: this.name }); + this._sendStatusError(res, response.status); return; } @@ -588,9 +590,7 @@ export class FilesPlugin extends Plugin { nodeStream.on("error", (err) => { logger.error("Stream error during %s: %O", opts.mode, err); if (!res.headersSent) { - res - .status(500) - .json({ error: `${label} failed`, plugin: this.name }); + this._sendStatusError(res, 500); } else { res.destroy(); } @@ -631,9 +631,7 @@ export class FilesPlugin extends Plugin { ); if (!result.ok) { - res - .status(result.status) - .json({ error: "Exists check failed", plugin: this.name }); + this._sendStatusError(res, result.status); return; } res.json({ exists: result.data }); @@ -669,9 +667,7 @@ export class FilesPlugin extends Plugin { ); if (!result.ok) { - res - .status(result.status) - .json({ error: "Metadata fetch failed", plugin: this.name }); + this._sendStatusError(res, result.status); return; } res.json(result.data); @@ -707,9 +703,7 @@ export class FilesPlugin extends Plugin { ); if (!result.ok) { - res - .status(result.status) - .json({ error: "Preview failed", plugin: this.name }); + this._sendStatusError(res, result.status); return; } res.json(result.data); @@ -807,9 +801,7 @@ export class FilesPlugin extends Plugin { path, contentLength ?? 0, ); - res - .status(result.status) - .json({ error: "Upload failed", plugin: this.name }); + this._sendStatusError(res, result.status); return; } @@ -862,9 +854,7 @@ export class FilesPlugin extends Plugin { ); if (!result.ok) { - res - .status(result.status) - .json({ error: "Create directory failed", plugin: this.name }); + this._sendStatusError(res, result.status); return; } @@ -909,9 +899,7 @@ export class FilesPlugin extends Plugin { ); if (!result.ok) { - res - .status(result.status) - .json({ error: "Delete failed", plugin: this.name }); + this._sendStatusError(res, result.status); return; } diff --git a/packages/appkit/src/plugins/files/tests/plugin.integration.test.ts b/packages/appkit/src/plugins/files/tests/plugin.integration.test.ts index e0117cdc..cbd5a529 100644 --- a/packages/appkit/src/plugins/files/tests/plugin.integration.test.ts +++ b/packages/appkit/src/plugins/files/tests/plugin.integration.test.ts @@ -567,7 +567,7 @@ describe("Files Plugin Integration", () => { expect(response.status).toBe(500); const data = (await response.json()) as { error: string; plugin: string }; - expect(data.error).toBe("Metadata fetch failed"); + expect(data.error).toBe("Internal Server Error"); expect(data.plugin).toBe("files"); }); @@ -583,7 +583,7 @@ describe("Files Plugin Integration", () => { expect(response.status).toBe(500); const data = (await response.json()) as { error: string; plugin: string }; - expect(data.error).toBe("List failed"); + expect(data.error).toBe("Internal Server Error"); expect(data.plugin).toBe("files"); }); @@ -602,7 +602,7 @@ describe("Files Plugin Integration", () => { error: string; plugin: string; }; - expect(data.error).toBe("Metadata fetch failed"); + expect(data.error).toBe("Internal Server Error"); expect(data.plugin).toBe("files"); }); @@ -622,7 +622,7 @@ describe("Files Plugin Integration", () => { error: string; plugin: string; }; - expect(data.error).toBe("Create directory failed"); + expect(data.error).toBe("Internal Server Error"); expect(data.plugin).toBe("files"); }); }); diff --git a/packages/appkit/src/plugins/files/tests/plugin.test.ts b/packages/appkit/src/plugins/files/tests/plugin.test.ts index 27b55cd5..99e08b8c 100644 --- a/packages/appkit/src/plugins/files/tests/plugin.test.ts +++ b/packages/appkit/src/plugins/files/tests/plugin.test.ts @@ -674,7 +674,7 @@ describe("FilesPlugin", () => { expect(res.status).toHaveBeenCalledWith(500); expect(res.json).toHaveBeenCalledWith( expect.objectContaining({ - error: "List failed", + error: "Internal Server Error", plugin: "files", }), ); @@ -698,7 +698,7 @@ describe("FilesPlugin", () => { expect(res.status).toHaveBeenCalledWith(500); expect(res.json).toHaveBeenCalledWith( - expect.objectContaining({ error: "Read failed" }), + expect.objectContaining({ error: "Internal Server Error" }), ); }); @@ -722,7 +722,7 @@ describe("FilesPlugin", () => { expect(res.status).toHaveBeenCalledWith(500); expect(res.json).toHaveBeenCalledWith( - expect.objectContaining({ error: "Exists check failed" }), + expect.objectContaining({ error: "Internal Server Error" }), ); }); @@ -746,7 +746,7 @@ describe("FilesPlugin", () => { expect(res.status).toHaveBeenCalledWith(500); expect(res.json).toHaveBeenCalledWith( - expect.objectContaining({ error: "Metadata fetch failed" }), + expect.objectContaining({ error: "Internal Server Error" }), ); }); @@ -768,7 +768,7 @@ describe("FilesPlugin", () => { expect(res.status).toHaveBeenCalledWith(500); expect(res.json).toHaveBeenCalledWith( - expect.objectContaining({ error: "Download failed" }), + expect.objectContaining({ error: "Internal Server Error" }), ); }); @@ -791,7 +791,7 @@ describe("FilesPlugin", () => { expect(res.status).toHaveBeenCalledWith(500); expect(res.json).toHaveBeenCalledWith( - expect.objectContaining({ error: "Create directory failed" }), + expect.objectContaining({ error: "Internal Server Error" }), ); }); @@ -835,7 +835,7 @@ describe("FilesPlugin", () => { await handlerPromise; const errorBody = res.json.mock.calls[0][0]; - expect(errorBody.error).toBe("List failed"); + expect(errorBody.error).toBe("Internal Server Error"); expect(errorBody.error).not.toContain("secret"); expect(errorBody.error).not.toContain("internal"); }); @@ -876,7 +876,7 @@ describe("FilesPlugin", () => { expect(signalWasAborted).toBe(true); expect(res.status).toHaveBeenCalledWith(500); expect(res.json).toHaveBeenCalledWith( - expect.objectContaining({ error: "List failed" }), + expect.objectContaining({ error: "Internal Server Error" }), ); });