Commit 02dcd259 authored by Georg Mittendorfer's avatar Georg Mittendorfer

Log errors that get returned to client.

parent 1343af67
......@@ -119,7 +119,9 @@ public class IriApiHandler {
.onErrorResume(retryPredicate(), throwable -> retry(throwable, command))
.onErrorResume(retryPredicate(), throwable -> retry(throwable, command))
.onErrorResume(retryPredicate(), throwable -> retry(throwable, command))
.map(responseEntity -> postProcess(command, responseEntity));
.map(responseEntity -> postProcess(command, responseEntity))
.doOnError(t -> logException(t, command))
.doOnSuccess(responseEntity -> logIfError(responseEntity, command));
if (rateLimitOperations.isIpRateLimited(ip)) {
RateLimiter rateLimiter = rateLimitOperations.rateLimiter(ip, command.getCommand());
......@@ -167,14 +169,14 @@ public class IriApiHandler {
return Mono.error(new NoNodeAvailable(msg));
} else {
Node node = nodes.get();
logCall(node.getName(), command);
logCallToCommandLog(node.getName(), command);
command.getExecutionHistory().add(node.getName());
// TODO call multiple nodes
// get several nodes.
// flux zip run in parallel on another scheduler.
// handle result flat/map/then something return.
return node.call(command)
.doOnNext(responseEntity -> logResponseIfError(node.getName(), command, responseEntity))
.doOnNext(responseEntity -> logToCommandLogIfError(node.getName(), command, responseEntity))
.map(responseEntity -> {
HttpStatus httpStatus = responseEntity.getStatusCode();
// TODO add error handling for unsynced get node info
......@@ -192,7 +194,7 @@ public class IriApiHandler {
}
}
private void logCall(String nodeName, IriCommand command) {
private void logCallToCommandLog(String nodeName, IriCommand command) {
commandLogger.info("#A [{}] #IP [{}] #N [{}] #C [{}] #D [{}] #ID [{}] #UA [{}]",
"REQ",
command.getIp(),
......@@ -203,7 +205,7 @@ public class IriApiHandler {
command.getUserAgent());
}
private void logResponseIfError(String nodeName, IriCommand command, ResponseEntity<String> responseEntity) {
private void logToCommandLogIfError(String nodeName, IriCommand command, ResponseEntity<String> responseEntity) {
if (responseEntity.getStatusCode().isError()) {
commandLogger.warn("#A [{}] #IP [{}] #N [{}] #C [{}] #D [{} => {}] #ID [{}] #UA [{}]",
"ERR",
......@@ -217,6 +219,20 @@ public class IriApiHandler {
}
}
private void logIfError(ResponseEntity<String> responseEntity, IriCommand command) {
if (responseEntity.getStatusCode().isError()) {
logger.warn("Returning error status code. #IP [{}} #C [{}] #D [{} => {}]",
command.getIp(),
command.getCommand(),
responseEntity.getStatusCodeValue(),
responseEntity.getBody());
}
}
private void logException(Throwable t, IriCommand command) {
logger.warn("[{}] #[ERR] #IP [{}] #C [{}] #D [{}]", t.getClass().getSimpleName(), command.getIp(), command.getCommand(), t.getMessage());
}
private Predicate<Throwable> retryPredicate() {
return throwable -> !(throwable instanceof NoNodeAvailable);
}
......
......@@ -98,4 +98,24 @@ public class IriApiHandlerIT extends AbstractIriApiHandlerTest {
}
@Test
public void givenRetriesConsumedWhenExchangeThenReturnErrorResponse() {
for (int i = 0; i < 5; i++) {
prepareResponse(response -> response
.setHeader("Content-Type", "application/json")
.setResponseCode(400)
.setBody("Tips are not consistent"));
}
String response = util.postCommand(TestCommandFactory.getBalances())
.expectStatus().isBadRequest()
.expectBody(String.class).returnResult().getResponseBody();
logger.info(response);
assertThat(response).contains("Tips are not consistent");
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment