apiGet stability improved

This commit is contained in:
tmshv committed 2024-11-26 18:54:32 +03:00
1 parent 460e8fc3cc
commit 21047227b8
1 file changed
+9 -4
+9 -4
View File
@@ -10,10 +10,15 @@ export type ApiDefaultResponse<O> = () => O
export function apiGet<I, O>(factory: FactoryFunction<I, O>) {
return async (url: string, getDefaultResponse: ApiDefaultResponse<O>) => {
const res = await fetch(url)
if (res.ok) {
const data = await res.json() as I
return factory(data)
try {
const res = await fetch(url)
if (res.ok) {
const data = await res.json() as I
return factory(data)
}
} catch (error) {
console.error(`Failed fetch request: ${error}`)
return getDefaultResponse()
}
return getDefaultResponse()