mirror of
https://github.com/movie-web/providers-api.git
synced 2025-09-13 10:13:26 +00:00
Add error handling to streaming
This commit is contained in:
47
src/index.ts
47
src/index.ts
@@ -88,6 +88,7 @@ app.get('/scrape', async (context) => {
|
||||
}
|
||||
|
||||
return streamSSE(context, async (stream) => {
|
||||
try {
|
||||
const output = await providers.runAll({
|
||||
media,
|
||||
events: {
|
||||
@@ -107,10 +108,20 @@ app.get('/scrape', async (context) => {
|
||||
});
|
||||
|
||||
if (output) {
|
||||
return await writeSSEEvent(stream, 'completed', output);
|
||||
await writeSSEEvent(stream, 'completed', output);
|
||||
return await stream.close();
|
||||
}
|
||||
|
||||
return await writeSSEEvent(stream, 'noOutput', '');
|
||||
await writeSSEEvent(stream, 'noOutput', '');
|
||||
return await stream.close();
|
||||
} catch (e: any) {
|
||||
await writeSSEEvent(stream, 'error', {
|
||||
name: e.name,
|
||||
message: e.message,
|
||||
stack: e.stack,
|
||||
});
|
||||
return await stream.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -145,6 +156,7 @@ app.get('/scrape/embed', async (context) => {
|
||||
}
|
||||
|
||||
return streamSSE(context, async (stream) => {
|
||||
try {
|
||||
const output = await providers.runEmbedScraper({
|
||||
id: embedInput.id,
|
||||
url: embedInput.url,
|
||||
@@ -156,14 +168,24 @@ app.get('/scrape/embed', async (context) => {
|
||||
});
|
||||
|
||||
if (output) {
|
||||
return await writeSSEEvent(stream, 'completed', output);
|
||||
await writeSSEEvent(stream, 'completed', output);
|
||||
return await stream.close();
|
||||
}
|
||||
|
||||
return await writeSSEEvent(stream, 'noOutput', '');
|
||||
await writeSSEEvent(stream, 'noOutput', '');
|
||||
return await stream.close();
|
||||
} catch (e: any) {
|
||||
await writeSSEEvent(stream, 'error', {
|
||||
name: e.name,
|
||||
message: e.message,
|
||||
stack: e.stack,
|
||||
});
|
||||
return await stream.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
app.get('/scrape/embed', async (context) => {
|
||||
app.get('/scrape/source', async (context) => {
|
||||
const queryParams = context.req.query();
|
||||
|
||||
const turnstileEnabled = Boolean(context.env?.TURNSTILE_ENABLED);
|
||||
@@ -194,6 +216,7 @@ app.get('/scrape/embed', async (context) => {
|
||||
}
|
||||
|
||||
return streamSSE(context, async (stream) => {
|
||||
try {
|
||||
const output = await providers.runSourceScraper({
|
||||
id: sourceInput.id,
|
||||
media: sourceInput,
|
||||
@@ -205,10 +228,20 @@ app.get('/scrape/embed', async (context) => {
|
||||
});
|
||||
|
||||
if (output) {
|
||||
return await writeSSEEvent(stream, 'completed', output);
|
||||
await writeSSEEvent(stream, 'completed', output);
|
||||
return await stream.close();
|
||||
}
|
||||
|
||||
return await writeSSEEvent(stream, 'noOutput', '');
|
||||
await writeSSEEvent(stream, 'noOutput', '');
|
||||
return await stream.close();
|
||||
} catch (e: any) {
|
||||
await writeSSEEvent(stream, 'error', {
|
||||
name: e.name,
|
||||
message: e.message,
|
||||
stack: e.stack,
|
||||
});
|
||||
return await stream.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
Reference in New Issue
Block a user