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

Improve logging when creating salt exception #305

Merged
merged 1 commit into from
Jan 11, 2023
Merged
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 @@ -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.
Expand Down Expand Up @@ -118,7 +122,7 @@ public void completed(HttpResponse response) {
future.completeExceptionally(e);
}
} else {
future.completeExceptionally(createSaltException(statusCode));
future.completeExceptionally(createSaltException(response));
}
}

Expand All @@ -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);
}
}
}