Get rid of null return values in API

This commit is contained in:
Alexis Sellier
2021-06-13 13:30:09 +02:00
parent 4e8081136f
commit 1c8c1b925e
4 changed files with 24 additions and 28 deletions
+11 -9
View File
@@ -1,25 +1,27 @@
import type { Config } from '@app/config';
export async function get(path: string, config: Config): Promise<any | null> {
if (! config.seed.api) return null;
export async function get(path: string, config: Config): Promise<any> {
if (! config.seed.api)
throw new Error("Seed HTTP API unavailable");
const url = `${config.seed.api}/v1/${path}`;
let response = null;
try {
const response = await fetch(url, {
response = await fetch(url, {
method: 'GET',
headers: {
'Accept': 'application/json',
}
});
if (! response.ok) {
return null;
}
return response.json();
} catch {
} catch (err) {
throw new ApiError(url, "API request failed");
}
if (! response.ok) {
throw new ApiError(url, "Not found");
}
return response.json();
}
class ApiError extends Error {
+7 -13
View File
@@ -15,17 +15,13 @@
export let onSelect: (event: { detail: string }) => void;
export let org: string | null = null;
let blob: Promise<proj.Blob | null> | null = null;
const fetchTree = async (path: string) => {
return proj.getTree(urn, commit, path, config);
};
$: if (path === "/") {
blob = proj.getReadme(urn, commit, config);
} else {
blob = proj.getBlob(urn, commit, path, config);
}
$: getBlob = path === "/"
? proj.getReadme(urn, commit, config)
: proj.getBlob(urn, commit, path, config);
$: getAnchor = org ? Org.getAnchor(org, urn, config) : null;
</script>
@@ -138,14 +134,10 @@
</div>
</div>
<div class="column-right">
{#await blob}
{#await getBlob}
<Loading small center />
{:then blob}
{#if blob}
<Blob {blob} />
{:else}
<!-- Project has no README -->
{/if}
<Blob {blob} />
{:catch}
<div class="error error-message file-not-found">
<header>
@@ -167,6 +159,8 @@
{:catch err}
<div class="container center-content">
<div class="error error-message text-small">
<!-- TODO: Differentiate between (1) commit doesn't exist and (2) failed
to fetch - this needs a change to the backend. -->
API request to <code class="text-small">{err.url}</code> failed
</div>
</div>
+2 -2
View File
@@ -13,8 +13,8 @@
};
export let urn: string;
export let org: string | undefined;
export let commit: string | undefined;
export let org: string = "";
export let commit: string = "";
export let config: Config;
export let path: string;
+4 -4
View File
@@ -66,7 +66,7 @@ export interface Tree {
path: string;
}
export async function getInfo(urn: string, config: Config): Promise<Info | null> {
export async function getInfo(urn: string, config: Config): Promise<Info> {
return api.get(`projects/${urn}`, config);
}
@@ -75,7 +75,7 @@ export async function getTree(
commit: string,
path: string,
config: Config
): Promise<any | null> {
): Promise<any> {
if (path === "/") {
path = "";
}
@@ -87,7 +87,7 @@ export async function getBlob(
commit: string,
path: string,
config: Config
): Promise<Blob | null> {
): Promise<Blob> {
return api.get(`projects/${urn}/blob/${commit}/${path}`, config);
}
@@ -95,7 +95,7 @@ export async function getReadme(
urn: string,
commit: string,
config: Config
): Promise<Blob | null> {
): Promise<Blob> {
return api.get(`projects/${urn}/readme/${commit}`, config);
}