From 2932ccb7caf471d1e70551a71be1343451dca11a Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Thu, 13 Aug 2026 09:50:46 +0200 Subject: [PATCH] Resolve error when fetching dividends for date ranges without events --- .../yahoo-finance/yahoo-finance.service.ts | 29 ++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/apps/api/src/services/data-provider/yahoo-finance/yahoo-finance.service.ts b/apps/api/src/services/data-provider/yahoo-finance/yahoo-finance.service.ts index e7859e23a..da0da1949 100644 --- a/apps/api/src/services/data-provider/yahoo-finance/yahoo-finance.service.ts +++ b/apps/api/src/services/data-provider/yahoo-finance/yahoo-finance.service.ts @@ -22,6 +22,7 @@ import { import { Injectable, Logger } from '@nestjs/common'; import { DataSource, SymbolProfile } from '@prisma/client'; import { addDays, format, isSameDay } from 'date-fns'; +import { StatusCodes } from 'http-status-codes'; import { uniqBy } from 'lodash'; import YahooFinance from 'yahoo-finance2'; import { ChartResultArray } from 'yahoo-finance2/esm/src/modules/chart'; @@ -41,6 +42,9 @@ import { SearchQuoteNonYahoo } from 'yahoo-finance2/esm/src/modules/search'; @Injectable() export class YahooFinanceService implements DataProviderInterface { + private static readonly DELISTED_ERROR_MESSAGE = + 'No data found, symbol may be delisted'; + private readonly logger = new Logger(YahooFinanceService.name); private readonly yahooFinance = new YahooFinance({ @@ -107,12 +111,23 @@ export class YahooFinanceService implements DataProviderInterface { return response; } catch (error) { - this.logger.error( - `Could not get dividends for ${symbol} (${this.getName()}) from ${format( - from, - DATE_FORMAT - )} to ${format(to, DATE_FORMAT)}: [${error.name}] ${error.message}` - ); + const message = `Could not get dividends for ${symbol} (${this.getName()}) from ${format( + from, + DATE_FORMAT + )} to ${format(to, DATE_FORMAT)}`; + + if (error.message === YahooFinanceService.DELISTED_ERROR_MESSAGE) { + this.logger.warn( + `${message}: ${YahooFinanceService.DELISTED_ERROR_MESSAGE}` + ); + } else if ( + error.name === 'HTTPError' && + error.code === StatusCodes.TOO_MANY_REQUESTS + ) { + this.logger.warn(`${message}: Rate limit exceeded`); + } else { + this.logger.error(`${message}: [${error.name}] ${error.message}`); + } return {}; } @@ -155,7 +170,7 @@ export class YahooFinanceService implements DataProviderInterface { return response; } catch (error) { - if (error.message === 'No data found, symbol may be delisted') { + if (error.message === YahooFinanceService.DELISTED_ERROR_MESSAGE) { throw new AssetProfileDelistedError( `No data found, ${symbol} (${this.getName()}) may be delisted` );