Skip to content

Commit 25b004e

Browse files
committed
Return the Spring MVC status for Spring MVC exceptions in the REST API
Exceptions raised by Spring MVC itself (e.g. no handler found for a URL, HTTP method not supported) were handled by the fallback exception handler and returned as 500 Internal server error. They now use the status and headers defined by Spring (e.g. 404, 405 with the Allow header), while the body keeps the Flowable ErrorInfo format. (cherry picked from commit dbb46f6)
1 parent ba1b45a commit 25b004e

1 file changed

Lines changed: 28 additions & 6 deletions

File tree

‎modules/flowable-common-rest/src/main/java/org/flowable/common/rest/exception/BaseExceptionHandlerAdvice.java‎

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
import org.slf4j.Logger;
2525
import org.slf4j.LoggerFactory;
2626
import org.springframework.http.HttpStatus;
27+
import org.springframework.http.HttpStatusCode;
28+
import org.springframework.http.ResponseEntity;
2729
import org.springframework.http.converter.HttpMessageConversionException;
30+
import org.springframework.web.ErrorResponse;
2831
import org.springframework.web.bind.annotation.ControllerAdvice;
2932
import org.springframework.web.bind.annotation.ExceptionHandler;
3033
import org.springframework.web.bind.annotation.ResponseBody;
@@ -137,20 +140,39 @@ public ErrorInfo handleTaskAlreadyClaimed(FlowableTaskAlreadyClaimedException e,
137140

138141
// Fall back
139142

140-
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) // 500
141143
@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+
144166
if (sendFullErrorException) {
145167
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));
147169
} else {
148170

149171
String errorIdentifier = UUID.randomUUID().toString();
150172
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);
152174
errorInfo.setException("Error with ID: " + errorIdentifier);
153-
return errorInfo;
175+
return responseBuilder.body(errorInfo);
154176
}
155177
}
156178

0 commit comments

Comments
 (0)