From 3307940dbe76435566b789543983f2be1df2860a Mon Sep 17 00:00:00 2001 From: Dessalines Date: Sun, 21 May 2023 22:45:44 -0400 Subject: [PATCH 1/2] 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 { From e19c60e341870482f976ee7d8e82c99504556792 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Sun, 21 May 2023 22:56:35 -0400 Subject: [PATCH 2/2] Switching to const. --- src/http.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/http.ts b/src/http.ts index 4ee38cc..d9cfb58 100644 --- a/src/http.ts +++ b/src/http.ts @@ -1289,8 +1289,8 @@ export class LemmyHttp { private async checkandThrowError(response: Response) { if (!response.ok) { - let errJson = await response.json(); - let errString: string = errJson["error"] ?? response.statusText; + const errJson = await response.json(); + const errString: string = errJson["error"] ?? response.statusText; throw new Error(errString); } }