Improve error handling for providers api

This commit is contained in:
mrjvs 2023-12-19 18:47:54 +01:00
parent ca2e20fdbc
commit 4847980947
2 changed files with 26 additions and 5 deletions

View File

@ -1,4 +1,4 @@
import { MetaOutput, ScrapeMedia } from "@movie-web/providers";
import { MetaOutput, NotFoundError, ScrapeMedia } from "@movie-web/providers";
import { mwFetch } from "@/backend/helpers/fetch";
@ -83,11 +83,32 @@ export function connectServerSideEvents<T>(url: string, endEvents: string[]) {
});
});
eventSource.addEventListener("error", (err) => {
eventSource.addEventListener("error", (err: MessageEvent<any>) => {
eventSource.close();
if (err.data) {
const data = JSON.parse(err.data);
let errObj = new Error("scrape error");
if (data.name === NotFoundError.name)
errObj = new NotFoundError("Notfound from server");
Object.assign(errObj, data);
promReject(errObj);
return;
}
console.error("Failed to connect to SSE", err);
promReject(err);
});
eventSource.addEventListener("message", (ev) => {
if (!ev) {
eventSource.close();
return;
}
setTimeout(() => {
promReject(new Error("SSE closed improperly"));
}, 1000);
});
return {
promise: () => promise,
on<Data>(event: string, cb: (data: Data) => void) {

View File

@ -43,7 +43,7 @@ export function useEmbedScraping(
const baseUrlMaker = makeProviderUrl(providerApiUrl);
const conn = connectServerSideEvents<EmbedOutput>(
baseUrlMaker.scrapeEmbed(embedId, url),
["completed"]
["completed", "noOutput"]
);
result = await conn.promise();
} else {
@ -107,7 +107,7 @@ export function useSourceScraping(sourceId: string | null, routerId: string) {
const baseUrlMaker = makeProviderUrl(providerApiUrl);
const conn = connectServerSideEvents<SourcererOutput>(
baseUrlMaker.scrapeSource(sourceId, scrapeMedia),
["completed"]
["completed", "noOutput"]
);
result = await conn.promise();
} else {
@ -151,7 +151,7 @@ export function useSourceScraping(sourceId: string | null, routerId: string) {
result.embeds[0].embedId,
result.embeds[0].url
),
["completed"]
["completed", "noOutput"]
);
embedResult = await conn.promise();
} else {