Improve project failed state

This commit is contained in:
Alexis Sellier
2021-06-11 10:44:10 +02:00
parent b9999209de
commit b856676573
5 changed files with 91 additions and 56 deletions
+30 -9
View File
@@ -3,15 +3,36 @@ import type { Config } from '@app/config';
export async function get(path: string, config: Config): Promise<any | null> {
if (! config.seed.api) return null;
const response = await fetch(`${config.seed.api}/v1/${path}`, {
method: 'GET',
headers: {
'Accept': 'application/json',
}
});
const url = `${config.seed.api}/v1/${path}`;
if (! response.ok) {
return null;
try {
const response = await fetch(url, {
method: 'GET',
headers: {
'Accept': 'application/json',
}
});
if (! response.ok) {
return null;
}
return response.json();
} catch {
throw new ApiError(url, "API request failed");
}
}
class ApiError extends Error {
url: string;
constructor(url: string, message: string) {
super(message);
if (Error.captureStackTrace) {
Error.captureStackTrace(this, ApiError);
}
this.name = "ApiError";
this.url = url;
}
return response.json();
}