|
24 | 24 | import org.slf4j.Logger; |
25 | 25 | import org.slf4j.LoggerFactory; |
26 | 26 | import org.springframework.http.HttpStatus; |
| 27 | +import org.springframework.http.HttpStatusCode; |
| 28 | +import org.springframework.http.ResponseEntity; |
27 | 29 | import org.springframework.http.converter.HttpMessageConversionException; |
| 30 | +import org.springframework.web.ErrorResponse; |
28 | 31 | import org.springframework.web.bind.annotation.ControllerAdvice; |
29 | 32 | import org.springframework.web.bind.annotation.ExceptionHandler; |
30 | 33 | import org.springframework.web.bind.annotation.ResponseBody; |
@@ -137,20 +140,39 @@ public ErrorInfo handleTaskAlreadyClaimed(FlowableTaskAlreadyClaimedException e, |
137 | 140 |
|
138 | 141 | // Fall back |
139 | 142 |
|
140 | | - @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) // 500 |
141 | 143 | @ExceptionHandler(Exception.class) |
142 | | - @ResponseBody |
143 | | - public ErrorInfo handleOtherException(Exception e, HttpServletRequest request) { |
| 144 | + public ResponseEntity<ErrorInfo> handleOtherException(Exception e, HttpServletRequest request) { |
| 145 | + ResponseEntity.BodyBuilder responseBuilder; |
| 146 | + String message; |
| 147 | + if (e instanceof ErrorResponse errorResponse) { |
| 148 | + // Spring MVC exceptions (e.g. no handler found, method not supported) define their own status |
| 149 | + HttpStatusCode status = errorResponse.getStatusCode(); |
| 150 | + responseBuilder = ResponseEntity.status(status).headers(errorResponse.getHeaders()); |
| 151 | + message = errorResponse.getBody().getTitle(); |
| 152 | + if (status.is4xxClientError()) { |
| 153 | + if (logger.isDebugEnabled()) { |
| 154 | + logger.debug("Spring MVC client error. Message: {}, Request: {} {}", e.getMessage(), request.getMethod(), request.getRequestURI()); |
| 155 | + } |
| 156 | + // The problem detail is meant for clients, the exception message can contain internal details (e.g. controller method signatures) |
| 157 | + ErrorInfo errorInfo = new ErrorInfo(message, null); |
| 158 | + errorInfo.setException(errorResponse.getBody().getDetail()); |
| 159 | + return responseBuilder.body(errorInfo); |
| 160 | + } |
| 161 | + } else { |
| 162 | + responseBuilder = ResponseEntity.internalServerError(); |
| 163 | + message = "Internal server error"; |
| 164 | + } |
| 165 | + |
144 | 166 | if (sendFullErrorException) { |
145 | 167 | logger.error("Unhandled exception. Request: {} {}", request.getMethod(), request.getRequestURI(), e); |
146 | | - return new ErrorInfo("Internal server error", e); |
| 168 | + return responseBuilder.body(new ErrorInfo(message, e)); |
147 | 169 | } else { |
148 | 170 |
|
149 | 171 | String errorIdentifier = UUID.randomUUID().toString(); |
150 | 172 | logger.error("Unhandled exception. Error ID: {}. Request: {} {}", errorIdentifier, request.getMethod(), request.getRequestURI(), e); |
151 | | - ErrorInfo errorInfo = new ErrorInfo("Internal server error", e); |
| 173 | + ErrorInfo errorInfo = new ErrorInfo(message, e); |
152 | 174 | errorInfo.setException("Error with ID: " + errorIdentifier); |
153 | | - return errorInfo; |
| 175 | + return responseBuilder.body(errorInfo); |
154 | 176 | } |
155 | 177 | } |
156 | 178 |
|
|
0 commit comments