Skip to content

Commit

Permalink
Log fetch errors only on retries
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Sightler committed Oct 28, 2024
1 parent 3f0a649 commit 8582f6f
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions packages/ring-client-api/rest-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ async function responseToError(response: Response) {

async function requestWithRetry<T>(
requestOptions: RequestOptions & { url: string; allowNoResponse?: boolean },
isRetry = false,
retryCount = 0,
): Promise<T & ExtendedResponse> {
try {
if (requestOptions.json || requestOptions.responseType === 'json') {
Expand Down Expand Up @@ -154,18 +154,18 @@ async function requestWithRetry<T>(
return data
} catch (e: any) {
if (!e.response && !requestOptions.allowNoResponse) {
if (isRetry) {
const message = 'Error: ' + e.message +
(e.cause?.message ? ', Cause: ' + e.cause.message : '') +
(e.cause?.code ? ', Code: ' + e.cause.code : '');
if (retryCount > 0) {
let detailedError = `Error: ${e.message}`
detailedError += e.cause?.message ? `, Cause: ${e.cause.message}` : ''
detailedError += e.cause?.code ? `, Code: ${e.cause.code}` : ''
logError(
`Failed to reach Ring server at ${requestOptions.url}. ${message}. Trying again in 5 seconds...`,
`Retry #${retryCount} failed to reach Ring server at ${requestOptions.url}. ${detailedError}. Trying again in 5 seconds...`,
)
if (e.message.includes('NGHTTP2_ENHANCE_YOUR_CALM')) {
logError(
`There is a known issue with your current NodeJS version (${process.version}). Please see https://github.com/dgreif/ring/wiki/NGHTTP2_ENHANCE_YOUR_CALM-Error for details`,
)
} else if ((e.message.includes('fetch is not defined'))) {
} else if (e.message.includes('fetch is not defined')) {
logError(
`Your current NodeJS version (${process.version}) is too old to support this plugin. Please upgrade to the latest LTS version of NodeJS.`,
)
Expand All @@ -174,7 +174,7 @@ async function requestWithRetry<T>(
}

await delay(5000)
return requestWithRetry(requestOptions, true)
return requestWithRetry(requestOptions, retryCount + 1)
}
throw e
}
Expand Down

0 comments on commit 8582f6f

Please sign in to comment.