From f998264300c6d8ac071ca963a50841b0b5e8eeb1 Mon Sep 17 00:00:00 2001 From: Jesse Wierzbinski Date: Wed, 19 Jun 2024 12:48:09 -1000 Subject: [PATCH] feat(client): :sparkles: Add global error handler --- client/lysand/base.ts | 66 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 52 insertions(+), 14 deletions(-) diff --git a/client/lysand/base.ts b/client/lysand/base.ts index dcbd297..583f41b 100644 --- a/client/lysand/base.ts +++ b/client/lysand/base.ts @@ -81,6 +81,9 @@ export class BaseClient { constructor( protected baseUrl: URL, private accessToken?: string, + public globalCatch: (error: ResponseError) => void = () => { + // Do nothing by default + }, ) {} get url(): URL { @@ -159,9 +162,12 @@ export class BaseClient { path: string, extra?: RequestInit, ): Promise> { - return this.request( + return this.request( this.constructRequest(path, "GET", undefined, extra), - ); + ).catch((e) => { + this.globalCatch(e); + throw e; + }); } public post( @@ -169,7 +175,12 @@ export class BaseClient { body?: object, extra?: RequestInit, ): Promise> { - return this.request(this.constructRequest(path, "POST", body, extra)); + return this.request( + this.constructRequest(path, "POST", body, extra), + ).catch((e) => { + this.globalCatch(e); + throw e; + }); } public postForm( @@ -177,14 +188,17 @@ export class BaseClient { body: FormData | ConvertibleObject, extra?: RequestInit, ): Promise> { - return this.request( + return this.request( this.constructRequest( path, "POST", body instanceof FormData ? body : objectToFormData(body), extra, ), - ); + ).catch((e) => { + this.globalCatch(e); + throw e; + }); } public put( @@ -192,7 +206,12 @@ export class BaseClient { body?: object, extra?: RequestInit, ): Promise> { - return this.request(this.constructRequest(path, "PUT", body, extra)); + return this.request( + this.constructRequest(path, "PUT", body, extra), + ).catch((e) => { + this.globalCatch(e); + throw e; + }); } public putForm( @@ -200,14 +219,17 @@ export class BaseClient { body: FormData | ConvertibleObject, extra?: RequestInit, ): Promise> { - return this.request( + return this.request( this.constructRequest( path, "PUT", body instanceof FormData ? body : objectToFormData(body), extra, ), - ); + ).catch((e) => { + this.globalCatch(e); + throw e; + }); } public patch( @@ -215,7 +237,12 @@ export class BaseClient { body?: object, extra?: RequestInit, ): Promise> { - return this.request(this.constructRequest(path, "PATCH", body, extra)); + return this.request( + this.constructRequest(path, "PATCH", body, extra), + ).catch((e) => { + this.globalCatch(e); + throw e; + }); } public patchForm( @@ -223,14 +250,17 @@ export class BaseClient { body: FormData | ConvertibleObject, extra?: RequestInit, ): Promise> { - return this.request( + return this.request( this.constructRequest( path, "PATCH", body instanceof FormData ? body : objectToFormData(body), extra, ), - ); + ).catch((e) => { + this.globalCatch(e); + throw e; + }); } public delete( @@ -238,7 +268,12 @@ export class BaseClient { body?: object, extra?: RequestInit, ): Promise> { - return this.request(this.constructRequest(path, "DELETE", body, extra)); + return this.request( + this.constructRequest(path, "DELETE", body, extra), + ).catch((e) => { + this.globalCatch(e); + throw e; + }); } public deleteForm( @@ -246,13 +281,16 @@ export class BaseClient { body: FormData | ConvertibleObject, extra?: RequestInit, ): Promise> { - return this.request( + return this.request( this.constructRequest( path, "DELETE", body instanceof FormData ? body : objectToFormData(body), extra, ), - ); + ).catch((e) => { + this.globalCatch(e); + throw e; + }); } }