diff --git a/src/backend/helpers/providerApi.ts b/src/backend/helpers/providerApi.ts index c6898d2c..affe387f 100644 --- a/src/backend/helpers/providerApi.ts +++ b/src/backend/helpers/providerApi.ts @@ -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(url: string, endEvents: string[]) { }); }); - eventSource.addEventListener("error", (err) => { + eventSource.addEventListener("error", (err: MessageEvent) => { + 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(event: string, cb: (data: Data) => void) { diff --git a/src/components/player/hooks/useSourceSelection.ts b/src/components/player/hooks/useSourceSelection.ts index b3cd5a8e..d1a50792 100644 --- a/src/components/player/hooks/useSourceSelection.ts +++ b/src/components/player/hooks/useSourceSelection.ts @@ -43,7 +43,7 @@ export function useEmbedScraping( const baseUrlMaker = makeProviderUrl(providerApiUrl); const conn = connectServerSideEvents( 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( 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 {