Skip to content

Read the error response body when the auth endpoint returns an error status - #382

Open
RaphaelFakhri wants to merge 1 commit into
pusher:masterfrom
RaphaelFakhri:fix-auth-error-response-body
Open

RaphaelFakhri wants to merge 1 commit into
pusher:masterfrom
RaphaelFakhri:fix-auth-error-response-body

Conversation

@RaphaelFakhri

Copy link
Copy Markdown

What does this PR do?

Fixes #261.

When the channel authorization or user authentication endpoint responds with a status of 400 or above, HttpURLConnection.getInputStream() throws (FileNotFoundException for 404, IOException: Server returned HTTP response code: 403 ... for other codes). The response body the server sent is lost, and the status check that follows the read never runs for these codes. The resulting AuthorizationFailureException or AuthenticationFailureException only carries the URL and status, not the reason the server gave.

This change in BaseHttpAuthClient:

  • Reads the status code first and reads the body from getErrorStream() for statuses of 400 and above.
  • Uses the response body as the exception message, as it already does for other non-2xx statuses.
  • Falls back to Server returned HTTP response code: <status> when the error response has no body.

Successful responses are handled as before. New tests in HttpChannelAuthorizerTest and HttpUserAuthenticatorTest start a local HttpServer that returns 403 with a JSON body and check that the exception message is that body. Both tests fail on master and pass with this change.

CHANGELOG

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.

FileNotFoundException thrown when server responds with 400 or above

1 participant