mirror of
https://github.com/movie-web/movie-web.git
synced 2025-01-01 12:26:18 +00:00
Improve error handling for providers api
This commit is contained in:
parent
ca2e20fdbc
commit
4847980947
|
@ -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";
|
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);
|
console.error("Failed to connect to SSE", err);
|
||||||
promReject(err);
|
promReject(err);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
eventSource.addEventListener("message", (ev) => {
|
||||||
|
if (!ev) {
|
||||||
|
eventSource.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setTimeout(() => {
|
||||||
|
promReject(new Error("SSE closed improperly"));
|
||||||
|
}, 1000);
|
||||||
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
promise: () => promise,
|
promise: () => promise,
|
||||||
on<Data>(event: string, cb: (data: Data) => void) {
|
on<Data>(event: string, cb: (data: Data) => void) {
|
||||||
|
|
|
@ -43,7 +43,7 @@ export function useEmbedScraping(
|
||||||
const baseUrlMaker = makeProviderUrl(providerApiUrl);
|
const baseUrlMaker = makeProviderUrl(providerApiUrl);
|
||||||
const conn = connectServerSideEvents<EmbedOutput>(
|
const conn = connectServerSideEvents<EmbedOutput>(
|
||||||
baseUrlMaker.scrapeEmbed(embedId, url),
|
baseUrlMaker.scrapeEmbed(embedId, url),
|
||||||
["completed"]
|
["completed", "noOutput"]
|
||||||
);
|
);
|
||||||
result = await conn.promise();
|
result = await conn.promise();
|
||||||
} else {
|
} else {
|
||||||
|
@ -107,7 +107,7 @@ export function useSourceScraping(sourceId: string | null, routerId: string) {
|
||||||
const baseUrlMaker = makeProviderUrl(providerApiUrl);
|
const baseUrlMaker = makeProviderUrl(providerApiUrl);
|
||||||
const conn = connectServerSideEvents<SourcererOutput>(
|
const conn = connectServerSideEvents<SourcererOutput>(
|
||||||
baseUrlMaker.scrapeSource(sourceId, scrapeMedia),
|
baseUrlMaker.scrapeSource(sourceId, scrapeMedia),
|
||||||
["completed"]
|
["completed", "noOutput"]
|
||||||
);
|
);
|
||||||
result = await conn.promise();
|
result = await conn.promise();
|
||||||
} else {
|
} else {
|
||||||
|
@ -151,7 +151,7 @@ export function useSourceScraping(sourceId: string | null, routerId: string) {
|
||||||
result.embeds[0].embedId,
|
result.embeds[0].embedId,
|
||||||
result.embeds[0].url
|
result.embeds[0].url
|
||||||
),
|
),
|
||||||
["completed"]
|
["completed", "noOutput"]
|
||||||
);
|
);
|
||||||
embedResult = await conn.promise();
|
embedResult = await conn.promise();
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in a new issue