From 5cf6e131ebef141eb816f2933471d2d669f1eb62 Mon Sep 17 00:00:00 2001 From: Roger Zhao Date: Fri, 22 Nov 2024 14:33:26 -0500 Subject: [PATCH] Changed error handling on related query --- .../src/tools/google_trends.ts | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/libs/langchain-community/src/tools/google_trends.ts b/libs/langchain-community/src/tools/google_trends.ts index 08ceae35dc49e..461dd9594661d 100644 --- a/libs/langchain-community/src/tools/google_trends.ts +++ b/libs/langchain-community/src/tools/google_trends.ts @@ -106,20 +106,23 @@ export class GoogleTrendsAPI extends Tool { } ); + let rising = []; + let top = []; if (!relatedRes.ok) { - throw new Error( + console.error( `Error fetching related queries from SerpAPI: ${relatedRes.statusText}` ); + } else { + const relatedDict = await relatedRes.json(); + rising = + relatedDict.related_queries?.rising?.map( + (result: any) => result.query + ) ?? []; + top = + relatedDict.related_queries?.top?.map((result: any) => result.query) ?? + []; } - const relatedDict = await relatedRes.json(); - const rising = - relatedDict.related_queries?.rising?.map((result: any) => result.query) ?? - []; - const top = - relatedDict.related_queries?.top?.map((result: any) => result.query) ?? - []; - const doc = [ `Query: ${query}`, `Date From: ${startDate[0]} ${startDate[1]}, ${startDate[2]}`,