HTTPClient: don't assume error responses are JSON#2529
Merged
Conversation
When receiving 500 responses, `HTTPClient` was always calling `ErrorResponse.from(_ data: Data)`, which tried to decode the content as if it was JSON. However, if the response had something completely different, it would fail to decode. This wasn't really a bug because `HTTPClient` was still forwarding the right result, but now it will do so without even trying to parse it.
NachoSoto
commented
May 23, 2023
| ) | ||
| .toNot(containElementSatisfying(Self.entryCondition(message: message, level: level))) | ||
| .toNot( | ||
| containElementSatisfying(Self.entryCondition(message: message, level: level)), |
Contributor
Author
There was a problem hiding this comment.
This provides a better failed assertion message, similar to verifyMessageWasLogged.
NachoSoto
commented
May 23, 2023
|
|
||
| expect(MockSigning.requests).to(beEmpty()) | ||
|
|
||
| logger.verifyMessageWasNotLogged("Couldn't decode data from json") |
Contributor
Author
There was a problem hiding this comment.
This was failing.
tonidero
approved these changes
May 24, 2023
| private extension HTTPResponse where Body == Data { | ||
|
|
||
| func parseUnsuccessfulResponse() -> NetworkError { | ||
| let isJSON = self.value(forHeaderField: HTTPClient.ResponseHeader.contentType.rawValue) == "application/json" |
Contributor
There was a problem hiding this comment.
My guess is yes but should we confirm the response will always have this header? (When the server is up)
Contributor
Author
There was a problem hiding this comment.
If it doesn't we still produce an error, we just won't try to parse the specific backend error, so in practice it doesn't really matter that much.
NachoSoto
added a commit
that referenced
this pull request
May 25, 2023
When receiving 500 responses, `HTTPClient` was always calling `ErrorResponse.from(_ data: Data)`, which tried to decode the content as if it was JSON. However, if the response had something completely different, it would fail to decode. This wasn't really a bug because `HTTPClient` was still forwarding the right result, but now it will do so without even trying to parse it.
This was referenced May 31, 2023
NachoSoto
added a commit
that referenced
this pull request
Aug 17, 2023
NachoSoto
added a commit
that referenced
this pull request
Aug 17, 2023
MarkVillacampa
pushed a commit
that referenced
this pull request
Sep 6, 2023
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
When receiving 500 responses,
HTTPClientwas always callingErrorResponse.from(_ data: Data), which tried to decode the content as if it was JSON.However, if the response had something completely different, it would fail to decode.
This wasn't really a bug because
HTTPClientwas still forwarding the right result, but now it will do so without even trying to parse it.