diff --git a/onyxia-api/src/main/java/fr/insee/onyxia/api/controller/RestExceptionHandler.java b/onyxia-api/src/main/java/fr/insee/onyxia/api/controller/RestExceptionHandler.java index e23c703b..813bb9b0 100644 --- a/onyxia-api/src/main/java/fr/insee/onyxia/api/controller/RestExceptionHandler.java +++ b/onyxia-api/src/main/java/fr/insee/onyxia/api/controller/RestExceptionHandler.java @@ -1,75 +1,36 @@ package fr.insee.onyxia.api.controller; -import fr.insee.onyxia.api.controller.exception.SchemaNotFoundException; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; import org.everit.json.schema.ValidationException; import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; +import org.springframework.http.ProblemDetail; import org.springframework.security.access.AccessDeniedException; import org.springframework.web.bind.annotation.ExceptionHandler; -import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; @RestControllerAdvice -public class RestExceptionHandler { +public class RestExceptionHandler extends ResponseEntityExceptionHandler { - @ResponseStatus(value = HttpStatus.FORBIDDEN) @ExceptionHandler(AccessDeniedException.class) - public void handleAccessDeniedException(Exception ignored) {} - - @ExceptionHandler(SchemaNotFoundException.class) - public ResponseEntity handleSchemaNotFoundException(SchemaNotFoundException ex) { - return new ResponseEntity<>(ex.getMessage(), HttpStatus.NOT_FOUND); + public ProblemDetail handleAccessDeniedException(Exception ignored) { + ProblemDetail problemDetail = ProblemDetail.forStatus(HttpStatus.FORBIDDEN); + problemDetail.setTitle("Access denied"); + return problemDetail; } @ExceptionHandler(ValidationException.class) - public ResponseEntity handleValidationException(ValidationException ex) { + public ProblemDetail handleValidationException(ValidationException ex) { List errors = ex.getCausingExceptions().stream() .map(ValidationException::getMessage) .collect(Collectors.toList()); - ErrorResponse errorResponse = - new ErrorResponse(HttpStatus.BAD_REQUEST.value(), "Validation failed", errors); - - return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST); - } - - // Define the ErrorResponse class within the GlobalExceptionHandler - public static class ErrorResponse { - private int status; - private String message; - private List errors; - - public ErrorResponse(int status, String message, List errors) { - this.status = status; - this.message = message; - this.errors = errors; - } - - public int getStatus() { - return status; - } - - public void setStatus(int status) { - this.status = status; - } - - public String getMessage() { - return message; - } - - public void setMessage(String message) { - this.message = message; - } - - public List getErrors() { - return errors; - } - - public void setErrors(List errors) { - this.errors = errors; - } + ProblemDetail problemDetail = ProblemDetail.forStatus(HttpStatus.BAD_REQUEST); + problemDetail.setProperties(Map.of("errors", errors)); + problemDetail.setTitle("Validation failed"); + return problemDetail; } } diff --git a/onyxia-api/src/main/java/fr/insee/onyxia/api/controller/exception/SchemaNotFoundException.java b/onyxia-api/src/main/java/fr/insee/onyxia/api/controller/exception/SchemaNotFoundException.java index 3387fe49..14257841 100644 --- a/onyxia-api/src/main/java/fr/insee/onyxia/api/controller/exception/SchemaNotFoundException.java +++ b/onyxia-api/src/main/java/fr/insee/onyxia/api/controller/exception/SchemaNotFoundException.java @@ -1,8 +1,11 @@ package fr.insee.onyxia.api.controller.exception; -public class SchemaNotFoundException extends RuntimeException { +import org.springframework.http.HttpStatus; +import org.springframework.web.ErrorResponseException; + +public class SchemaNotFoundException extends ErrorResponseException { public SchemaNotFoundException(String schemaName) { - super("Schema not found: " + schemaName); + super(HttpStatus.NOT_FOUND); } }