Skip to content

Commit

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

async function requestWithRetry<T>(
requestOptions: RequestOptions & { url: string; allowNoResponse?: boolean },
retryCount: number,
): Promise<T & ExtendedResponse> {
try {
const options = {
Expand All @@ -82,7 +83,7 @@ async function requestWithRetry<T>(
}
return data
} catch (e: any) {
if (!e.response && !requestOptions.allowNoResponse) {
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 @@ -94,7 +95,7 @@ async function requestWithRetry<T>(
logDebug(e)

await delay(5000)
return requestWithRetry(requestOptions)
return requestWithRetry(requestOptions, retryCount ? 1 : retryCount++)
}
throw e
}
Expand Down Expand Up @@ -422,15 +423,16 @@ export class RingRestClient {
} catch (e: any) {
const response = e.response || {}

if (response.statusCode === 401) {
await this.refreshAuth()
return this.request(options)
}

if (response.statusCode === 504) {
// Gateway Timeout. These should be recoverable, but wait a few seconds just to be on the safe side
await delay(5000)
return this.request(options)
switch(response.statusCode) {
case 401: // Unauthorized
await this.refreshAuth()
return this.request(options)
case 500: // Service error
case 502: // Bad gateway
case 503: // Service Unavailable
case 504: // Gateway timeout
await delay(30000)
return this.request(options)
}

if (
Expand Down Expand Up @@ -484,7 +486,9 @@ export class RingRestClient {
logError(`Request to ${url} failed:`)
logError(e)
}
}

if (!options.allowNoResponse) {
throw e
}
}
Expand Down

0 comments on commit f518f37

Please sign in to comment.