node-chargify icon indicating copy to clipboard operation
node-chargify copied to clipboard

When Chargify sends a specific error, we may return a JSON parse error instead

Open natevw opened this issue 4 years ago • 2 comments

When trying to run the tests after updating dependencies, I discovered that Chargify's API is kind of broken. For example, when one doesn't have valid API token, Chargify return a response like:

status: '401 Unauthorized',
'content-type': 'application/json; charset=utf-8',
body: 'HTTP Basic: Access denied.\n',

But regardless because Chargify claims this is "application/json" we try to parse it as JSON and so the resulting error is just something like:

 Uncaught SyntaxError: Unexpected token H in JSON at position 0

instead of something more related to the 401 which would be more helpful.

I wonder if older versions of the request module used to flag non-2xx responses like this as an err to our callback? Or if this is a temporary bug on Chargify's side?

natevw avatar Jun 08 '21 18:06 natevw

(I'm not eager to spend my own time working around Chargify's broken responses but filing this as a known issue at least and would be willing to review a pull request.)

natevw avatar Jun 08 '21 18:06 natevw

Oh, maybe this this is a longstanding thing, since in my notes about migrating to Fermata instead I mentioned one of the benefits being:

improved callback arguments (an error is now provided for bad status codes)

natevw avatar Jun 08 '21 18:06 natevw