From d4db80789a1a8648d01f9a08d4f74f91e2d91b38 Mon Sep 17 00:00:00 2001 From: WebStreamr <210764791+webstreamr@users.noreply.github.com> Date: Tue, 13 May 2025 11:58:49 +0000 Subject: [PATCH] feat(handler): return handler issues as streams for better visibility --- src/controller/StreamController.ts | 52 ++++++++++++++++++------------ 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/src/controller/StreamController.ts b/src/controller/StreamController.ts index 5d30f03..a60b47b 100644 --- a/src/controller/StreamController.ts +++ b/src/controller/StreamController.ts @@ -1,4 +1,5 @@ import { Request, Response, Router } from 'express'; +import { Stream } from 'stremio-addon-sdk'; import { Handler } from '../handler'; import { Config, UrlResult } from '../types'; import bytes from 'bytes'; @@ -43,6 +44,8 @@ export class StreamController { return; } + const streams: Stream[] = []; + const urlResults: UrlResult[] = []; const handlerPromises = selectedHandlers.map(async (handler) => { if (!handler.contentTypes.includes(type)) { @@ -55,6 +58,11 @@ export class StreamController { urlResults.push(...(handlerUrlResults.filter(handlerUrlResult => handlerUrlResult !== undefined))); } catch (err) { + streams.push({ + name: 'WebStreamr', + title: `❌ Error with handler "${handler.id}". Please check the logs or create an issue if this persists.`, + ytId: 'E4WlUXrJgy4', + }); this.logger.error(`${handler.id} error: ` + err); } }); @@ -71,29 +79,31 @@ export class StreamController { this.logger.info(`Return ${urlResults.length} streams`); - const streams = urlResults.map((urlResult) => { - let name = 'WebStreamr'; - if (urlResult.height) { - name += ` ${urlResult.height}p`; - } + streams.push( + ...urlResults.map((urlResult) => { + let name = 'WebStreamr'; + if (urlResult.height) { + name += ` ${urlResult.height}p`; + } - let title = urlResult.label; - if (urlResult.bytes) { - title += ` | 💾 ${bytes.format(urlResult.bytes, { unitSeparator: ' ' })}`; - } - if (urlResult.countryCode) { - title += ` | ${flag(urlResult.countryCode)}`; - } + let title = urlResult.label; + if (urlResult.bytes) { + title += ` | 💾 ${bytes.format(urlResult.bytes, { unitSeparator: ' ' })}`; + } + if (urlResult.countryCode) { + title += ` | ${flag(urlResult.countryCode)}`; + } - return { - url: urlResult.url.toString(), - name, - title, - behaviourHints: { - group: `webstreamr-${urlResult.sourceId}`, - }, - }; - }); + return { + url: urlResult.url.toString(), + name, + title, + behaviourHints: { + group: `webstreamr-${urlResult.sourceId}`, + }, + }; + }), + ); res.send(JSON.stringify({ streams })); };