Skip to content

Commit

Permalink
Implement problem details (#504)
Browse files Browse the repository at this point in the history
  • Loading branch information
olevitt authored Oct 8, 2024
1 parent 2be86c6 commit 086217e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -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<String> 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<ErrorResponse> handleValidationException(ValidationException ex) {
public ProblemDetail handleValidationException(ValidationException ex) {
List<String> 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<String> errors;

public ErrorResponse(int status, String message, List<String> 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<String> getErrors() {
return errors;
}

public void setErrors(List<String> errors) {
this.errors = errors;
}
ProblemDetail problemDetail = ProblemDetail.forStatus(HttpStatus.BAD_REQUEST);
problemDetail.setProperties(Map.of("errors", errors));
problemDetail.setTitle("Validation failed");
return problemDetail;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 086217e

Please sign in to comment.