Skip to content

best way to save what you love

web/api: remove deprecated statuses, update error type, time out request

wukkoac6d68ec4585
diff --git a/web/src/components/save/buttons/DownloadButton.svelte b/web/src/components/save/buttons/DownloadButton.svelte
index c2a26a49..3f026dac 100644
--- a/web/src/components/save/buttons/DownloadButton.svelte
+++ b/web/src/components/save/buttons/DownloadButton.svelte
@@ -68,16 +68,16 @@
return createDialog({
...defaultErrorPopup,
- bodyText: "couldn't access the api",
+ bodyText: $t("error.api.unreachable"),
});
}
- if (response.status === "error" || response.status === "rate-limit") {
+ if (response.status === "error") {
changeDownloadButton("error");
return createDialog({
...defaultErrorPopup,
- bodyText: response.text,
+ bodyText: $t(response.error.code),
});
}
@@ -101,7 +101,7 @@
return createDialog({
...defaultErrorPopup,
- bodyText: "couldn't probe the stream",
+ bodyText: $t("error.stream.failed_probe"),
});
}
}
@@ -139,7 +139,7 @@
return createDialog({
...defaultErrorPopup,
- bodyText: "unknown/unsupported status",
+ bodyText: $t("error.api.unknown_response"),
});
};
</script>
diff --git a/web/src/lib/api.ts b/web/src/lib/api.ts
index dfa3bb90..a631bf1c 100644
--- a/web/src/lib/api.ts
+++ b/web/src/lib/api.ts
@@ -91,12 +91,22 @@ const request = async (url: string) => {
const response: Optional<CobaltAPIResponse> = await fetch(api, {
method: "POST",
redirect: "manual",
+ signal: AbortSignal.timeout(10000),
body: JSON.stringify(request),
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
- }).then(r => r.json()).catch(() => {});
+ }).then(r => r.json()).catch((e) => {
+ if (e?.message?.includes("timed out")) {
+ return {
+ status: "error",
+ error: {
+ code: "error.api.timed_out"
+ }
+ }
+ }
+ });
return response;
}
diff --git a/web/src/lib/types/api.ts b/web/src/lib/types/api.ts
index f3b81851..8df6d570 100644
--- a/web/src/lib/types/api.ts
+++ b/web/src/lib/types/api.ts
@@ -1,14 +1,19 @@
enum CobaltResponseType {
Error = 'error',
- RateLimit = 'rate-limit',
Picker = 'picker',
Redirect = 'redirect',
Stream = 'stream',
}
type CobaltErrorResponse = {
- status: CobaltResponseType.Error | CobaltResponseType.RateLimit,
- text: string,
+ status: CobaltResponseType.Error,
+ error: {
+ code: string,
+ context?: {
+ service?: string,
+ limit?: number,
+ }
+ }
};
type CobaltPartialURLResponse = {