-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobalExceptionHandler.java
More file actions
59 lines (51 loc) · 2.41 KB
/
Copy pathGlobalExceptionHandler.java
File metadata and controls
59 lines (51 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.healthbridge.integration.api.error;
import jakarta.persistence.EntityNotFoundException;
import jakarta.validation.ConstraintViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ProblemDetail;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.stream.Collectors;
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(EntityNotFoundException.class)
public ProblemDetail handleNotFound(EntityNotFoundException ex) {
ProblemDetail detail = ProblemDetail.forStatus(HttpStatus.NOT_FOUND);
detail.setDetail(ex.getMessage());
return detail;
}
@ExceptionHandler({MethodArgumentNotValidException.class, ConstraintViolationException.class})
public ProblemDetail handleValidation(Exception ex) {
ProblemDetail detail = ProblemDetail.forStatus(HttpStatus.BAD_REQUEST);
if (ex instanceof MethodArgumentNotValidException manv) {
String errors = manv.getBindingResult()
.getFieldErrors()
.stream()
.map(error -> "%s %s".formatted(error.getField(), error.getDefaultMessage()))
.collect(Collectors.joining("; "));
detail.setDetail(errors);
} else if (ex instanceof ConstraintViolationException cve) {
String errors = cve.getConstraintViolations()
.stream()
.map(violation -> "%s %s".formatted(violation.getPropertyPath(), violation.getMessage()))
.collect(Collectors.joining("; "));
detail.setDetail(errors);
} else {
detail.setDetail("Validation failed");
}
return detail;
}
@ExceptionHandler(IllegalArgumentException.class)
public ProblemDetail handleIllegalArgument(IllegalArgumentException ex) {
ProblemDetail detail = ProblemDetail.forStatus(HttpStatus.BAD_REQUEST);
detail.setDetail(ex.getMessage());
return detail;
}
@ExceptionHandler(Exception.class)
public ProblemDetail handleGeneric(Exception ex) {
ProblemDetail detail = ProblemDetail.forStatus(HttpStatus.INTERNAL_SERVER_ERROR);
detail.setDetail("Unexpected error occurred");
return detail;
}
}