mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 16:08:56 +00:00
fix(cli): name the refused file and parameter when a cloud upload is rejected (#7155)
* fix(cli): name the refused file and parameter when a cloud upload is rejected * fix(cli): blame the file type only for type refusals at upload complete
This commit is contained in:
@@ -109,6 +109,21 @@ describe("cloud/errors reportApiError", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("names the refused parameter next to the server's message", () => {
|
||||
const err = new HyperframesApiError({
|
||||
status: 400,
|
||||
message: "Stored file type not supported",
|
||||
code: "invalid_parameter",
|
||||
param: "content_type",
|
||||
});
|
||||
expect(() => reportApiError("Media upload failed", err)).toThrow(CliRuntimeError);
|
||||
expect(errorBox).toHaveBeenCalledWith(
|
||||
"Media upload failed (HTTP 400)",
|
||||
"Stored file type not supported (parameter: content_type)",
|
||||
expect.any(String),
|
||||
);
|
||||
});
|
||||
|
||||
it("omits the third line when there is no code, hint, or suggestion", () => {
|
||||
const err = new HyperframesApiError({ status: 503, message: "unavailable" });
|
||||
expect(() => reportApiError("Get failed", err)).toThrow(CliRuntimeError);
|
||||
|
||||
@@ -75,18 +75,22 @@ export function reportApiError(
|
||||
const code = hintCode(err);
|
||||
const hint = code ? hints[code] : undefined;
|
||||
const title = `${stage} (HTTP ${err.status})`;
|
||||
const message =
|
||||
err.param && !err.message.includes(err.param)
|
||||
? `${err.message} (parameter: ${err.param})`
|
||||
: err.message;
|
||||
// Priority: code-specific hint > caller suggestion > bare code
|
||||
// label > no third line. Code-specific hints win because they
|
||||
// address the specific failure mode; the caller suggestion is a
|
||||
// generic-context fallback.
|
||||
if (hint) {
|
||||
errorBox(title, err.message, hint);
|
||||
errorBox(title, message, hint);
|
||||
} else if (options.suggestion) {
|
||||
errorBox(title, err.message, options.suggestion);
|
||||
errorBox(title, message, options.suggestion);
|
||||
} else if (err.code) {
|
||||
errorBox(title, err.message, `code: ${err.code}`);
|
||||
errorBox(title, message, `code: ${err.code}`);
|
||||
} else {
|
||||
errorBox(title, err.message);
|
||||
errorBox(title, message);
|
||||
}
|
||||
failCommand();
|
||||
}
|
||||
|
||||
@@ -192,6 +192,62 @@ describe("uploadZipViaDirectUpload", () => {
|
||||
expect(completeCalls).toBe(1);
|
||||
});
|
||||
|
||||
it("names the file and keeps the server's words when complete refuses its type", async () => {
|
||||
const completeAssetUpload = vi.fn(async () => {
|
||||
throw new HyperframesApiError({
|
||||
status: 400,
|
||||
message: "Stored file type not supported: application/octet-stream",
|
||||
code: "invalid_parameter",
|
||||
param: "asset_id",
|
||||
});
|
||||
});
|
||||
const client = makeClient({
|
||||
completeAssetUpload:
|
||||
completeAssetUpload as unknown as HyperframesCloudClient["completeAssetUpload"],
|
||||
});
|
||||
|
||||
const err = await uploadAssetViaDirectUpload({
|
||||
client,
|
||||
bytes: new Uint8Array([0]),
|
||||
filename: "song.mp3",
|
||||
contentType: "audio/mpeg",
|
||||
fetchImpl: makeFetchOk(),
|
||||
}).catch((e: unknown) => e);
|
||||
|
||||
expect(err).toBeInstanceOf(HyperframesApiError);
|
||||
expect((err as HyperframesApiError).message).toBe(
|
||||
"The cloud couldn't read this audio file's type: song.mp3. It said: Stored file type not supported: application/octet-stream",
|
||||
);
|
||||
expect((err as HyperframesApiError).param).toBe("asset_id");
|
||||
expect((err as HyperframesApiError).status).toBe(400);
|
||||
});
|
||||
|
||||
it("does not blame the file's type when complete refuses an expired upload", async () => {
|
||||
const completeAssetUpload = vi.fn(async () => {
|
||||
throw new HyperframesApiError({
|
||||
status: 400,
|
||||
message: "This upload has expired; create a new upload.",
|
||||
code: "invalid_parameter",
|
||||
});
|
||||
});
|
||||
const client = makeClient({
|
||||
completeAssetUpload:
|
||||
completeAssetUpload as unknown as HyperframesCloudClient["completeAssetUpload"],
|
||||
});
|
||||
|
||||
const err = await uploadAssetViaDirectUpload({
|
||||
client,
|
||||
bytes: new Uint8Array([0]),
|
||||
filename: "song.mp3",
|
||||
contentType: "audio/mpeg",
|
||||
fetchImpl: makeFetchOk(),
|
||||
}).catch((e: unknown) => e);
|
||||
|
||||
expect((err as HyperframesApiError).message).toBe(
|
||||
"The cloud refused song.mp3: This upload has expired; create a new upload.",
|
||||
);
|
||||
});
|
||||
|
||||
it("surfaces PUT failures with response body detail", async () => {
|
||||
const client = makeClient();
|
||||
const fetchImpl = vi.fn(
|
||||
|
||||
@@ -199,6 +199,19 @@ async function completeWithRetry(
|
||||
throw lastErr ?? new Error("completeAssetUpload retries exhausted");
|
||||
}
|
||||
|
||||
// complete answers invalid_parameter for type, size, checksum and expiry; only type refusals blame the type.
|
||||
const TYPE_REFUSAL = /^(Stored file type|content_type not allowed|Stored file does not have a ZIP)/;
|
||||
|
||||
function nameRefusedFile(err: unknown, filename: string, contentType: string): unknown {
|
||||
if (!(err instanceof HyperframesApiError) || err.code !== "invalid_parameter") return err;
|
||||
const kind = contentType.split("/")[0];
|
||||
const noun = kind === "audio" || kind === "video" || kind === "image" ? `${kind} file` : "file";
|
||||
const lead = TYPE_REFUSAL.test(err.message)
|
||||
? `The cloud couldn't read this ${noun}'s type: ${filename}. It said:`
|
||||
: `The cloud refused ${filename}:`;
|
||||
return new HyperframesApiError({ ...err, message: `${lead} ${err.message}` });
|
||||
}
|
||||
|
||||
export async function uploadAssetViaDirectUpload(
|
||||
opts: Omit<UploadZipViaDirectOptions, "bytes"> & { contentType: string } & (
|
||||
| { bytes: Uint8Array }
|
||||
@@ -231,7 +244,13 @@ export async function uploadAssetViaDirectUpload(
|
||||
opts.onProgress?.({ phase: "upload", percent: 100 });
|
||||
|
||||
opts.onProgress?.({ phase: "complete" });
|
||||
const completed = await completeWithRetry(opts.client, initialize.asset_id, checksum_sha256);
|
||||
const completed = await completeWithRetry(
|
||||
opts.client,
|
||||
initialize.asset_id,
|
||||
checksum_sha256,
|
||||
).catch((err: unknown) => {
|
||||
throw nameRefusedFile(err, filename, opts.contentType);
|
||||
});
|
||||
return {
|
||||
asset_id: completed.asset_id,
|
||||
url: completed.url,
|
||||
|
||||
Reference in New Issue
Block a user