Watcher: Fix wrong logging in reporting attachment parser (elastic/x-pack-elasticsearch#1900)

The logging shows a wrong HTTP response status code from a previous
request. In addition the body now also gets logged, as debugging
is impossible otherwise.

Original commit: elastic/x-pack-elasticsearch@cc998cd587
This commit is contained in:
Alexander Reelsen 2017-07-04 13:01:14 +02:00 committed by GitHub
parent fd518ea020
commit fff72256a5
2 changed files with 11 additions and 5 deletions

View File

@ -136,16 +136,18 @@ public class ReportingAttachmentParser implements EmailAttachmentParser<Reportin
logger.trace("Watch[{}] reporting[{}] pdf is not ready, polling in [{}] again", context.watch().id(), attachment.id(),
TimeValue.timeValueMillis(sleepMillis));
} else if (response.status() >= 400) {
String body = response.body() != null ? response.body().utf8ToString() : null;
throw new ElasticsearchException("Watch[{}] reporting[{}] Error when polling pdf from host[{}], port[{}], " +
"method[{}], path[{}], status[{}]", context.watch().id(), attachment.id(), request.host(), request.port(),
request.method(), request.path(), reportGenerationResponse.status());
"method[{}], path[{}], status[{}], body[{}]", context.watch().id(), attachment.id(), request.host(),
request.port(), request.method(), request.path(), response.status(), body);
} else if (response.status() == 200) {
return new Attachment.Bytes(attachment.id(), BytesReference.toBytes(response.body()),
response.contentType(), attachment.inline());
} else {
String body = response.body() != null ? response.body().utf8ToString() : null;
String message = LoggerMessageFormat.format("", "Watch[{}] reporting[{}] Unexpected status code host[{}], port[{}], " +
"method[{}], path[{}], status[{}]", context.watch().id(), attachment.id(), request.host(), request.port(),
request.method(), request.path(), reportGenerationResponse.status());
"method[{}], path[{}], status[{}], body[{}]", context.watch().id(), attachment.id(), request.host(),
request.port(), request.method(), request.path(), response.status(), body);
throw new IllegalStateException(message);
}
}

View File

@ -240,9 +240,10 @@ public class ReportingAttachmentParserTests extends ESTestCase {
}
public void testPollingRequestIsError() throws Exception {
boolean hasBody = randomBoolean();
when(httpClient.execute(any(HttpRequest.class)))
.thenReturn(new HttpResponse(200, "{\"path\":\"whatever\"}"))
.thenReturn(new HttpResponse(403));
.thenReturn(new HttpResponse(403, hasBody ? "no permissions" : null));
ReportingAttachment attachment =
new ReportingAttachment("foo", "http://www.example.org/", randomBoolean(), TimeValue.timeValueMillis(1), 10, null, null);
@ -250,6 +251,9 @@ public class ReportingAttachmentParserTests extends ESTestCase {
ElasticsearchException e = expectThrows(ElasticsearchException.class,
() -> reportingAttachmentParser.toAttachment(createWatchExecutionContext(), Payload.EMPTY, attachment));
assertThat(e.getMessage(), containsString("Error when polling pdf"));
if (hasBody) {
assertThat(e.getMessage(), containsString("body[no permissions]"));
}
}
public void testPollingRequestRetryIsExceeded() throws Exception {