Skip to content

Commit

Permalink
Explicitly handle DirtyTrick exception
Browse files Browse the repository at this point in the history
  • Loading branch information
bitxon committed Jun 27, 2024
1 parent b6b249a commit 473b0a4
Show file tree
Hide file tree
Showing 14 changed files with 97 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package bitxon.common.exception;

public class DirtyTrickException extends RuntimeException {

public DirtyTrickException(String message) {
super("Dirty Trick: " + message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import bitxon.dropwizard.db.AccountDao;
import bitxon.dropwizard.db.AccountDaoHibernateImpl;
import bitxon.dropwizard.db.model.Account;
import bitxon.dropwizard.errorhandler.DirtyTrickExceptionHandler;
import bitxon.dropwizard.errorhandler.JerseyViolationExceptionHandler;
import bitxon.dropwizard.errorhandler.ResourceNotFoundExceptionHandler;
import bitxon.dropwizard.mapper.AccountMapper;
Expand Down Expand Up @@ -63,6 +64,7 @@ protected void configure() {

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

environment.jersey().register(DirtyTrickExceptionHandler.class);
environment.jersey().register(JerseyViolationExceptionHandler.class);
environment.jersey().register(ResourceNotFoundExceptionHandler.class);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package bitxon.dropwizard.errorhandler;

import bitxon.common.api.model.error.ErrorResponse;
import bitxon.common.exception.DirtyTrickException;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.ExceptionMapper;
import jakarta.ws.rs.ext.Provider;

import java.util.List;

@Provider
public class DirtyTrickExceptionHandler implements ExceptionMapper<DirtyTrickException> {

@Override
public Response toResponse(DirtyTrickException ex) {
return Response
.status(500)
.entity(new ErrorResponse(List.of(ex.getMessage()))).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import bitxon.common.api.model.Account;
import bitxon.common.api.model.MoneyTransfer;
import bitxon.common.exception.DirtyTrickException;
import bitxon.common.exception.ResourceNotFoundException;
import bitxon.dropwizard.client.exchange.ExchangeClient;
import bitxon.dropwizard.db.AccountDao;
Expand Down Expand Up @@ -82,7 +83,7 @@ public void transfer(@NotNull @Valid MoneyTransfer transfer,
dao.save(sender);

if (FAIL_TRANSFER.equals(dirtyTrick)) {
throw new RuntimeException("Error during money transfer");
throw new DirtyTrickException("Error during money transfer");
}

recipient.setMoneyAmount(recipient.getMoneyAmount() + (int)(transfer.moneyAmount() * exchangeRateValue));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ void transferWithServerProblemDuringTransfer() {
.when()
.post("/accounts/transfers")
.then()
.statusCode(500);
.statusCode(500)
.body("errors", hasItem("Dirty Trick: Error during money transfer"));
//@formatter:on

get("/accounts/" + senderId).then()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import bitxon.common.api.model.Account;
import bitxon.common.api.model.MoneyTransfer;
import bitxon.common.exception.DirtyTrickException;
import bitxon.common.exception.ResourceNotFoundException;
import bitxon.micronaut.client.exchange.ExchangeClient;
import bitxon.micronaut.db.AccountDao;
Expand Down Expand Up @@ -82,7 +83,7 @@ public void transfer(@Body @Valid MoneyTransfer transfer,
dao.save(sender);

if (FAIL_TRANSFER.equals(dirtyTrick)) {
throw new RuntimeException("Error during money transfer");
throw new DirtyTrickException("Error during money transfer");
}

recipient.setMoneyAmount(recipient.getMoneyAmount() + (int) (transfer.moneyAmount() * exchangeRateValue));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package bitxon.micronaut.errorhandler;

import bitxon.common.api.model.error.ErrorResponse;
import bitxon.common.exception.DirtyTrickException;
import io.micronaut.context.annotation.Requires;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.HttpStatus;
import io.micronaut.http.annotation.Produces;
import io.micronaut.http.server.exceptions.ExceptionHandler;
import jakarta.inject.Singleton;

import java.util.List;

@Produces
@Singleton
@Requires(classes = {DirtyTrickException.class, ExceptionHandler.class})
public class DirtyTrickExceptionHandler implements ExceptionHandler<DirtyTrickException, HttpResponse> {
@Override
public HttpResponse handle(HttpRequest request, DirtyTrickException ex) {
return HttpResponse
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(new ErrorResponse(List.of(ex.getMessage())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ void transferWithServerProblemDuringTransfer() {
.when()
.post("/accounts/transfers")
.then()
.statusCode(500);
.statusCode(500)
.body("errors", hasItem("Dirty Trick: Error during money transfer"));
//@formatter:on

RestAssured.get("/accounts/" + senderId).then()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package bitxon.quarkus.errorhandler;

import bitxon.common.api.model.error.ErrorResponse;
import bitxon.common.exception.DirtyTrickException;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.ExceptionMapper;
import jakarta.ws.rs.ext.Provider;

import java.util.List;

@Provider
public class DirtyTrickExceptionHandler implements ExceptionMapper<DirtyTrickException> {

@Override
public Response toResponse(DirtyTrickException ex) {
return Response
.status(500)
.entity(new ErrorResponse(List.of(ex.getMessage()))).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import bitxon.common.api.model.Account;
import bitxon.common.api.model.MoneyTransfer;
import bitxon.common.exception.DirtyTrickException;
import bitxon.common.exception.ResourceNotFoundException;
import bitxon.quarkus.client.exchange.ExchangeClient;
import bitxon.quarkus.db.AccountDao;
Expand Down Expand Up @@ -78,7 +79,7 @@ public void transfer(@Valid MoneyTransfer transfer,
dao.save(sender);

if (FAIL_TRANSFER.equals(dirtyTrick)) {
throw new RuntimeException("Error during money transfer");
throw new DirtyTrickException("Error during money transfer");
}

recipient.setMoneyAmount(recipient.getMoneyAmount() + (int)(transfer.moneyAmount() * exchangeRateValue));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ void transferWithServerProblemDuringTransfer() {
.when()
.post("/accounts/transfers")
.then()
.statusCode(500);
.statusCode(500)
.body("errors", hasItem("Dirty Trick: Error during money transfer"));
//@formatter:on

get("/accounts/" + senderId).then()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import bitxon.common.api.model.Account;
import bitxon.common.api.model.MoneyTransfer;
import bitxon.common.exception.DirtyTrickException;
import bitxon.common.exception.ResourceNotFoundException;
import bitxon.spring.client.ExchangeClient;
import bitxon.spring.db.AccountDao;
Expand Down Expand Up @@ -80,7 +81,7 @@ public void transfer(@Valid @RequestBody MoneyTransfer transfer,
dao.save(sender);

if (FAIL_TRANSFER.equals(dirtyTrick)) {
throw new RuntimeException("Error during money transfer");
throw new DirtyTrickException("Error during money transfer");
}

recipient.setMoneyAmount(recipient.getMoneyAmount() + (int) (transfer.moneyAmount() * exchangeRateValue));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bitxon.spring.errorhandler;

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

@ExceptionHandler(DirtyTrickException.class)
public ResponseEntity<Object> handle(DirtyTrickException ex) {
return create(500, ex.getMessage());
}

@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity<Object> handle(ResourceNotFoundException ex) {
return create(404, ex.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ void transferWithServerProblemDuringTransfer() {
.when()
.post("/accounts/transfers")
.then()
.statusCode(500);
.statusCode(500)
.body("errors", hasItem("Dirty Trick: Error during money transfer"));
//@formatter:on

get("/accounts/" + senderId).then()
Expand Down

0 comments on commit 473b0a4

Please sign in to comment.