Skip to content

Commit

Permalink
Use authentication error message from response body
Browse files Browse the repository at this point in the history
All versions of Trino send the error message in the response body.
The reason phrase does not exist in HTTP/2.
  • Loading branch information
electrum committed Jan 14, 2023
1 parent d72c95d commit 31bdffa
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 15 deletions.
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

0 comments on commit 31bdffa

Please sign in to comment.