chore: handle NotFoundError / 404 inside source and cache it also

This commit is contained in:
WebStreamr 2025-09-01 10:53:59 +00:00
parent 285746a4a9
commit 4d05ac859f
No known key found for this signature in database
3 changed files with 14 additions and 7 deletions

View file

@ -1,5 +1,6 @@
import TTLCache from '@isaacs/ttlcache';
import { ContentType } from 'stremio-addon-sdk';
import { NotFoundError } from '../error';
import { Context, CountryCode } from '../types';
import { Id } from '../utils';
@ -35,7 +36,17 @@ export abstract class Source {
return this.sourceResultCache.get(id.id) as SourceResult[];
}
const sourceResults = await this.handleInternal(ctx, type, id);
let sourceResults: SourceResult[];
try {
sourceResults = await this.handleInternal(ctx, type, id);
} catch (error) {
if (error instanceof NotFoundError) {
sourceResults = [];
} else {
throw error;
}
}
this.sourceResultCache.set(id.id, sourceResults);
return sourceResults;

View file

@ -192,7 +192,7 @@ export class Fetcher {
}
private determineCacheTtl(status: number, policy: CachePolicy, init?: CustomRequestInit): number {
if (status === 200) {
if (status === 200 || status === 404) {
return Math.max(policy.timeToLive(), init?.minCacheTtl ?? this.MIN_CACHE_TTL);
}

View file

@ -1,7 +1,7 @@
import bytes from 'bytes';
import { ContentType, Stream } from 'stremio-addon-sdk';
import winston from 'winston';
import { logErrorAndReturnNiceString, NotFoundError } from '../error';
import { logErrorAndReturnNiceString } from '../error';
import { ExtractorRegistry } from '../extractor';
import { Source } from '../source';
import { Context, Format, UrlResult } from '../types';
@ -56,10 +56,6 @@ export class StreamResolver {
urlResults.push(...sourceUrlResults.flat());
} catch (error) {
if (error instanceof NotFoundError) {
return;
}
sourceErrorOccurred = true;
if (showErrors(ctx.config)) {