Skip to content

Commit 473b0a4

Browse files
committed
Explicitly handle DirtyTrick exception
1 parent b6b249a commit 473b0a4

File tree

14 files changed

+97
-8
lines changed

14 files changed

+97
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package bitxon.common.exception;
2+
3+
public class DirtyTrickException extends RuntimeException {
4+
5+
public DirtyTrickException(String message) {
6+
super("Dirty Trick: " + message);
7+
}
8+
}

dropwizard-app/src/main/java/bitxon/dropwizard/DropwizardApplication.java

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import bitxon.dropwizard.db.AccountDao;
66
import bitxon.dropwizard.db.AccountDaoHibernateImpl;
77
import bitxon.dropwizard.db.model.Account;
8+
import bitxon.dropwizard.errorhandler.DirtyTrickExceptionHandler;
89
import bitxon.dropwizard.errorhandler.JerseyViolationExceptionHandler;
910
import bitxon.dropwizard.errorhandler.ResourceNotFoundExceptionHandler;
1011
import bitxon.dropwizard.mapper.AccountMapper;
@@ -63,6 +64,7 @@ protected void configure() {
6364

6465
environment.jersey().register(AccountResource.class);
6566

67+
environment.jersey().register(DirtyTrickExceptionHandler.class);
6668
environment.jersey().register(JerseyViolationExceptionHandler.class);
6769
environment.jersey().register(ResourceNotFoundExceptionHandler.class);
6870
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package bitxon.dropwizard.errorhandler;
2+
3+
import bitxon.common.api.model.error.ErrorResponse;
4+
import bitxon.common.exception.DirtyTrickException;
5+
import jakarta.ws.rs.core.Response;
6+
import jakarta.ws.rs.ext.ExceptionMapper;
7+
import jakarta.ws.rs.ext.Provider;
8+
9+
import java.util.List;
10+
11+
@Provider
12+
public class DirtyTrickExceptionHandler implements ExceptionMapper<DirtyTrickException> {
13+
14+
@Override
15+
public Response toResponse(DirtyTrickException ex) {
16+
return Response
17+
.status(500)
18+
.entity(new ErrorResponse(List.of(ex.getMessage()))).build();
19+
}
20+
}

dropwizard-app/src/main/java/bitxon/dropwizard/resource/AccountResource.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import bitxon.common.api.model.Account;
77
import bitxon.common.api.model.MoneyTransfer;
8+
import bitxon.common.exception.DirtyTrickException;
89
import bitxon.common.exception.ResourceNotFoundException;
910
import bitxon.dropwizard.client.exchange.ExchangeClient;
1011
import bitxon.dropwizard.db.AccountDao;
@@ -82,7 +83,7 @@ public void transfer(@NotNull @Valid MoneyTransfer transfer,
8283
dao.save(sender);
8384

8485
if (FAIL_TRANSFER.equals(dirtyTrick)) {
85-
throw new RuntimeException("Error during money transfer");
86+
throw new DirtyTrickException("Error during money transfer");
8687
}
8788

8889
recipient.setMoneyAmount(recipient.getMoneyAmount() + (int)(transfer.moneyAmount() * exchangeRateValue));

dropwizard-app/src/test/java/bitxon/dropwizard/test/MoneyTransferDropwizardTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ void transferWithServerProblemDuringTransfer() {
7171
.when()
7272
.post("/accounts/transfers")
7373
.then()
74-
.statusCode(500);
74+
.statusCode(500)
75+
.body("errors", hasItem("Dirty Trick: Error during money transfer"));
7576
//@formatter:on
7677

7778
get("/accounts/" + senderId).then()

micronaut-app/src/main/java/bitxon/micronaut/controller/AccountController.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import bitxon.common.api.model.Account;
77
import bitxon.common.api.model.MoneyTransfer;
8+
import bitxon.common.exception.DirtyTrickException;
89
import bitxon.common.exception.ResourceNotFoundException;
910
import bitxon.micronaut.client.exchange.ExchangeClient;
1011
import bitxon.micronaut.db.AccountDao;
@@ -82,7 +83,7 @@ public void transfer(@Body @Valid MoneyTransfer transfer,
8283
dao.save(sender);
8384

8485
if (FAIL_TRANSFER.equals(dirtyTrick)) {
85-
throw new RuntimeException("Error during money transfer");
86+
throw new DirtyTrickException("Error during money transfer");
8687
}
8788

8889
recipient.setMoneyAmount(recipient.getMoneyAmount() + (int) (transfer.moneyAmount() * exchangeRateValue));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package bitxon.micronaut.errorhandler;
2+
3+
import bitxon.common.api.model.error.ErrorResponse;
4+
import bitxon.common.exception.DirtyTrickException;
5+
import io.micronaut.context.annotation.Requires;
6+
import io.micronaut.http.HttpRequest;
7+
import io.micronaut.http.HttpResponse;
8+
import io.micronaut.http.HttpStatus;
9+
import io.micronaut.http.annotation.Produces;
10+
import io.micronaut.http.server.exceptions.ExceptionHandler;
11+
import jakarta.inject.Singleton;
12+
13+
import java.util.List;
14+
15+
@Produces
16+
@Singleton
17+
@Requires(classes = {DirtyTrickException.class, ExceptionHandler.class})
18+
public class DirtyTrickExceptionHandler implements ExceptionHandler<DirtyTrickException, HttpResponse> {
19+
@Override
20+
public HttpResponse handle(HttpRequest request, DirtyTrickException ex) {
21+
return HttpResponse
22+
.status(HttpStatus.INTERNAL_SERVER_ERROR)
23+
.body(new ErrorResponse(List.of(ex.getMessage())));
24+
}
25+
}

micronaut-app/src/test/java/bitxon/micronaut/test/MoneyTransferMicronautTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ void transferWithServerProblemDuringTransfer() {
7272
.when()
7373
.post("/accounts/transfers")
7474
.then()
75-
.statusCode(500);
75+
.statusCode(500)
76+
.body("errors", hasItem("Dirty Trick: Error during money transfer"));
7677
//@formatter:on
7778

7879
RestAssured.get("/accounts/" + senderId).then()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package bitxon.quarkus.errorhandler;
2+
3+
import bitxon.common.api.model.error.ErrorResponse;
4+
import bitxon.common.exception.DirtyTrickException;
5+
import jakarta.ws.rs.core.Response;
6+
import jakarta.ws.rs.ext.ExceptionMapper;
7+
import jakarta.ws.rs.ext.Provider;
8+
9+
import java.util.List;
10+
11+
@Provider
12+
public class DirtyTrickExceptionHandler implements ExceptionMapper<DirtyTrickException> {
13+
14+
@Override
15+
public Response toResponse(DirtyTrickException ex) {
16+
return Response
17+
.status(500)
18+
.entity(new ErrorResponse(List.of(ex.getMessage()))).build();
19+
}
20+
}

quarkus-app/src/main/java/bitxon/quarkus/resource/AccountResource.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import bitxon.common.api.model.Account;
77
import bitxon.common.api.model.MoneyTransfer;
8+
import bitxon.common.exception.DirtyTrickException;
89
import bitxon.common.exception.ResourceNotFoundException;
910
import bitxon.quarkus.client.exchange.ExchangeClient;
1011
import bitxon.quarkus.db.AccountDao;
@@ -78,7 +79,7 @@ public void transfer(@Valid MoneyTransfer transfer,
7879
dao.save(sender);
7980

8081
if (FAIL_TRANSFER.equals(dirtyTrick)) {
81-
throw new RuntimeException("Error during money transfer");
82+
throw new DirtyTrickException("Error during money transfer");
8283
}
8384

8485
recipient.setMoneyAmount(recipient.getMoneyAmount() + (int)(transfer.moneyAmount() * exchangeRateValue));

quarkus-app/src/test/java/bitxon/quarkus/test/MoneyTransferQuarkusTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ void transferWithServerProblemDuringTransfer() {
7474
.when()
7575
.post("/accounts/transfers")
7676
.then()
77-
.statusCode(500);
77+
.statusCode(500)
78+
.body("errors", hasItem("Dirty Trick: Error during money transfer"));
7879
//@formatter:on
7980

8081
get("/accounts/" + senderId).then()

spring-app/src/main/java/bitxon/spring/controller/AccountController.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import bitxon.common.api.model.Account;
77
import bitxon.common.api.model.MoneyTransfer;
8+
import bitxon.common.exception.DirtyTrickException;
89
import bitxon.common.exception.ResourceNotFoundException;
910
import bitxon.spring.client.ExchangeClient;
1011
import bitxon.spring.db.AccountDao;
@@ -80,7 +81,7 @@ public void transfer(@Valid @RequestBody MoneyTransfer transfer,
8081
dao.save(sender);
8182

8283
if (FAIL_TRANSFER.equals(dirtyTrick)) {
83-
throw new RuntimeException("Error during money transfer");
84+
throw new DirtyTrickException("Error during money transfer");
8485
}
8586

8687
recipient.setMoneyAmount(recipient.getMoneyAmount() + (int) (transfer.moneyAmount() * exchangeRateValue));

spring-app/src/main/java/bitxon/spring/errorhandler/ErrorControllerAdvice.java

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package bitxon.spring.errorhandler;
22

33
import bitxon.common.api.model.error.ErrorResponse;
4+
import bitxon.common.exception.DirtyTrickException;
45
import bitxon.common.exception.ResourceNotFoundException;
56
import org.springframework.core.Ordered;
67
import org.springframework.core.annotation.Order;
@@ -25,6 +26,11 @@ public ResponseEntity<Object> handle(Exception ex) {
2526
return create(500, "Unknown error, see logs.");
2627
}
2728

29+
@ExceptionHandler(DirtyTrickException.class)
30+
public ResponseEntity<Object> handle(DirtyTrickException ex) {
31+
return create(500, ex.getMessage());
32+
}
33+
2834
@ExceptionHandler(ResourceNotFoundException.class)
2935
public ResponseEntity<Object> handle(ResourceNotFoundException ex) {
3036
return create(404, ex.getMessage());

spring-app/src/test/java/bitxon/spring/test/MoneyTransferSpringTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ void transferWithServerProblemDuringTransfer() {
7272
.when()
7373
.post("/accounts/transfers")
7474
.then()
75-
.statusCode(500);
75+
.statusCode(500)
76+
.body("errors", hasItem("Dirty Trick: Error during money transfer"));
7677
//@formatter:on
7778

7879
get("/accounts/" + senderId).then()

0 commit comments

Comments
 (0)