Skip to content

Feature/explicit exception #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions common-api/build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
plugins {
id "io.freefair.lombok" version "6.5.0.3"
id 'java'
id 'java-library'
}

group = 'bitxon.common'
version = '1.0-SNAPSHOT'
sourceCompatibility = '17'

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

repositories {
mavenCentral()
Expand Down
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);
}
}
8 changes: 6 additions & 2 deletions common-wiremock/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
plugins {
id 'java'
id 'java-library'
}

group = 'bitxon.common'
version = '1.0-SNAPSHOT'
sourceCompatibility = '17'

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
6 changes: 5 additions & 1 deletion dropwizard-app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ plugins {

group = 'bitxon.dropwizard'
version = '1.0-SNAPSHOT'
sourceCompatibility = '17'

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

repositories {
mavenCentral()
Expand Down
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
7 changes: 5 additions & 2 deletions micronaut-app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ plugins {

group = 'bitxon.micronaut'
version = '1.0-SNAPSHOT'
sourceCompatibility = '17'
targetCompatibility = '17'

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

repositories {
mavenCentral()
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
6 changes: 5 additions & 1 deletion quarkus-app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ ext.set("quarkus.package.type", "uber-jar")

group = 'bitxon.quarkus'
version = '1.0-SNAPSHOT'
sourceCompatibility = '17'

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

repositories {
mavenCentral()
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
6 changes: 5 additions & 1 deletion spring-app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ plugins {

group = 'bitxon.spring'
version = '1.0-SNAPSHOT'
sourceCompatibility = '17'

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

repositories {
mavenCentral()
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
Loading