Skip to content

Commit

Permalink
hot fix reddit
Browse files Browse the repository at this point in the history
  • Loading branch information
garrrikkotua committed Nov 11, 2024
1 parent c7919cb commit 4875a4c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 4875a4c

Please sign in to comment.