Error: SyntaxError: Unexpected end of JSON input when server response has no body but JSON content type
yjukaku opened this issue · comments
Our server responds to a certain endpoint with headers but no body.
Expected result:
No error is thrown by rollbar.js
Actual result:
The rollbar.js method scrubJson
fails on these responses because it tries to parse the empty body as JSON.
rollbar.js/src/browser/telemetry.js
Line 438 in a9c567c
Please reopen as this is now an issue again 😄
I’ll look into it! I too couldn’t get your test to fail, but could you spin up a minimal repo that shows the issue?
Yep, here's one written in the Fastify JS framework: https://glitch.com/edit/#!/boundless-separated-antimony
The POST to /
responds with nothing but the header Content-Type
set to application/json
. That makes Rollbar's scrubJson function error out, as you can see in the console on that page.
Sorry, but I'm unable to recreate the issue. I saw an error pop up before I changed to token to my personal projects token in the rollbar config. I recreated the same endpoint in a rust server as well for the nextjs example in the rollbar-react project and can't get what you're describing. The only time I saw a JSON error was when I tried parsing the response body explicitly in the promise response.
Perhaps a repo linked to on github with a docker file with the exact same server setup you've got would be easier? That would also be easier for us to fix than a glitch link as then we could link to our local repo for development.
I'm not sure why it's not reproducible for you using the glitch link, but here's a screencast from right now:
Using Chrome 99
Mac OS 12.2.1
https://user-images.githubusercontent.com/908365/158882611-5909ae77-8304-4ce5-bf4b-6476fb80c244.mov
Here's a fork of the react-rollbar Github repo with changes necessary to the nextjs server to trigger the error.
EDIT: Forgot to link 😆 https://github.com/yjukaku/rollbar-react
Ah, I wasn't specifying the request's headers as application/json, just the response! Thanks for being responsive and helping me reproduce!
I went ahead and opened another PR with the same change. I couldn't cause a test to fail either. I'll talk with another community member about merging it since it shouldn't be throwing an error, but I'll also note that the problem goes away when I prevented the server from incorrectly broadcasting that it is responding with JSON.
This is released in 2.25.0. https://github.com/rollbar/rollbar.js/releases/tag/v2.25.0