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

Use authentication error message from response body #15720

Merged
merged 1 commit into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,16 @@
public final class JsonResponse<T>
{
private final int statusCode;
private final String statusMessage;
private final Headers headers;
@Nullable
private final String responseBody;
private final boolean hasValue;
private final T value;
private final IllegalArgumentException exception;

private JsonResponse(int statusCode, String statusMessage, Headers headers, String responseBody)
private JsonResponse(int statusCode, Headers headers, String responseBody)
{
this.statusCode = statusCode;
this.statusMessage = statusMessage;
this.headers = requireNonNull(headers, "headers is null");
this.responseBody = requireNonNull(responseBody, "responseBody is null");

Expand All @@ -56,10 +54,9 @@ private JsonResponse(int statusCode, String statusMessage, Headers headers, Stri
this.exception = null;
}

private JsonResponse(int statusCode, String statusMessage, Headers headers, @Nullable String responseBody, @Nullable T value, @Nullable IllegalArgumentException exception)
private JsonResponse(int statusCode, Headers headers, @Nullable String responseBody, @Nullable T value, @Nullable IllegalArgumentException exception)
{
this.statusCode = statusCode;
this.statusMessage = statusMessage;
this.headers = requireNonNull(headers, "headers is null");
this.responseBody = responseBody;
this.value = value;
Expand All @@ -72,11 +69,6 @@ public int getStatusCode()
return statusCode;
}

public String getStatusMessage()
{
return statusMessage;
}

public Headers getHeaders()
{
return headers;
Expand Down Expand Up @@ -111,7 +103,6 @@ public String toString()
{
return toStringHelper(this)
.add("statusCode", statusCode)
.add("statusMessage", statusMessage)
.add("headers", headers.toMultimap())
.add("hasValue", hasValue)
.add("value", value)
Expand Down Expand Up @@ -158,9 +149,9 @@ public static <T> JsonResponse<T> execute(JsonCodec<T> codec, OkHttpClient clien
}
exception = new IllegalArgumentException(message, e);
}
return new JsonResponse<>(response.code(), response.message(), response.headers(), body, value, exception);
return new JsonResponse<>(response.code(), response.headers(), body, value, exception);
}
return new JsonResponse<>(response.code(), response.message(), response.headers(), responseBody.string());
return new JsonResponse<>(response.code(), response.headers(), responseBody.string());
}
catch (IOException e) {
throw new UncheckedIOException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ private RuntimeException requestFailedException(String task, Request request, Js
if (!response.hasValue()) {
if (response.getStatusCode() == HTTP_UNAUTHORIZED) {
return new ClientException("Authentication failed" +
Optional.ofNullable(response.getStatusMessage())
response.getResponseBody()
.map(message -> ": " + message)
.orElse(""));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,8 @@ public void testMultiplePasswordAuthenticatorsMessages()
.headers(Headers.of("Authorization", Credentials.basic(TEST_USER_LOGIN, "wrong_password")))
.build();
try (Response response = client.newCall(request).execute()) {
assertThat(response.message()).isEqualTo("Access Denied: Invalid credentials | Access Denied: Invalid credentials2");
assertThat(requireNonNull(response.body()).string())
.isEqualTo("Access Denied: Invalid credentials | Access Denied: Invalid credentials2");
}
}
}
Expand Down