Skip to content

Commit

Permalink
Fix: prevent crashing when tvdb is down
Browse files Browse the repository at this point in the history
  • Loading branch information
cbackas committed May 24, 2024
1 parent 6f5a6f3 commit 8016af4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/lib/shows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ export async function checkForAiringEpisodes (): Promise<void> {
})

for (const show of shows) {
await updateEpisodes(show.imdbId, show.tvdbId)
try {
await updateEpisodes(show.imdbId, show.tvdbId)
} catch (error) {
console.error(`Error updating episodes for ${show.name} (${show.imdbId})`)
}
}

console.info('== Finished checking all shows for airing episodes ==')
Expand Down
23 changes: 18 additions & 5 deletions src/lib/tvdb.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios, { type AxiosRequestConfig } from 'axios'
import axios, { AxiosError, type AxiosRequestConfig } from 'axios'
import { type SearchByRemoteIdResult, type SearchResult, type SeriesExtendedRecord } from '../interfaces/tvdb.generated'

if (process.env.TVDB_API_KEY === undefined) throw new Error('TVDB_API_KEY is not defined')
Expand All @@ -18,11 +18,24 @@ async function getToken (): Promise<typeof token> {
token = response.data.data.token
return response.data.data.token
} catch (error) {
console.error('Error Getting TVDB Token:', error)
logPossibleAxiosError(error, 'Getting TVDB Token')
}
return undefined
}

function logPossibleAxiosError (error: unknown, errorPrefix: string): void {
if (error instanceof AxiosError) {
console.error(`Error ${errorPrefix}:`, {
url: error.config?.url,
code: error.code,
data: error.response?.data,
status: error.response?.status
})
} else {
console.error(`Unexpected Error ${errorPrefix}:`, error)
}
}

async function axiosOptions (): Promise<AxiosRequestConfig<any>> {
const token = await getToken()
if (token == null) throw new Error("Failed to get TVDB token, couldn't build axios options")
Expand Down Expand Up @@ -61,7 +74,7 @@ export async function getSeries (tvdbId: number): Promise<SeriesExtendedRecord |

return response.data?.data
} catch (error) {
console.error('Error Getting Extended Show Data:', error)
logPossibleAxiosError(error, 'Getting Extended Show Data')
}
return undefined
}
Expand All @@ -75,7 +88,7 @@ async function searchSeriesByImdbId (imdbId: string): Promise<SearchByRemoteIdRe

return response.data.data?.at(0)
} catch (error) {
console.error('Error Searching Series by IMDB ID:', error)
logPossibleAxiosError(error, 'Searching Series by IMDB ID')
}
return undefined
}
Expand All @@ -91,7 +104,7 @@ async function searchSeriesByName (query: string): Promise<SearchResult[] | unde
if (searchResult == null || searchResult[0].tvdb_id == null) return undefined
return searchResult
} catch (error) {
console.error('Error Searching Series:', error)
logPossibleAxiosError(error, 'Searching Series')
}
return undefined
}
Expand Down

0 comments on commit 8016af4

Please sign in to comment.