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
Lines changed: 8 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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
}
Lines changed: 20 additions & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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));
Lines changed: 25 additions & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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()
Lines changed: 20 additions & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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));

0 commit comments

Comments
 (0)