mirror of
https://github.com/LemmyNet/lemmy-js-client.git
synced 2024-11-26 06:11:14 +00:00
Making the error json into an exception.
This commit is contained in:
parent
6801b2ae24
commit
3307940dbe
1 changed files with 12 additions and 0 deletions
12
src/http.ts
12
src/http.ts
|
@ -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 {
|
||||||
|
|
Loading…
Reference in a new issue