better FE error handling

This commit is contained in:
ari committed 2022-04-18 18:03:00 -04:00
1 parent b329e3f594
commit 9e9cea9269
5 files changed
+106 -10

No files matched your search

+13 -5
View File
@@ -1,14 +1,15 @@
import { APIRequest, APIResponse } from './types';
export interface API {
makeCall: <Res = any, Req = any>(req: APIRequest<Req>) => Promise<APIResponse<Res>>;
makeCall: <Res = any, Req = any>(req: APIRequest<Req>) => Promise<APIResponse<Res>>;
}
export const makeRemoteAPI = (baseURL: string): API => ({
async makeCall({
path,
method = "POST",
data={}
}) {
path,
method = "POST",
data = {}
}) {
try {
const reply = await fetch(baseURL + path, {
method,
headers: {
@@ -30,5 +31,12 @@ export const makeRemoteAPI = (baseURL: string): API => ({
console.error(response)
}
return response
} catch (err) {
console.error(err)
return {
ok: false,
error: `${err} ${err?.stack}`,
}
}
}
})