Exception handling #82
@ -1,5 +1,9 @@
|
||||
package se.su.dsv.studentportalen.bff.exception;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.HttpStatus;
|
||||
@ -9,11 +13,6 @@ import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Centralized exception handler for all REST controllers.
|
||||
* Returns RFC 7807 Problem Details for all error responses.
|
||||
@ -21,7 +20,9 @@ import java.util.stream.Collectors;
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(GlobalExceptionHandler.class);
|
||||
private static final Logger LOG = LoggerFactory.getLogger(
|
||||
GlobalExceptionHandler.class
|
||||
);
|
||||
|
||||
/**
|
||||
* Handles Jakarta Bean Validation errors (missing/invalid fields).
|
||||
@ -29,14 +30,22 @@ public class GlobalExceptionHandler {
|
||||
*/
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
public ProblemDetail handleValidation(MethodArgumentNotValidException ex) {
|
||||
Map<String, List<String>> violations = ex.getBindingResult().getFieldErrors().stream()
|
||||
.collect(Collectors.groupingBy(
|
||||
Map<String, List<String>> violations = ex
|
||||
.getBindingResult()
|
||||
.getFieldErrors()
|
||||
.stream()
|
||||
.collect(
|
||||
Collectors.groupingBy(
|
||||
FieldError::getField,
|
||||
Collectors.mapping(
|
||||
error -> error.getDefaultMessage() != null ? error.getDefaultMessage() : "Invalid value",
|
||||
error ->
|
||||
error.getDefaultMessage() != null
|
||||
? error.getDefaultMessage()
|
||||
: "Invalid value",
|
||||
Collectors.toList()
|
||||
)
|
||||
));
|
||||
)
|
||||
);
|
||||
|
||||
LOG.warn("Validation failed: {}", violations);
|
||||
|
||||
@ -76,7 +85,9 @@ public class GlobalExceptionHandler {
|
||||
public ProblemDetail handleUnexpected(Exception ex) {
|
||||
LOG.error("Unexpected error", ex);
|
||||
|
||||
ProblemDetail problem = ProblemDetail.forStatus(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
ProblemDetail problem = ProblemDetail.forStatus(
|
||||
HttpStatus.INTERNAL_SERVER_ERROR
|
||||
);
|
||||
problem.setType(URI.create("studentportalen:internal-error"));
|
||||
problem.setTitle("An unexpected error occurred");
|
||||
problem.setDetail("Please try again later");
|
||||
|
||||
@ -1,9 +1,8 @@
|
||||
package se.su.dsv.studentportalen.bff.exception;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
/**
|
||||
* Exception for validation errors, including field-level violations.
|
||||
@ -17,7 +16,10 @@ public class ValidationException extends StudentportalenException {
|
||||
this(message, Map.of());
|
||||
}
|
||||
|
||||
public ValidationException(String message, Map<String, List<String>> violations) {
|
||||
public ValidationException(
|
||||
String message,
|
||||
Map<String, List<String>> violations
|
||||
) {
|
||||
super(message);
|
||||
this.violations = violations != null ? Map.copyOf(violations) : Map.of();
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user