diff --git a/services/libs/integrations/src/integrations/reddit/api/errorHandler.ts b/services/libs/integrations/src/integrations/reddit/api/errorHandler.ts index bcb91bcc83..4b8143a12e 100644 --- a/services/libs/integrations/src/integrations/reddit/api/errorHandler.ts +++ b/services/libs/integrations/src/integrations/reddit/api/errorHandler.ts @@ -13,23 +13,23 @@ export const handleRedditError = ( ) => { const logger = ctx.log - let url = config.url - if (config.params) { + let url = config?.url + if (config?.params) { const queryParams: string[] = [] for (const [key, value] of Object.entries(config.params)) { // eslint-disable-next-line @typescript-eslint/no-explicit-any queryParams.push(`${key}=${encodeURIComponent(value as any)}`) } - url = `${config.url}?${queryParams.join('&')}` + url = `${config?.url}?${queryParams.join('&')}` } - if (err && err.response && err.response.status === 429) { + if (err && err?.response && err?.response?.status === 429) { logger.warn('Reddit API rate limit exceeded') let rateLimitResetSeconds = 60 - if (err.response.headers['x-ratelimit-reset']) { - rateLimitResetSeconds = parseInt(err.response.headers['x-ratelimit-reset'], 10) + if (err?.response?.headers?.['x-ratelimit-reset']) { + rateLimitResetSeconds = parseInt(err?.response?.headers?.['x-ratelimit-reset'], 10) } return new RateLimitError(rateLimitResetSeconds, url, err) diff --git a/services/libs/integrations/src/integrations/reddit/api/getPosts.ts b/services/libs/integrations/src/integrations/reddit/api/getPosts.ts index 231b24eb51..c16e402e8d 100644 --- a/services/libs/integrations/src/integrations/reddit/api/getPosts.ts +++ b/services/libs/integrations/src/integrations/reddit/api/getPosts.ts @@ -51,9 +51,9 @@ async function getPosts( // If ctx.onboarding is false, check the last post's date if (!ctx.onboarding) { const fiveHoursAgo = Date.now() / 1000 - 5 * 60 * 60 // in seconds - const lastPost = response.data.children[response.data.children.length - 1] + const lastPost = response?.data?.children?.[response?.data?.children?.length - 1] - if (lastPost && lastPost.data.created < fiveHoursAgo) { + if (lastPost && lastPost?.data?.created < fiveHoursAgo) { // If the last post is older than 5 hours, set 'after' to null so the next API call won't go deeper ctx.log.info( lastPost,