Making the error json into an exception.

This commit is contained in:
Dessalines 2023-05-21 22:45:44 -04:00
parent 6801b2ae24
commit 3307940dbe

View file

@ -1268,6 +1268,8 @@ export class LemmyHttp {
headers: this.headers, headers: this.headers,
}); });
await this.checkandThrowError(response);
return await response.json(); return await response.json();
} else { } else {
const response = await fetch(this.buildFullUrl(endpoint), { const response = await fetch(this.buildFullUrl(endpoint), {
@ -1279,9 +1281,19 @@ export class LemmyHttp {
body: JSON.stringify(form), body: JSON.stringify(form),
}); });
await this.checkandThrowError(response);
return await response.json(); return await response.json();
} }
} }
private async checkandThrowError(response: Response) {
if (!response.ok) {
let errJson = await response.json();
let errString: string = errJson["error"] ?? response.statusText;
throw new Error(errString);
}
}
} }
function encodeGetParams<BodyType extends object>(p: BodyType): string { function encodeGetParams<BodyType extends object>(p: BodyType): string {