Skip to content

HTTPClient: don't assume error responses are JSON#2529

Merged
NachoSoto merged 2 commits into
mainfrom
http-client-error-non-json
May 24, 2023
Merged

HTTPClient: don't assume error responses are JSON#2529
NachoSoto merged 2 commits into
mainfrom
http-client-error-non-json

Conversation

@NachoSoto

Copy link
Copy Markdown
Contributor

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.

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 NachoSoto added the pr:fix A bug fix label May 23, 2023
@NachoSoto NachoSoto requested a review from a team May 23, 2023 18:18
)
.toNot(containElementSatisfying(Self.entryCondition(message: message, level: level)))
.toNot(
containElementSatisfying(Self.entryCondition(message: message, level: level)),

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This provides a better failed assertion message, similar to verifyMessageWasLogged.


expect(MockSigning.requests).to(beEmpty())

logger.verifyMessageWasNotLogged("Couldn't decode data from json")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This was failing.

private extension HTTPResponse where Body == Data {

func parseUnsuccessfulResponse() -> NetworkError {
let isJSON = self.value(forHeaderField: HTTPClient.ResponseHeader.contentType.rawValue) == "application/json"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

My guess is yes but should we confirm the response will always have this header? (When the server is up)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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 NachoSoto merged commit 94537a3 into main May 24, 2023
@NachoSoto NachoSoto deleted the http-client-error-non-json branch May 24, 2023 16:51
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr:fix A bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants