mirror of
https://github.com/LemmyNet/lemmy-js-client.git
synced 2024-11-25 22:01:12 +00:00
Merge pull request #122 from LemmyNet/add_error_message_as_exception
Making the http `{"error": "..."}` json into a string exception.
This commit is contained in:
commit
a6278881ea
1 changed files with 12 additions and 0 deletions
12
src/http.ts
12
src/http.ts
|
@ -1282,6 +1282,8 @@ export class LemmyHttp {
|
|||
headers: this.headers,
|
||||
});
|
||||
|
||||
await this.checkandThrowError(response);
|
||||
|
||||
return await response.json();
|
||||
} else {
|
||||
const response = await fetch(this.buildFullUrl(endpoint), {
|
||||
|
@ -1293,9 +1295,19 @@ export class LemmyHttp {
|
|||
body: JSON.stringify(form),
|
||||
});
|
||||
|
||||
await this.checkandThrowError(response);
|
||||
|
||||
return await response.json();
|
||||
}
|
||||
}
|
||||
|
||||
private async checkandThrowError(response: Response) {
|
||||
if (!response.ok) {
|
||||
const errJson = await response.json();
|
||||
const errString: string = errJson["error"] ?? response.statusText;
|
||||
throw new Error(errString);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function encodeGetParams<BodyType extends object>(p: BodyType): string {
|
||||
|
|
Loading…
Reference in a new issue