Fix throwing for non-JSON error responses.

This commit is contained in:
diamondburned 2023-06-19 14:41:12 -07:00
parent 89bd17fa52
commit ee898d8ea0
No known key found for this signature in database
GPG key ID: D78C4471CE776659

View file

@ -1315,13 +1315,21 @@ export class LemmyHttp {
body: JSON.stringify(form), body: JSON.stringify(form),
}); });
} }
const json = await response.json();
if (!response.ok) { if (!response.ok) {
throw json["error"] ?? response.statusText; 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 { } else {
return json; error = await response.text();
} }
throw error || response.statusText;
}
return await response.json();
} }
} }