rollbar / rollbar.js

Error tracking and logging from Javascript to Rollbar
https://docs.rollbar.com/docs/javascript
MIT License
566 stars 212 forks source link

Handle Rollbar API 50x errors, had a 502 Bad Gateway and didn't fail gracefully #1092

Open saxaathlete opened 1 year ago

saxaathlete commented 1 year ago

Hi there,

Sometimes Rollbar API has difficulties and throws 502 or 500 errors but instead of JSON the response is html:

<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.17.9</center>
</body>
</html>

This type of response isn't handled and is failing when response is being parsed:

INTERNAL_ROLLBAR_ERROR SyntaxError: Unexpected token < in JSON at position 0
at Object.parse (<anonymous>)
at Object.jsonParse (/node_modules/rollbar/src/utility.js:356:25)
at _parseApiResponse (/node_modules/rollbar/src/server/transport.js:145:22)
at IncomingMessage.<anonymous> (/node_modules/rollbar/src/server/transport.js:120:5)

with new SyntaxError("JSON");:

726        if (state !== "ok" || (/[^\u0020\t\n\r]/.test(source))) {
727          throw (state instanceof SyntaxError)
728            ? state
729            : new SyntaxError("JSON");
729        }