Skip to content

Commit

Permalink
Should be spotless now.
Browse files Browse the repository at this point in the history
  • Loading branch information
trygu committed Dec 18, 2024
1 parent 7585224 commit 4ae1ed8
Show file tree
Hide file tree
Showing 5 changed files with 249 additions and 212 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
package fr.insee.onyxia.api.controller;

import jakarta.servlet.http.HttpServletRequest;
import java.net.URI;
import java.util.List;
import java.util.stream.Collectors;
import org.everit.json.schema.ValidationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ProblemDetail;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import jakarta.servlet.http.HttpServletRequest;
import org.everit.json.schema.ValidationException;

import java.net.URI;
import java.util.List;
import java.util.stream.Collectors;

@RestControllerAdvice
public class RestExceptionHandler {

// Helper method to create ProblemDetail
private ProblemDetail createProblemDetail(HttpStatus status, URI type, String title, String detail, String instancePath) {
private ProblemDetail createProblemDetail(
HttpStatus status, URI type, String title, String detail, String instancePath) {
ProblemDetail problemDetail = ProblemDetail.forStatus(status);
problemDetail.setType(type);
problemDetail.setTitle(title);
Expand All @@ -28,37 +27,38 @@ private ProblemDetail createProblemDetail(HttpStatus status, URI type, String ti

// AccessDeniedException handler
@ExceptionHandler(AccessDeniedException.class)
public ProblemDetail handleAccessDeniedException(AccessDeniedException exception, HttpServletRequest request) {
public ProblemDetail handleAccessDeniedException(
AccessDeniedException exception, HttpServletRequest request) {
return createProblemDetail(
HttpStatus.FORBIDDEN,
RestExceptionTypes.ACCESS_DENIED,
"Access Denied",
"You do not have permission to access this resource.",
request.getRequestURI()
);
request.getRequestURI());
}

// ValidationException handler
@ExceptionHandler(ValidationException.class)
public ProblemDetail handleValidationException(ValidationException ex, HttpServletRequest request) {
List<String> errors = ex.getCausingExceptions() != null && !ex.getCausingExceptions().isEmpty()
? ex.getCausingExceptions().stream()
.map(ValidationException::getMessage)
.collect(Collectors.toList())
: List.of(ex.getMessage()); // Fallback to the main exception message if causing exceptions are empty.
public ProblemDetail handleValidationException(
ValidationException ex, HttpServletRequest request) {
List<String> errors =
ex.getCausingExceptions() != null && !ex.getCausingExceptions().isEmpty()
? ex.getCausingExceptions().stream()
.map(ValidationException::getMessage)
.collect(Collectors.toList())
: List.of(ex.getMessage()); // Fallback to the main exception message if
// causing exceptions are empty.

ProblemDetail problemDetail = createProblemDetail(
HttpStatus.BAD_REQUEST,
RestExceptionTypes.VALIDATION_FAILED,
"Validation Failed",
"The request contains invalid data.",
request.getRequestURI()
);
ProblemDetail problemDetail =
createProblemDetail(
HttpStatus.BAD_REQUEST,
RestExceptionTypes.VALIDATION_FAILED,
"Validation Failed",
"The request contains invalid data.",
request.getRequestURI());

// Add 'errors' as a custom property
problemDetail.setProperty("errors", errors);
return problemDetail;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@
// These are mostly examples
public class RestExceptionTypes {
public static final URI ACCESS_DENIED = URI.create("urn:org:onyxia:api:error:access-denied");
public static final URI VALIDATION_FAILED = URI.create("urn:org:onyxia:api:error:validation-failed");
public static final URI INVALID_ARGUMENT = URI.create("urn:org:onyxia:api:error:invalid-argument");
public static final URI INSTALLATION_FAILURE = URI.create("urn:org:onyxia:api:error:installation-failure");
public static final URI NAMESPACE_NOT_FOUND = URI.create("urn:org:onyxia:api:error:namespace-not-found");
public static final URI HELM_LIST_FAILURE = URI.create("urn:org:onyxia:api:error:helm-list-failure");
public static final URI HELM_RELEASE_FETCH_FAILURE = URI.create("urn:org:onyxia:api:error:helm-release-fetch-failure");
public static final URI VALIDATION_FAILED =
URI.create("urn:org:onyxia:api:error:validation-failed");
public static final URI INVALID_ARGUMENT =
URI.create("urn:org:onyxia:api:error:invalid-argument");
public static final URI INSTALLATION_FAILURE =
URI.create("urn:org:onyxia:api:error:installation-failure");
public static final URI NAMESPACE_NOT_FOUND =
URI.create("urn:org:onyxia:api:error:namespace-not-found");
public static final URI HELM_LIST_FAILURE =
URI.create("urn:org:onyxia:api:error:helm-list-failure");
public static final URI HELM_RELEASE_FETCH_FAILURE =
URI.create("urn:org:onyxia:api:error:helm-release-fetch-failure");
public static final URI GENERIC_ERROR = URI.create("urn:org:onyxia:api:error:unknown-error");

private RestExceptionTypes() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
import org.springframework.http.ProblemDetail;

/**
* Custom exception class for propagating Kubernetes-related errors
* with structured ProblemDetail information.
* Custom exception class for propagating Kubernetes-related errors with structured ProblemDetail
* information.
*/
public class CustomKubernetesException extends RuntimeException {

private final ProblemDetail problemDetail;

/**
* Constructor to create a CustomKubernetesException.
*
*
* @param problemDetail The ProblemDetail containing error details
*/
public CustomKubernetesException(ProblemDetail problemDetail) {
Expand All @@ -22,7 +22,7 @@ public CustomKubernetesException(ProblemDetail problemDetail) {

/**
* Getter for the ProblemDetail object.
*
*
* @return ProblemDetail containing structured error details
*/
public ProblemDetail getProblemDetail() {
Expand Down
Loading

0 comments on commit 4ae1ed8

Please sign in to comment.