-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
18 additions
and
54 deletions.
There are no files selected for viewing
65 changes: 13 additions & 52 deletions
65
onyxia-api/src/main/java/fr/insee/onyxia/api/controller/RestExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
7 changes: 5 additions & 2 deletions
7
...a-api/src/main/java/fr/insee/onyxia/api/controller/exception/SchemaNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |