Include request body in CurseForge and Modrinth upload errors
This commit is contained in:
parent
95af305966
commit
e2a4de3f45
@ -1,4 +1,4 @@
|
||||
import { readErrorBody } from "../utils/fetch";
|
||||
import { readErrorBody, summarizeFormData } from "../utils/fetch";
|
||||
|
||||
/** CurseForge API 客户端,封装两套 API 的鉴权与 User-Agent。 */
|
||||
export class CurseForgeClient {
|
||||
@ -73,9 +73,10 @@ export class CurseForgeClient {
|
||||
body,
|
||||
});
|
||||
if (!res.ok) {
|
||||
const reqBody = summarizeFormData(body);
|
||||
const resBody = await readErrorBody(res);
|
||||
throw new Error(
|
||||
`CurseForge Upload POST ${path} failed: ${res.status} ${res.statusText}\n${resBody}`,
|
||||
`CurseForge Upload POST ${path} failed: ${res.status} ${res.statusText}\n\n--Request--:\n${reqBody}\n\n--Response--:\n${resBody}`,
|
||||
);
|
||||
}
|
||||
return res.json() as Promise<T>;
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import { readErrorBody } from "../utils/fetch";
|
||||
import { readErrorBody, summarizeFormData } from "../utils/fetch";
|
||||
|
||||
/** Modrinth API v2 的基础请求客户端,封装鉴权与 User-Agent。 */
|
||||
export class ModrinthClient {
|
||||
@ -47,9 +47,10 @@ export class ModrinthClient {
|
||||
body,
|
||||
});
|
||||
if (!res.ok) {
|
||||
const reqBody = summarizeFormData(body);
|
||||
const resBody = await readErrorBody(res);
|
||||
throw new Error(
|
||||
`Modrinth API POST ${path} failed: ${res.status} ${res.statusText}\n${resBody}`,
|
||||
`Modrinth API POST ${path} failed: ${res.status} ${res.statusText}\n\n--Request--:\n${reqBody}\n\n--Response--:\n${resBody}`,
|
||||
);
|
||||
}
|
||||
return res.json() as Promise<T>;
|
||||
|
||||
@ -10,3 +10,18 @@ export async function readErrorBody(res: Response): Promise<string> {
|
||||
return "(unable to read response body)";
|
||||
}
|
||||
}
|
||||
|
||||
/** 将 FormData 内容转为可读摘要字符串,用于拼入错误信息。 */
|
||||
export function summarizeFormData(form: FormData): string {
|
||||
const parts: string[] = [];
|
||||
for (const [key, value] of form.entries()) {
|
||||
if (typeof value === "string") {
|
||||
parts.push(`${key}: ${value.slice(0, 2048)}`);
|
||||
} else {
|
||||
parts.push(
|
||||
`${key}: <File name="${value.name.slice(0, 256)}" size=${value.size} type="${value.type}">`,
|
||||
);
|
||||
}
|
||||
}
|
||||
return parts.length > 0 ? parts.join("\n") : "(empty body)";
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user