/
RestErrorHandler.java
100 lines (86 loc) · 4.3 KB
/
RestErrorHandler.java
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 com.salesmanager.shop.store.api.exception;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
@Order(Ordered.HIGHEST_PRECEDENCE)
@ControllerAdvice
//@ResponseBody
//@Produces({"application/json"})
//@RequestMapping(produces = "application/json")
public class RestErrorHandler {
private static final Logger log = LoggerFactory.getLogger(RestErrorHandler.class);
@RequestMapping(produces = "application/json")
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public @ResponseBody ErrorEntity handleServiceException(Exception exception) {
log.error(exception.getMessage(), exception);
ErrorEntity errorEntity = createErrorEntity(null, exception.getMessage(),
exception.getLocalizedMessage());
return errorEntity;
}
/**
* Generic exception serviceException handler
*/
@RequestMapping(produces = "application/json")
@ExceptionHandler(ServiceRuntimeException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public @ResponseBody ErrorEntity handleServiceException(ServiceRuntimeException exception) {
log.error(exception.getErrorMessage(), exception);
ErrorEntity errorEntity = createErrorEntity(exception.getErrorCode(), exception.getErrorMessage(),
exception.getLocalizedMessage());
return errorEntity;
}
@RequestMapping(produces = "application/json")
@ExceptionHandler(ConversionRuntimeException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public @ResponseBody ErrorEntity handleServiceException(ConversionRuntimeException exception) {
log.error(exception.getErrorMessage(), exception);
ErrorEntity errorEntity = createErrorEntity(exception.getErrorCode(), exception.getErrorMessage(),
exception.getLocalizedMessage());
return errorEntity;
}
@RequestMapping(produces = "application/json")
@ExceptionHandler(ResourceNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public @ResponseBody ErrorEntity handleServiceException(ResourceNotFoundException exception) {
log.error(exception.getErrorMessage(), exception);
ErrorEntity errorEntity = createErrorEntity(exception.getErrorCode(), exception.getErrorMessage(),
exception.getLocalizedMessage());
return errorEntity;
}
@RequestMapping(produces = "application/json")
@ExceptionHandler(UnauthorizedException.class)
@ResponseStatus(HttpStatus.UNAUTHORIZED)
public @ResponseBody ErrorEntity handleServiceException(UnauthorizedException exception) {
log.error(exception.getErrorMessage(), exception);
ErrorEntity errorEntity = createErrorEntity(exception.getErrorCode(), exception.getErrorMessage(),
exception.getLocalizedMessage());
return errorEntity;
}
@RequestMapping(produces = "application/json")
@ExceptionHandler(RestApiException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public @ResponseBody ErrorEntity handleRestApiException(RestApiException exception) {
log.error(exception.getErrorMessage(), exception);
ErrorEntity errorEntity = createErrorEntity(exception.getErrorCode(), exception.getErrorMessage(),
exception.getLocalizedMessage());
return errorEntity;
}
private ErrorEntity createErrorEntity(String errorCode, String message, String detailMessage) {
ErrorEntity errorEntity = new ErrorEntity();
Optional.ofNullable(errorCode)
.ifPresent(errorEntity::setErrorCode);
String resultMessage = message != null ? message : detailMessage;
Optional.ofNullable(resultMessage)
.ifPresent(errorEntity::setMessage);
return errorEntity;
}
}