From 6423a4718892b48ff4437c35b752778c5e544862 Mon Sep 17 00:00:00 2001 From: Welder Luz Date: Wed, 11 Jan 2023 10:23:45 -0300 Subject: [PATCH] Improve logging when creating salt exception --- .../client/impl/HttpAsyncClientImpl.java | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/suse/salt/netapi/client/impl/HttpAsyncClientImpl.java b/src/main/java/com/suse/salt/netapi/client/impl/HttpAsyncClientImpl.java index 76b5ce8e1..bef279014 100644 --- a/src/main/java/com/suse/salt/netapi/client/impl/HttpAsyncClientImpl.java +++ b/src/main/java/com/suse/salt/netapi/client/impl/HttpAsyncClientImpl.java @@ -15,10 +15,14 @@ import org.apache.http.entity.StringEntity; import org.apache.http.nio.client.HttpAsyncClient; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; import java.net.URI; import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionStage; +import java.util.stream.Collectors; /** * AsyncHttpClient implemented with Apache's HttpAsyncClient. @@ -118,7 +122,7 @@ public void completed(HttpResponse response) { future.completeExceptionally(e); } } else { - future.completeExceptionally(createSaltException(statusCode)); + future.completeExceptionally(createSaltException(response)); } } @@ -132,16 +136,27 @@ public void cancelled() { } /** - * Create the appropriate exception for the given HTTP status code. + * Create the appropriate exception for the given HTTP response. * - * @param statusCode HTTP status code + * @param response HTTP response * @return {@link SaltException} instance */ - private SaltException createSaltException(int statusCode) { + private SaltException createSaltException(HttpResponse response) { + int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_UNAUTHORIZED) { return new SaltUserUnauthorizedException( "Salt user does not have sufficient permissions"); } - return new SaltException("Response code: " + statusCode); + else { + String content = ""; + try { + content = new BufferedReader(new InputStreamReader(response.getEntity().getContent())) + .lines().parallel().collect(Collectors.joining("\n")); + } + catch (IOException e) { + // error trying to get the response body, nothing to do... + } + return new SaltException("Response code: " + statusCode + ". Response body:\n" + content); + } } }