From 3307940dbe76435566b789543983f2be1df2860a Mon Sep 17 00:00:00 2001 From: Dessalines Date: Sun, 21 May 2023 22:45:44 -0400 Subject: [PATCH] Making the error json into an exception. --- src/http.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/http.ts b/src/http.ts index ef1c917..4ee38cc 100644 --- a/src/http.ts +++ b/src/http.ts @@ -1268,6 +1268,8 @@ export class LemmyHttp { headers: this.headers, }); + await this.checkandThrowError(response); + return await response.json(); } else { const response = await fetch(this.buildFullUrl(endpoint), { @@ -1279,9 +1281,19 @@ export class LemmyHttp { body: JSON.stringify(form), }); + await this.checkandThrowError(response); + 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(p: BodyType): string {