Skip to content

JSON::ParserError when an API gives 404 response with empty response body

When making a request for records that yields a 404, if the response body is empty, a JSON::ParserError is raised, without providing access to the response object or any other information.

Stacktrace: (Click to expand)
     JSON::ParserError:
       unexpected token at ''
     # .rvm/gems/ruby-3.1.2/gems/json-2.6.3/lib/json/common.rb:216:in `parse'
     # .rvm/gems/ruby-3.1.2/gems/json-2.6.3/lib/json/common.rb:216:in `parse'
     # .rvm/gems/ruby-3.1.2/gems/oauth2-2.0.9/lib/oauth2/response.rb:147:in `block in <top (required)>'
     # .rvm/gems/ruby-3.1.2/gems/oauth2-2.0.9/lib/oauth2/response.rb:87:in `parsed'
     # .rvm/gems/ruby-3.1.2/gems/oauth2-2.0.9/lib/oauth2/error.rb:13:in `initialize'
     # .rvm/gems/ruby-3.1.2/gems/oauth2-2.0.9/lib/oauth2/client.rb:138:in `new'
     # .rvm/gems/ruby-3.1.2/gems/oauth2-2.0.9/lib/oauth2/client.rb:138:in `request'
     # .rvm/gems/ruby-3.1.2/gems/oauth2-2.0.9/lib/oauth2/access_token.rb:150:in `request'

Ideally, this would just yield an empty response, or an OAuth2::Error with the response object so consumers can check the headers for a 404 (this was the behaviour in version 1.4), so this can be distinguished from any other scenarios producing a JSON parsing error.