From 607a9a0aaf72d415f1284475c531026ce279ea41 Mon Sep 17 00:00:00 2001 From: Oliver Siegmar Date: Thu, 28 Mar 2024 18:49:21 +0100 Subject: [PATCH] cleanup code --- .../java/de/siegmar/logbackgelf/GelfHttpAppender.java | 11 ++++++----- .../de/siegmar/logbackgelf/GelfHttpAppenderTest.java | 5 +++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/main/java/de/siegmar/logbackgelf/GelfHttpAppender.java b/src/main/java/de/siegmar/logbackgelf/GelfHttpAppender.java index 231d41e..e681ff4 100644 --- a/src/main/java/de/siegmar/logbackgelf/GelfHttpAppender.java +++ b/src/main/java/de/siegmar/logbackgelf/GelfHttpAppender.java @@ -19,6 +19,9 @@ package de.siegmar.logbackgelf; +import static java.net.HttpURLConnection.HTTP_ACCEPTED; +import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR; + import java.io.IOException; import java.net.URI; import java.net.http.HttpClient; @@ -40,8 +43,6 @@ @SuppressWarnings("checkstyle:ClassFanOutComplexity") public class GelfHttpAppender extends UnsynchronizedAppenderBase { - private static final int HTTP_BAD_REQUEST = 400; - private static final int HTTP_INTERNAL_SERVER_ERROR = 500; private static final int DEFAULT_CONNECT_TIMEOUT = 15_000; private static final int DEFAULT_REQUEST_TIMEOUT = 5_000; private static final int DEFAULT_MAX_RETRIES = 2; @@ -260,15 +261,15 @@ private int sendRequest(final HttpRequest request) throws IOException, Interrupt final HttpResponse response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); final int statusCode = response.statusCode(); - if (statusCode >= HTTP_INTERNAL_SERVER_ERROR) { + if (statusCode >= HTTP_INTERNAL_ERROR) { // Throw exception for server errors (retry) throw new IllegalStateException(String.format("Error sending message via %s. Code: %s; Message: %s", getUri(), response.statusCode(), response.body())); } - if (statusCode >= HTTP_BAD_REQUEST) { + if (statusCode != HTTP_ACCEPTED) { // Don't throw exception for client errors (no retry) - addError(String.format("Error sending message via %s. Code: %s; Message: %s", + addError(String.format("Invalid response from %s. Code: %s; Message: %s", getUri(), response.statusCode(), response.body())); } diff --git a/src/test/java/de/siegmar/logbackgelf/GelfHttpAppenderTest.java b/src/test/java/de/siegmar/logbackgelf/GelfHttpAppenderTest.java index 7c6e82d..e4cf878 100644 --- a/src/test/java/de/siegmar/logbackgelf/GelfHttpAppenderTest.java +++ b/src/test/java/de/siegmar/logbackgelf/GelfHttpAppenderTest.java @@ -19,8 +19,9 @@ package de.siegmar.logbackgelf; -import static com.github.tomakehurst.wiremock.client.WireMock.ok; +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; import static com.github.tomakehurst.wiremock.client.WireMock.post; +import static java.net.HttpURLConnection.HTTP_ACCEPTED; import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson; import static org.awaitility.Awaitility.await; @@ -69,7 +70,7 @@ void simple() { } private static RequestPattern gelfRequest() { - return WIRE_MOCK.stubFor(post("/gelf").willReturn(ok())) + return WIRE_MOCK.stubFor(post("/gelf").willReturn(aResponse().withStatus(HTTP_ACCEPTED))) .getRequest(); }