Skip to content

Commit

Permalink
πŸ› Exception logging added. (#45)
Browse files Browse the repository at this point in the history
* πŸ› Exception logging added.

* 🎨 Spottless Camel Route formatting off.

* β™»οΈπŸ› Refactoring, exception logging added.

* β™»οΈπŸ› Refactoring, exception logging added.

* β™»οΈπŸ› Refactoring, exception logging added.

* β™»οΈπŸ› Refactoring, exception logging added.

* onException code cleaned up

* spotless

---------

Co-authored-by: martin.dietrich <[email protected]>
  • Loading branch information
sfi2022 and martind260 authored Jul 23, 2024
1 parent 897ff85 commit 9543e1a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 54 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@
</dependencies>
<configuration>
<java>
<toggleOffOn />
<includes>
<include>src/main/java/**/*.java</include> <!-- Check application code -->
<include>src/test/java/**/*.java</include> <!-- Check application tests code -->
Expand Down
56 changes: 29 additions & 27 deletions src/main/java/de/muenchen/mobidam/rest/S3Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,30 @@ public void configure() throws Exception {
/**
* GET /filesInFolder : Get S3 bucket object list
**/
// spotless:off
rest()
.get("/filesInFolder")
.description("Get S3 bucket object list")
.id("filesInFolderGetApi")
.produces("application/json")
.outType(BucketContentInner[].class)
.param()
.name("bucketName")
.type(RestParamType.query)
.required(true)
.description("Bucket name")
.name("bucketName")
.type(RestParamType.query)
.required(true)
.description("Bucket name")
.endParam()
.param()
.name("path")
.type(RestParamType.query)
.required(false)
.description("S3 path")
.name("path")
.type(RestParamType.query)
.required(false)
.description("S3 path")
.endParam()
.param()
.name("archived")
.type(RestParamType.query)
.required(false)
.description("Return archived files")
.name("archived")
.type(RestParamType.query)
.required(false)
.description("Return archived files")
.endParam()
.to("{{camel.route.common}}");

Expand All @@ -55,15 +56,15 @@ public void configure() throws Exception {
.outType(PresignedUrl.class)
.param()
.name("bucketName")
.type(RestParamType.query)
.required(true)
.description("Bucket name")
.type(RestParamType.query)
.required(true)
.description("Bucket name")
.endParam()
.param()
.name("objectName")
.type(RestParamType.query)
.required(true)
.description("Object name")
.name("objectName")
.type(RestParamType.query)
.required(true)
.description("Object name")
.endParam()
.to("{{camel.route.common}}");

Expand All @@ -75,18 +76,19 @@ public void configure() throws Exception {
.description("Move 'finished' file to archive.")
.id("moveFinishedFileToArchiveApi")
.param()
.name("bucketName")
.type(RestParamType.query)
.required(true)
.description("Bucket name")
.name("bucketName")
.type(RestParamType.query)
.required(true)
.description("Bucket name")
.endParam()
.param()
.name("objectName")
.type(RestParamType.query)
.required(true)
.description("Object name")
.name("objectName")
.type(RestParamType.query)
.required(true)
.description("Object name")
.endParam()
.to("{{camel.route.common}}");

}
// spotless:on
}
50 changes: 24 additions & 26 deletions src/main/java/de/muenchen/mobidam/s3/S3RouteBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import lombok.RequiredArgsConstructor;
import org.apache.camel.Exchange;
import org.apache.camel.LoggingLevel;
import org.apache.camel.ResolveEndpointFailedException;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.aws2.s3.AWS2S3Constants;
import org.apache.camel.component.aws2.s3.AWS2S3Operations;
Expand All @@ -33,35 +32,30 @@ public void configure() {
onException(S3Exception.class)
.handled(true)
.process(exchange -> {
var s3Exception = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, S3Exception.class);
logException(exchange, s3Exception);
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);
exchange.getMessage().setBody(ErrorResponseBuilder.build(400, s3Exception.getClass().getName()));
Exception exception = (Exception) exchange.getAllProperties().get(Exchange.EXCEPTION_CAUGHT);
// even with onException(S3Exception), the transported exception may still be sth. else:
if (((Throwable) exchange.getAllProperties().get(Exchange.EXCEPTION_CAUGHT)).getCause()instanceof S3Exception s3Exception) {
exception = s3Exception;
}
logException(exchange, exception);
var statusCode = HttpStatus.BAD_REQUEST.value();
if (exception instanceof S3Exception s3Exception) {
statusCode = s3Exception.statusCode();
}
exchange.getMessage().setBody(ErrorResponseBuilder.build(statusCode, exception.getClass().getName()));
exchange.getMessage().setHeader(Exchange.HTTP_RESPONSE_CODE, statusCode);
});

onException(Exception.class)
.handled(true)
.process(exchange -> {
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 {
Throwable exception = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Throwable.class);
logException(exchange, exception);
Throwable exception = (Throwable) exchange.getAllProperties().get(Exchange.EXCEPTION_CAUGHT);
logException(exchange, exception);
if (!(exchange.getMessage().getBody() instanceof ErrorResponse)) {
ErrorResponse res = ErrorResponseBuilder.build(HttpStatus.INTERNAL_SERVER_ERROR.value(), exception.getClass().getName());
exchange.getMessage().setBody(res);
}
exchange.getMessage().setHeader(Exchange.HTTP_RESPONSE_CODE, ((ErrorResponse) exchange.getMessage().getBody()).getStatus());
});

from("{{camel.route.common}}")
Expand Down Expand Up @@ -126,11 +120,15 @@ public void configure() {
}

private void logException(Exchange exchange, Throwable exception) {
log.error("Error occurred in route", exception);
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);
} else {
log.error("Error occurred in route", exception);
log.error(getFormattedArchiveException(exchange, exception.getMessage()));
}
}

private static String getFormattedArchiveException(Exchange exchange, String errorText) {
return String.format("Archive clean up (%s): %s", exchange.getProperty(Constants.ARCHIVE_ENTITY, MobidamArchive.class).toString(),
errorText);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
)
@EnableAutoConfiguration
@DirtiesContext
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@ActiveProfiles(TestConstants.SPRING_NO_SECURITY_PROFILE)
class S3ArchiveDeleteTest {

Expand Down

0 comments on commit 9543e1a

Please sign in to comment.