Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix(api): log why upload verification fails

Andrej6160046c234f
diff --git a/apps/api/src/storage/describe-storage-error.ts b/apps/api/src/storage/describe-storage-error.ts
new file mode 100644
index 000000000..c6d94be5d
--- /dev/null
+++ b/apps/api/src/storage/describe-storage-error.ts
@@ -0,0 +1,22 @@
+type StorageErrorShape = {
+ name?: unknown;
+ code?: unknown;
+ Code?: unknown;
+ cause?: { code?: unknown };
+ $metadata?: { httpStatusCode?: unknown };
+ $response?: { statusCode?: unknown };
+};
+
+export function describeStorageError(error: unknown) {
+ if (typeof error !== "object" || error === null) {
+ return { name: typeof error };
+ }
+ const shape = error as StorageErrorShape;
+ const status = shape.$metadata?.httpStatusCode ?? shape.$response?.statusCode;
+ const code = shape.Code ?? shape.code ?? shape.cause?.code;
+ return {
+ name: typeof shape.name === "string" ? shape.name : "Error",
+ ...(typeof status === "number" ? { status } : {}),
+ ...(typeof code === "string" ? { code } : {}),
+ };
+}
diff --git a/apps/api/src/storage/s3.ts b/apps/api/src/storage/s3.ts
index 6b89819ab..2112b6110 100644
--- a/apps/api/src/storage/s3.ts
+++ b/apps/api/src/storage/s3.ts
@@ -12,6 +12,7 @@ import {
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import { createId } from "@paralleldrive/cuid2";
import { config } from "dotenv-mono";
+import { describeStorageError } from "./describe-storage-error";
config();
@@ -447,6 +448,10 @@ export async function verifyTaskAssetUpload(
throw new InvalidUploadedAssetError("Uploaded object was not found.");
}
// Provider errors may include endpoints, signed URLs or other secrets.
+ console.error(
+ "Unable to verify uploaded object:",
+ describeStorageError(error),
+ );
throw new Error("Unable to verify uploaded object.");
}
@@ -459,7 +464,11 @@ export async function verifyTaskAssetUpload(
new DeleteObjectCommand({ Bucket: config.bucket, Key: key }),
{ abortSignal },
);
- } catch {
+ } catch (error) {
+ console.error(
+ "Unable to remove oversized uploaded object:",
+ describeStorageError(error),
+ );
throw new Error("Unable to remove oversized uploaded object.");
}
throw new InvalidUploadedAssetError(
diff --git a/tests/api/storage/describe-storage-error.test.ts b/tests/api/storage/describe-storage-error.test.ts
new file mode 100644
index 000000000..c6b78290a
--- /dev/null
+++ b/tests/api/storage/describe-storage-error.test.ts
@@ -0,0 +1,54 @@
+import { describe, expect, it } from "vite-plus/test";
+import { describeStorageError } from "../../../apps/api/src/storage/describe-storage-error";
+
+describe("describeStorageError", () => {
+ it("keeps the SDK error name and HTTP status", () => {
+ const error = Object.assign(new Error("UnknownError"), {
+ name: "Forbidden",
+ $metadata: { httpStatusCode: 403 },
+ });
+
+ expect(describeStorageError(error)).toEqual({
+ name: "Forbidden",
+ status: 403,
+ });
+ });
+
+ it("keeps network error codes without the message", () => {
+ const error = Object.assign(
+ new Error("getaddrinfo ENOTFOUND bucket.storage.example.test"),
+ { code: "ENOTFOUND" },
+ );
+
+ const described = describeStorageError(error);
+
+ expect(described).toEqual({ name: "Error", code: "ENOTFOUND" });
+ expect(JSON.stringify(described)).not.toContain("storage.example.test");
+ });
+
+ it("reads the status from the raw response when metadata is missing", () => {
+ const error = Object.assign(new Error("Deserialization error"), {
+ $response: { statusCode: 200 },
+ });
+
+ expect(describeStorageError(error)).toEqual({
+ name: "Error",
+ status: 200,
+ });
+ });
+
+ it("reads codes from the error cause", () => {
+ const error = new Error("socket hang up", {
+ cause: { code: "ECONNRESET" },
+ });
+
+ expect(describeStorageError(error)).toEqual({
+ name: "Error",
+ code: "ECONNRESET",
+ });
+ });
+
+ it("describes non-error values by type", () => {
+ expect(describeStorageError("boom")).toEqual({ name: "string" });
+ });
+});
diff --git a/tests/api/storage/s3.test.ts b/tests/api/storage/s3.test.ts
index 3285d695d..40b530910 100644
--- a/tests/api/storage/s3.test.ts
+++ b/tests/api/storage/s3.test.ts
@@ -27,6 +27,7 @@ import {
sanitizePathSegment,
validateProjectBackgroundUploadInput,
validateTaskAssetUploadInput,
+ verifyTaskAssetUpload,
} from "../../../apps/api/src/storage/s3";
describe("S3 helpers", () => {
@@ -376,6 +377,34 @@ describe("S3 helpers", () => {
expect(searchParams.has("x-amz-sdk-checksum-algorithm")).toBe(false);
});
+ it("logs a safe summary when an uploaded object cannot be verified", async () => {
+ process.env.S3_ENDPOINT = "https://storage.example.test";
+ process.env.S3_BUCKET = "kaneo";
+ process.env.S3_ACCESS_KEY_ID = "test-access-key";
+ process.env.S3_SECRET_ACCESS_KEY = "test-secret-key";
+
+ vi.spyOn(S3Client.prototype, "send").mockRejectedValue(
+ Object.assign(
+ new Error("https://storage.example.test/kaneo?X-Amz-Signature=x"),
+ { name: "Forbidden", $metadata: { httpStatusCode: 403 } },
+ ) as never,
+ );
+ const log = vi.spyOn(console, "error").mockImplementation(() => {});
+
+ await expect(
+ verifyTaskAssetUpload("workspace/w1/project/p1/task/t1/a.png", {
+ size: 12,
+ contentType: "image/png",
+ }),
+ ).rejects.toThrow("Unable to verify uploaded object.");
+
+ expect(log).toHaveBeenCalledWith("Unable to verify uploaded object:", {
+ name: "Forbidden",
+ status: 403,
+ });
+ expect(JSON.stringify(log.mock.calls)).not.toContain("X-Amz-Signature");
+ });
+
it("copies a stored object into the destination task prefix", async () => {
process.env.S3_ENDPOINT = "https://storage.example.test";
process.env.S3_BUCKET = "kaneo";