Skip to content

Commit

Permalink
fix: retry oauth requests on 404 with known error codes
Browse files Browse the repository at this point in the history
If an alarm base station is offline, on cellular backup, or updating
firmware, the `connections` endpoint returns an error code.  When that
happens, we will just try again in 20 seconds.

Removing auth token delays because they weren't actually necessary
(thought they might be the cause of the 404 errors)
  • Loading branch information
dgreif committed Nov 8, 2018
1 parent bfdf516 commit 5b0be21
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions rest-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ const colors = require( 'colors/safe' )
const querystring = require( 'querystring' )

const loggableObject = color => object => colors[ color ]( JSON.stringify( object, null, 4 ))
const ringErrorCodes = {
7050: 'NO_ASSET',
7019: 'ASSET_OFFLINE',
7061: 'ASSET_CELL_BACKUP',
7062: 'UPDATING',
7063: 'MAINTENANCE'
}

module.exports = bottle => bottle.service( 'restClient', restClient,
'apiUrls',
Expand Down Expand Up @@ -101,9 +108,6 @@ function restClient( apiUrls, { email, password }, logger ) {
throw propagatedError( `could not get auth token for user ${email}`, requestError )
}

// delay to allow ring api time to store the new auth token
await delay( 1500 )

logger( 'got auth token', colors.green( authToken ))

return authToken
Expand Down Expand Up @@ -137,9 +141,6 @@ function restClient( apiUrls, { email, password }, logger ) {
throw propagatedError( `could not get a session token given auth token ${authToken}`, requestError )
}

// delay to allow ring api time to store the new session token
await delay( 1500 )

logger( 'got session token', colors.green( sessionToken ))

return sessionToken
Expand Down Expand Up @@ -204,11 +205,29 @@ function restClient( apiUrls, { email, password }, logger ) {
})

} catch ( e ) {
if ( e.causedBy && e.causedBy.response && e.causedBy.response.status === 401 ) {
const response = e.causedBy && e.causedBy.response

if ( response && response.status === 401 ) {
reauthenticate()
return this.oauthRequest( method, url, data )
}

if ( response && response.status === 404
&& response.data && Array.isArray( response.data.errors )) {
const
errors = response.data.errors,
errorText = errors.map( code => ringErrorCodes[ code ]).filter( x => x ).join( ', ' )

if ( errorText ) {
logger( colors.red( `http request failed. ${url} returned errors: (${errorText}). Trying again in 20 seconds` ))

await delay( 20000 )
return this.oauthRequest( method, url, data )
} else {
logger( colors.red( `http request failed. ${url} returned unknown errors: (${errors}). Trying again in 20 seconds` ))
}
}

throw e
}
}
Expand Down

0 comments on commit 5b0be21

Please sign in to comment.