Skip to content

Conversation

@iamacook
Copy link
Contributor

@iamacook iamacook commented Dec 1, 2022

What it solves

Empty respones from thorwing on parse.

How this PR fixes it

When json() is called on the fetch response, 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.

@iamacook iamacook requested review from katspaugh and schmanu December 1, 2022 09:07
@iamacook iamacook self-assigned this Dec 1, 2022
@github-actions
Copy link

github-actions bot commented Dec 1, 2022

ESLint Summary View Full Report

Annotations are provided inline on the Files Changed tab. You can also see all annotations that were generated on the annotations page.

Type Occurrences Fixable
Errors 0 0
Warnings 0 0
Ignored 0 N/A
  • Result: ✅ success
  • Annotations: 0 total

Report generated by eslint-plus-action

src/utils.ts Outdated
try {
json = await resp.json()
} catch {
// ignore
Copy link
Contributor

@schmanu schmanu Dec 1, 2022

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.

Copy link
Contributor Author

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".

@iamacook iamacook requested review from katspaugh and schmanu December 1, 2022 10:27
@iamacook iamacook requested a review from katspaugh December 1, 2022 12:11
Co-authored-by: katspaugh <381895+katspaugh@users.noreply.github.com>
@iamacook iamacook requested a review from katspaugh December 1, 2022 12:38
src/utils.ts Outdated
Comment on lines 64 to 65
if (resp.headers && resp.headers.get('content-length') === '0') {
throw new Error('Empty response. ' + resp.statusText)
Copy link
Contributor

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:

Suggested change
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)

Copy link
Contributor

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: ')

Copy link
Contributor Author

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.

@iamacook iamacook requested a review from schmanu December 1, 2022 16:54
Copy link
Contributor

@schmanu schmanu left a 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

@iamacook iamacook merged commit dfcc0bc into main Dec 2, 2022
@iamacook iamacook deleted the json-response branch December 2, 2022 08:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants