Fix throwing for non-JSON error responses.
This commit is contained in:
parent
89bd17fa52
commit
ee898d8ea0
1 changed files with 12 additions and 4 deletions
16
src/http.ts
16
src/http.ts
|
@ -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;
|
||||||
} else {
|
if (response.headers.get("content-type")?.includes("application/json")) {
|
||||||
return json;
|
error = await response
|
||||||
|
.json()
|
||||||
|
.then(json => json["error"])
|
||||||
|
.catch(() => undefined);
|
||||||
|
} else {
|
||||||
|
error = await response.text();
|
||||||
|
}
|
||||||
|
throw error || response.statusText;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return await response.json();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue