improved error handling

This commit is contained in:
helori_ollivier
2025-11-24 14:47:01 +01:00
parent 0671b4bac2
commit 22ff458756
9 changed files with 76 additions and 5 deletions

View File

@@ -1,6 +1,6 @@
package bzh.risotto.galactik.api.exceptions; package bzh.risotto.galactik.api.exceptions;
public class CorpoNotFoundException extends RuntimeException { public class CorpoNotFoundException extends NotFoundException {
public CorpoNotFoundException(Long id) { public CorpoNotFoundException(Long id) {
super("Corpo: " + id + " not found"); super("Corpo: " + id + " not found");

View File

@@ -1,6 +1,6 @@
package bzh.risotto.galactik.api.exceptions; package bzh.risotto.galactik.api.exceptions;
public class FacilityNotFoundException extends RuntimeException { public class FacilityNotFoundException extends NotFoundException {
public FacilityNotFoundException(Long id) { public FacilityNotFoundException(Long id) {
super("Facilty: " + id + " not found"); super("Facilty: " + id + " not found");

View File

@@ -6,6 +6,10 @@ public class NotEnoughtRessourceException extends RuntimeException {
super("corpo: " + corpoId + " has not enough resource of type: " + ressourceId); super("corpo: " + corpoId + " has not enough resource of type: " + ressourceId);
} }
public NotEnoughtRessourceException(String ressource, String corpo) {
super("corpo: " + corpo + " has not enough resource of type: " + ressource);
}
public NotEnoughtRessourceException(String msg) { public NotEnoughtRessourceException(String msg) {
super(msg); super(msg);
} }

View File

@@ -0,0 +1,7 @@
package bzh.risotto.galactik.api.exceptions;
public class NotFoundException extends RuntimeException {
public NotFoundException(String message) {
super(message);
}
}

View File

@@ -1,6 +1,6 @@
package bzh.risotto.galactik.api.exceptions; package bzh.risotto.galactik.api.exceptions;
public class RessourceNotFoundException extends RuntimeException { public class RessourceNotFoundException extends NotFoundException {
public RessourceNotFoundException(int ressourceId) { public RessourceNotFoundException(int ressourceId) {
super("Ressource: " + ressourceId + " not found"); super("Ressource: " + ressourceId + " not found");

View File

@@ -1,6 +1,6 @@
package bzh.risotto.galactik.api.exceptions; package bzh.risotto.galactik.api.exceptions;
public class ShipNotFoundException extends RuntimeException { public class ShipNotFoundException extends NotFoundException {
public ShipNotFoundException(long shipId) { public ShipNotFoundException(long shipId) {
super("Ship: " + shipId + " not found"); super("Ship: " + shipId + " not found");

View File

@@ -89,7 +89,7 @@ public class CorpoService {
} }
if (ressource.getQuantity() <= value) { if (ressource.getQuantity() <= value) {
throw new NotEnoughtRessourceException(ressourceId, corpoId); throw new NotEnoughtRessourceException(ressource.getType().getName(), corpo.getName());
} }
else { else {
res = true; res = true;

View File

@@ -0,0 +1,32 @@
package bzh.risotto.galactik.api.utils;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ProblemDetail;
import org.springframework.lang.NonNull;
import org.springframework.web.ErrorResponse;
public class CustomErrorResponse implements ErrorResponse {
HttpStatus status;
ProblemDetail detail;
public CustomErrorResponse(HttpStatus status, ProblemDetail detail) {
super();
this.status = status;
this.detail = detail;
}
@Override
@NonNull
public HttpStatusCode getStatusCode() {
return this.status;
}
@Override
@NonNull
public ProblemDetail getBody() {
return this.detail;
}
}

View File

@@ -0,0 +1,28 @@
package bzh.risotto.galactik.api.utils;
import bzh.risotto.galactik.api.exceptions.*;
import org.springframework.beans.factory.parsing.Location;
import org.springframework.beans.factory.parsing.Problem;
import org.springframework.http.HttpStatus;
import org.springframework.http.ProblemDetail;
import org.springframework.http.ResponseEntity;
import org.springframework.web.ErrorResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
@ControllerAdvice
public class CustomExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(NotFoundException.class)
public ProblemDetail handleNotFoundException(NotFoundException ex, WebRequest request) {
return ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND, ex.getMessage());
}
@ExceptionHandler(NotEnoughtRessourceException.class)
public ProblemDetail handleNotEnoughtResourceException(NotEnoughtRessourceException ex, WebRequest request) {
return ProblemDetail.forStatusAndDetail(HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage());
}
}