feat: better error-handling for non-JSON responses #538
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
🧰 Changes
Right now, if we receive an error response from the API when running the
openapi
command, we have some not-so-great error handling where if the response body isn't JSON, we basically just write it off as a timeout error:rdme/src/cmds/openapi.js
Lines 111 to 129 in 862d400
Our debug logs in this situation aren't helpful either: since we blindly use
res.json()
to parse the response body, our debug logs consistently return an unhelpfulUnexpected token < in JSON
error.In this PR, our approach to parsing the response from our API has been improved to better allow us to understand non-JSON response bodies. We now do some smarter inspection of the response to determine whether or not it's a timeout. It probably is in most cases, but now we can actually inspect the response body and determine that.
In order to do this, I refactored our generic
handleRes
handler to dynamically handle JSON and non-JSON responses better. I was able to eliminate some duplicate code and theopenapi
command now leverages useshandleRes
for error-handling.🧬 QA & Testing
Do tests pass and do the changes look good?
If you want to try this out with an OAS file that timeouts consistently, check out this comment. I confirmed locally with that file that the timeout error is thrown properly.
I also confirmed that the debug logs now properly log non-JSON response bodies properly: