-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobalExceptionHandler.java
More file actions
100 lines (86 loc) · 4.58 KB
/
GlobalExceptionHandler.java
File metadata and controls
100 lines (86 loc) · 4.58 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
88
89
90
91
92
93
94
95
96
97
98
99
100
package uk.gov.hmcts.cp.subscription.controllers;
import jakarta.persistence.EntityNotFoundException;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.servlet.NoHandlerFoundException;
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
private static final String NOT_FOUND_MESSAGE = "No row with the given identifier exists";
@ExceptionHandler(EntityNotFoundException.class)
public ResponseEntity<String> handleNotFoundException(final EntityNotFoundException exception) {
log.error("NotFoundException {}", exception.getMessage());
return ResponseEntity
.status(HttpStatus.NOT_FOUND)
.body(NOT_FOUND_MESSAGE);
}
@ExceptionHandler(HttpClientErrorException.class)
public ResponseEntity<String> handleClientException(final HttpClientErrorException exception) {
log.error("HttpClientErrorException {}", exception.getMessage());
return ResponseEntity
.status(exception.getStatusCode())
.body(exception.getMessage());
}
@ExceptionHandler(HttpMessageNotReadableException.class)
public ResponseEntity<String> handleHttpMessageNotReadableException(final HttpMessageNotReadableException exception) {
log.error("Invalid request body - HttpMessageNotReadableException: {}", exception.getMessage(), exception);
return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body("");
}
@ExceptionHandler(Exception.class)
public ResponseEntity<String> handleUnknownException(final Exception exception) {
log.error("Exception {}", exception.getMessage());
return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body(exception.getMessage());
}
@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
public ResponseEntity<String> handleHttpMediaTypeNotSupportedException(
final HttpMediaTypeNotSupportedException exception) {
log.error("Unsupported media type - HttpMediaTypeNotSupportedException: {}", exception.getMessage(), exception);
return ResponseEntity
.status(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
.body(exception.getMessage());
}
@ExceptionHandler(ConstraintViolationException.class)
public ResponseEntity<Void> handleConstraintViolation(final ConstraintViolationException ex) {
log.error("Exception {}", ex.getMessage());
return ResponseEntity.badRequest().build();
}
@ExceptionHandler(NoHandlerFoundException.class)
public ResponseEntity<String> handleNoHandlerFoundException(final NoHandlerFoundException exception) {
log.error("No handler found for request: {} {}", exception.getHttpMethod(), exception.getRequestURL(), exception);
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Not Found");
}
@ExceptionHandler(ResponseStatusException.class)
public ResponseEntity<String> handleResponseStatusException(final ResponseStatusException exception) {
log.error("ResponseStatusException: {}", exception.getMessage());
return ResponseEntity
.status(exception.getStatusCode())
.body(exception.getReason() != null ? exception.getReason() : exception.getMessage());
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<String> handleValidationException(final MethodArgumentNotValidException exception) {
log.error("Validation failed: {}", exception.getMessage());
return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body(exception.getMessage());
}
@ExceptionHandler(UnsupportedOperationException.class)
public ResponseEntity<String> handleUnsupportedOperation(final UnsupportedOperationException exception) {
log.error("Unsupported operation: {}", exception.getMessage());
return ResponseEntity
.status(HttpStatus.NOT_IMPLEMENTED)
.body("Unsupported");
}
}