diff --git a/lib/api/http.ts b/lib/api/http.ts index 4447b45..ccefd91 100644 --- a/lib/api/http.ts +++ b/lib/api/http.ts @@ -6,14 +6,14 @@ type JsonRequestInit = Omit & { json?: unknown } -async function readJsonBody(res: Response): Promise { +async function readJsonBody(res: Response): Promise<{ json: unknown | null; text: string }> { const text = await res.text() - if (!text) return null + if (!text) return { json: null, text: "" } try { - return JSON.parse(text) as unknown + return { json: JSON.parse(text) as unknown, text } } catch { - return null + return { json: null, text } } } @@ -64,7 +64,7 @@ export async function httpJson(path: string, init?: JsonRequestInit): Promise body, }) - const data = await readJsonBody(res) + const { json: data, text } = await readJsonBody(res) if (res.ok) { return data as T @@ -83,5 +83,8 @@ export async function httpJson(path: string, init?: JsonRequestInit): Promise throw apiError } - throw { code: res.status, msg: res.statusText || "Request failed" } satisfies ApiError + throw { + code: res.status, + msg: text || res.statusText || "Request failed", + } satisfies ApiError }