From ee898d8ea02d0517275ec8a3d42f4574fd60ea4d Mon Sep 17 00:00:00 2001 From: diamondburned Date: Mon, 19 Jun 2023 14:41:12 -0700 Subject: [PATCH] Fix throwing for non-JSON error responses. --- src/http.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/http.ts b/src/http.ts index a0b2732..cad8cee 100644 --- a/src/http.ts +++ b/src/http.ts @@ -1315,13 +1315,21 @@ export class LemmyHttp { body: JSON.stringify(form), }); } - const json = await response.json(); if (!response.ok) { - throw json["error"] ?? response.statusText; - } else { - return json; + let error: string | undefined = undefined; + if (response.headers.get("content-type")?.includes("application/json")) { + error = await response + .json() + .then(json => json["error"]) + .catch(() => undefined); + } else { + error = await response.text(); + } + throw error || response.statusText; } + + return await response.json(); } }