feat(client): Add global error handler

This commit is contained in:
Jesse Wierzbinski 2024-06-19 12:48:09 -10:00
parent 218af68dcc
commit f998264300
No known key found for this signature in database

View file

@ -81,6 +81,9 @@ export class BaseClient {
constructor( constructor(
protected baseUrl: URL, protected baseUrl: URL,
private accessToken?: string, private accessToken?: string,
public globalCatch: (error: ResponseError) => void = () => {
// Do nothing by default
},
) {} ) {}
get url(): URL { get url(): URL {
@ -159,9 +162,12 @@ export class BaseClient {
path: string, path: string,
extra?: RequestInit, extra?: RequestInit,
): Promise<Output<ReturnType>> { ): Promise<Output<ReturnType>> {
return this.request( return this.request<ReturnType>(
this.constructRequest(path, "GET", undefined, extra), this.constructRequest(path, "GET", undefined, extra),
); ).catch((e) => {
this.globalCatch(e);
throw e;
});
} }
public post<ReturnType>( public post<ReturnType>(
@ -169,7 +175,12 @@ export class BaseClient {
body?: object, body?: object,
extra?: RequestInit, extra?: RequestInit,
): Promise<Output<ReturnType>> { ): Promise<Output<ReturnType>> {
return this.request(this.constructRequest(path, "POST", body, extra)); return this.request<ReturnType>(
this.constructRequest(path, "POST", body, extra),
).catch((e) => {
this.globalCatch(e);
throw e;
});
} }
public postForm<ReturnType>( public postForm<ReturnType>(
@ -177,14 +188,17 @@ export class BaseClient {
body: FormData | ConvertibleObject, body: FormData | ConvertibleObject,
extra?: RequestInit, extra?: RequestInit,
): Promise<Output<ReturnType>> { ): Promise<Output<ReturnType>> {
return this.request( return this.request<ReturnType>(
this.constructRequest( this.constructRequest(
path, path,
"POST", "POST",
body instanceof FormData ? body : objectToFormData(body), body instanceof FormData ? body : objectToFormData(body),
extra, extra,
), ),
); ).catch((e) => {
this.globalCatch(e);
throw e;
});
} }
public put<ReturnType>( public put<ReturnType>(
@ -192,7 +206,12 @@ export class BaseClient {
body?: object, body?: object,
extra?: RequestInit, extra?: RequestInit,
): Promise<Output<ReturnType>> { ): Promise<Output<ReturnType>> {
return this.request(this.constructRequest(path, "PUT", body, extra)); return this.request<ReturnType>(
this.constructRequest(path, "PUT", body, extra),
).catch((e) => {
this.globalCatch(e);
throw e;
});
} }
public putForm<ReturnType>( public putForm<ReturnType>(
@ -200,14 +219,17 @@ export class BaseClient {
body: FormData | ConvertibleObject, body: FormData | ConvertibleObject,
extra?: RequestInit, extra?: RequestInit,
): Promise<Output<ReturnType>> { ): Promise<Output<ReturnType>> {
return this.request( return this.request<ReturnType>(
this.constructRequest( this.constructRequest(
path, path,
"PUT", "PUT",
body instanceof FormData ? body : objectToFormData(body), body instanceof FormData ? body : objectToFormData(body),
extra, extra,
), ),
); ).catch((e) => {
this.globalCatch(e);
throw e;
});
} }
public patch<ReturnType>( public patch<ReturnType>(
@ -215,7 +237,12 @@ export class BaseClient {
body?: object, body?: object,
extra?: RequestInit, extra?: RequestInit,
): Promise<Output<ReturnType>> { ): Promise<Output<ReturnType>> {
return this.request(this.constructRequest(path, "PATCH", body, extra)); return this.request<ReturnType>(
this.constructRequest(path, "PATCH", body, extra),
).catch((e) => {
this.globalCatch(e);
throw e;
});
} }
public patchForm<ReturnType>( public patchForm<ReturnType>(
@ -223,14 +250,17 @@ export class BaseClient {
body: FormData | ConvertibleObject, body: FormData | ConvertibleObject,
extra?: RequestInit, extra?: RequestInit,
): Promise<Output<ReturnType>> { ): Promise<Output<ReturnType>> {
return this.request( return this.request<ReturnType>(
this.constructRequest( this.constructRequest(
path, path,
"PATCH", "PATCH",
body instanceof FormData ? body : objectToFormData(body), body instanceof FormData ? body : objectToFormData(body),
extra, extra,
), ),
); ).catch((e) => {
this.globalCatch(e);
throw e;
});
} }
public delete<ReturnType>( public delete<ReturnType>(
@ -238,7 +268,12 @@ export class BaseClient {
body?: object, body?: object,
extra?: RequestInit, extra?: RequestInit,
): Promise<Output<ReturnType>> { ): Promise<Output<ReturnType>> {
return this.request(this.constructRequest(path, "DELETE", body, extra)); return this.request<ReturnType>(
this.constructRequest(path, "DELETE", body, extra),
).catch((e) => {
this.globalCatch(e);
throw e;
});
} }
public deleteForm<ReturnType>( public deleteForm<ReturnType>(
@ -246,13 +281,16 @@ export class BaseClient {
body: FormData | ConvertibleObject, body: FormData | ConvertibleObject,
extra?: RequestInit, extra?: RequestInit,
): Promise<Output<ReturnType>> { ): Promise<Output<ReturnType>> {
return this.request( return this.request<ReturnType>(
this.constructRequest( this.constructRequest(
path, path,
"DELETE", "DELETE",
body instanceof FormData ? body : objectToFormData(body), body instanceof FormData ? body : objectToFormData(body),
extra, extra,
), ),
); ).catch((e) => {
this.globalCatch(e);
throw e;
});
} }
} }