-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobalExceptionHandler.java
More file actions
87 lines (74 loc) · 3.72 KB
/
GlobalExceptionHandler.java
File metadata and controls
87 lines (74 loc) · 3.72 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.podzilla.auth.exception;
import io.micrometer.common.lang.NonNull;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
@RestControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
@Override // Good practice to use @Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(
final MethodArgumentNotValidException ex,
@NonNull final HttpHeaders headers,
@NonNull final HttpStatusCode status,
@NonNull final WebRequest request) {
StringBuilder errorMessage = new StringBuilder("Validation failed: ");
for (FieldError error : ex.getBindingResult().getFieldErrors()) {
errorMessage.append(error.getField()).append(": ")
.append(error.getDefaultMessage()).append("; ");
}
ErrorResponse errorResponse = new ErrorResponse(
errorMessage.toString(), (HttpStatus) status);
return new ResponseEntity<>(errorResponse, status);
}
@ExceptionHandler(AccessDeniedException.class)
public ResponseEntity<ErrorResponse> handleAccessDeniedException(
final AccessDeniedException exception) {
return ResponseEntity.status(HttpStatus.FORBIDDEN)
.body(new ErrorResponse(exception.getMessage(),
HttpStatus.FORBIDDEN));
}
@ExceptionHandler(AuthenticationException.class)
public ResponseEntity<ErrorResponse> handleAuthenticationException(
final AuthenticationException exception) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
.body(new ErrorResponse(exception.getMessage(),
HttpStatus.UNAUTHORIZED));
}
@ExceptionHandler(NotFoundException.class)
public ResponseEntity<ErrorResponse> handleNotFoundException(
final NotFoundException exception) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(new ErrorResponse(exception.getMessage(),
HttpStatus.NOT_FOUND));
}
@ExceptionHandler(ValidationException.class)
public ResponseEntity<ErrorResponse> handleValidationException(
final ValidationException exception) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(new ErrorResponse(exception.getMessage(),
HttpStatus.BAD_REQUEST));
}
@ExceptionHandler(InvalidActionException.class)
public ResponseEntity<ErrorResponse> handleInvalidActionException(
final InvalidActionException exception) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(new ErrorResponse(exception.getMessage(),
HttpStatus.BAD_REQUEST));
}
@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> handleException(
final Exception exception) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(new ErrorResponse(exception.getMessage(),
HttpStatus.INTERNAL_SERVER_ERROR));
}
}