-
Couldn't load subscription status.
- Fork 26
fix: prevent parsing empty responses from throwing #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
ESLint Summary View Full Report
Report generated by eslint-plus-action |
src/utils.ts
Outdated
| try { | ||
| json = await resp.json() | ||
| } catch { | ||
| // ignore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we check here if the header?.get("content-length") is greater than 0 and throw in that case?
It would mean that something was returned in the response which is not valid.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will not throw if the "content-length" is "0".
Co-authored-by: katspaugh <381895+katspaugh@users.noreply.github.com>
src/utils.ts
Outdated
| if (resp.headers && resp.headers.get('content-length') === '0') { | ||
| throw new Error('Empty response. ' + resp.statusText) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should be like this:
| if (resp.headers && resp.headers.get('content-length') === '0') { | |
| throw new Error('Empty response. ' + resp.statusText) | |
| if (resp.headers && resp.headers.get('content-length') !== '0') { | |
| throw new Error('Response has invalid format: ' + resp.text()) |
Otherwise we would throw if a response is empty (which we want to avoid with this PR)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah wait. After calling resp.json() we cannot call resp.text() anymore. So i would probably just throw like this:
throw new Error('Response has invalid format: ')
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. I've adjusted it as suggested.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good now :) Let's gooo
What it solves
Empty respones from thorwing on parse.
How this PR fixes it
When
json()is called on thefetchresponse, it is caught. It would otherwise throw if there is no response JSON to parse.The endpoints in question which caused this error are the proposal/confirmation of Safe messages.