Skip to content

Commit

Permalink
♻️🐛 Refactoring, exception logging added.
Browse files Browse the repository at this point in the history
  • Loading branch information
sfi2022 committed Jul 18, 2024
1 parent c4cfe14 commit 1592af7
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions src/main/java/de/muenchen/mobidam/s3/S3RouteBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ public void configure() {
.handled(true)
.process(exchange -> {
var s3Exception = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, S3Exception.class);
logException(exchange, s3Exception);
logException(exchange, s3Exception.getMessage());
exchange.getMessage().setBody(ErrorResponseBuilder.build(s3Exception.statusCode(), s3Exception.getClass().getName()));
});

onException(ResolveEndpointFailedException.class)
.handled(true)
.process(exchange -> {
var s3Exception = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, ResolveEndpointFailedException.class);
logException(exchange, s3Exception);
logException(exchange, s3Exception.getMessage());
exchange.getMessage().setBody(ErrorResponseBuilder.build(400, s3Exception.getClass().getName()));
});

Expand All @@ -52,15 +52,11 @@ public void configure() {
if (exchange.getMessage().getBody()instanceof ErrorResponse res) {
exchange.getMessage().setHeader(Exchange.HTTP_RESPONSE_CODE, res.getStatus());
if (exchange.getIn().getHeader(Constants.CAMEL_SERVLET_CONTEXT_PATH).equals(Constants.ARCHIVE_ENTITY)) {
log.error(
String.format("Archive clean up (%s): %s", exchange.getProperty(Constants.ARCHIVE_ENTITY, MobidamArchive.class).toString(),
res.getError()));
} else {
log.error(exchange.getIn().getBody().toString());
logException(exchange, res.getError());
}
} else {
Throwable exception = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Throwable.class);
logException(exchange, exception);
logException(exchange, exception.getMessage());
ErrorResponse res = ErrorResponseBuilder.build(HttpStatus.INTERNAL_SERVER_ERROR.value(), exception.getClass().getName());
exchange.getMessage().setBody(res);
}
Expand Down Expand Up @@ -127,12 +123,13 @@ public void configure() {

}

private void logException(Exchange exchange, Throwable exception) {
private void logException(Exchange exchange, String errorMessage) {
if (exchange.getIn().getHeader(Constants.CAMEL_SERVLET_CONTEXT_PATH).equals(Constants.ARCHIVE_ENTITY)) {
log.error(String.format("Archive clean up (%s)", exchange.getProperty(Constants.ARCHIVE_ENTITY, MobidamArchive.class).toString()),
exception);
log.error(String.format("Archive clean up (%s): %s", exchange.getProperty(Constants.ARCHIVE_ENTITY, MobidamArchive.class).toString(), errorMessage));
} else {
log.error("Error occurred in route", exception);
log.error("Error occurred in route with message '{}'.", errorMessage);
}
}


}

0 comments on commit 1592af7

Please sign in to comment.