Skip to content
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

Implement problem details #504

Merged
merged 2 commits into from
Oct 8, 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
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);
}
}