Skip to content
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

Parse JSON internally when content-type=json is missing. #17

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

gannan08
Copy link

@gannan08 gannan08 commented Dec 5, 2024

No description provided.

@codecov-commenter
Copy link

codecov-commenter commented Dec 5, 2024

Codecov Report

Attention: Patch coverage is 65.21739% with 8 lines in your changes missing coverage. Please review.

Project coverage is 89.61%. Comparing base (7f65af5) to head (77a97cd).

Files with missing lines Patch % Lines
lib/index.js 50.00% 8 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##              main      #17       +/-   ##
============================================
- Coverage   100.00%   89.61%   -10.39%     
============================================
  Files            2        3        +1     
  Lines           55       77       +22     
============================================
+ Hits            55       69       +14     
- Misses           0        8        +8     
Files with missing lines Coverage Δ
lib/logger.js 100.00% <100.00%> (ø)
lib/index.js 85.45% <50.00%> (-14.55%) ⬇️

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7f65af5...77a97cd. Read the comment docs.

return await result.json();
} catch(e) {
const {url} = result;
logger.error(`DataError: could not parse JSON from: "${url}"`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this something that should be logged? Would it be good enough to let calling code handle the error? Maybe add the URL to it.

}

try {
return await result.json();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this should be open ended and always try to parse arbitrary input. One specific use case is when you get application/octet-stream due to the server not knowing what to do (ex, on github pages with no extension). What other content types make sense to try and parse? If it's HTML or an image, better to not bother and let it fail. So maybe should refactor to something like as a special case:

if(result.headers.get('content-type') === 'application/octet-stream') {
  // try the parse
} else {
  // throw an unsupported content type error
}

(And consider using content-type npm package for parsing the header string.)

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.

3 participants