Skip to content

Commit

Permalink
cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
osiegmar committed Mar 28, 2024
1 parent bd9ec95 commit 607a9a0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
11 changes: 6 additions & 5 deletions src/main/java/de/siegmar/logbackgelf/GelfHttpAppender.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -40,8 +43,6 @@
@SuppressWarnings("checkstyle:ClassFanOutComplexity")
public class GelfHttpAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {

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;
Expand Down Expand Up @@ -260,15 +261,15 @@ private int sendRequest(final HttpRequest request) throws IOException, Interrupt
final HttpResponse<String> 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()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}

Expand Down

0 comments on commit 607a9a0

Please sign in to comment.