stanford-oval / thingpedia-common-devices

Thingpedia interface code for commonly used devices
Other
38 stars 28 forks source link

Catch and handle rate-limit issues in Spotify #288

Closed gcampax closed 3 years ago

gcampax commented 3 years ago

We finally got a handle on the misterious error without message. In case of rate limiting, the server will reply with HTTP 503 and no request body.

Example from the scenario tests:

searching for  https://api.spotify.com/v1/search?q=artist%3Ataylor%20swift&type=artist&market=from_token&limit=5
HTTP request failed: 
Failed to invoke query { Error: Unexpected HTTP error 503 in request to https://api.spotify.com/v1/audio-features/?ids=0Jlcvv8IykzHaSmj49uNW8%2C4R2kfaDFhslZEMJqAFNpdd%2C2Eeur20xVqfUoM3Q7EFPFt%2C4pvb0WLRcMtbPGmtejJJ6y%2C1MgV7FIyNxIG7WzMRJV5HC%2C0ZNU020wNYvgW84iljPkPP%2C6KJqZcs9XDgVck7Lg9QOTC%2C3hUxzQpSfdDqwM3ZTFQY0K%2C7kt9e9LFSpN1zQtYEl19o1%2C2NmsngXHeC1GQ9wWrzhOMf%2C6VsvKPJ4xjVNKpI8VVZ3SV%2C2QDyYdZyhlP2fp79KZX8Bi%2C08fa9LFcFBTcilB3iq2e2A%2C5kI4eCXXzyuIUXjQra0Cxi%2C7MbT4I8qGntX4fMdqMQgke%2C6MWoRt97mnSTXZhu3ggi9C
    at IncomingMessage.res.on (/home/travis/build/stanford-oval/thingpedia-common-devices/node_modules/thingpedia/dist/helpers/http.js:134:35)
    at IncomingMessage.emit (events.js:203:15)
    at endReadableNT (_stream_readable.js:1145:12)
    at process._tickCallback (internal/process/next_tick.js:63:19)
  code: 503,
  url:
   'https://api.spotify.com/v1/audio-features/?ids=0Jlcvv8IykzHaSmj49uNW8%2C4R2kfaDFhslZEMJqAFNpdd%2C2Eeur20xVqfUoM3Q7EFPFt%2C4pvb0WLRcMtbPGmtejJJ6y%2C1MgV7FIyNxIG7WzMRJV5HC%2C0ZNU020wNYvgW84iljPkPP%2C6KJqZcs9XDgVck7Lg9QOTC%2C3hUxzQpSfdDqwM3ZTFQY0K%2C7kt9e9LFSpN1zQtYEl19o1%2C2NmsngXHeC1GQ9wWrzhOMf%2C6VsvKPJ4xjVNKpI8VVZ3SV%2C2QDyYdZyhlP2fp79KZX8Bi%2C08fa9LFcFBTcilB3iq2e2A%2C5kI4eCXXzyuIUXjQra0Cxi%2C7MbT4I8qGntX4fMdqMQgke%2C6MWoRt97mnSTXZhu3ggi9C',
  detail: '' }

According to spec, there might be a Retry-After header telling us how long to wait before issuing a new request. Otherwise, we should try again once after a fixed timeout. Failing that we throw an error telling that Spotify is temporarily not available and the user should try again later.

ryachen01 commented 3 years ago

Are we sure this is a rate limiting issue? According to https://developer.spotify.com/documentation/web-api/ 503 represents a service unavailable error and 429 represents rate limiting.

gcampax commented 3 years ago

Well, it smells like a rate limiting, but it has 503 error. I don't think we've seen a 429 error before? Either way, we should handle both sensibly, by retrying and then bailing.