Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tsightler committed Nov 5, 2023
1 parent f518f37 commit c007ee6
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions packages/ring-client-api/rest-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ export interface ExtendedResponse {

async function requestWithRetry<T>(
requestOptions: RequestOptions & { url: string; allowNoResponse?: boolean },
retryCount: number,
retryCount?: number,
): Promise<T & ExtendedResponse> {
retryCount = retryCount || 0
try {
const options = {
...defaultRequestOptions,
Expand All @@ -83,7 +84,7 @@ async function requestWithRetry<T>(
}
return data
} catch (e: any) {
if (!e.response && !requestOptions.allowNoResponse && retryCount >= 3) {
if (!e.response && !requestOptions.allowNoResponse && retryCount % 3) {
logError(
`Failed to reach Ring server at ${requestOptions.url}. ${e.message}. Trying again in 5 seconds...`,
)
Expand All @@ -95,7 +96,7 @@ async function requestWithRetry<T>(
logDebug(e)

await delay(5000)
return requestWithRetry(requestOptions, retryCount ? 1 : retryCount++)
return requestWithRetry(requestOptions, retryCount++)
}
throw e
}
Expand Down Expand Up @@ -486,10 +487,7 @@ export class RingRestClient {
logError(`Request to ${url} failed:`)
logError(e)
}
}

if (!options.allowNoResponse) {
throw e
return response
}
}

Expand Down

0 comments on commit c007ee6

Please sign in to comment.