use of com.infiniteautomation.mango.rest.v2.exception.ServerErrorException in project ma-core-public by infiniteautomation.
the class MangoSpringExceptionHandler method handleExceptionInternal.
/* (non-Javadoc)
* @see org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler#handleExceptionInternal(java.lang.Exception, java.lang.Object, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatus, org.springframework.web.context.request.WebRequest)
*/
@Override
protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatus status, WebRequest request) {
HttpServletRequest servletRequest = ((ServletWebRequest) request).getRequest();
HttpServletResponse servletResponse = ((ServletWebRequest) request).getResponse();
this.storeException(servletRequest, ex, status);
// Log all but not found exceptions
if (body instanceof ServerErrorException || body instanceof GenericRestException || !(body instanceof AbstractRestV2Exception))
ExceptionUtils.logWebException(ex, servletRequest, LOG);
if (this.browserHtmlRequestMatcher.matches(servletRequest)) {
String uri;
if (status == HttpStatus.FORBIDDEN) {
// browser HTML request
uri = ACCESS_DENIED;
User user = Common.getHttpUser();
if (user != null) {
uri = DefaultPagesDefinition.getUnauthorizedUri(servletRequest, servletResponse, user);
}
// Put exception into request scope (perhaps of use to a view)
servletRequest.setAttribute(WebAttributes.ACCESS_DENIED_403, ex);
// Set the 403 status code.
servletResponse.setStatus(HttpServletResponse.SC_FORBIDDEN);
} else {
uri = DefaultPagesDefinition.getErrorUri(servletRequest, servletResponse);
}
try {
servletResponse.sendRedirect(uri);
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
return null;
} else {
// To strip off the double messages generated by this...
if (ex instanceof NestedRuntimeException)
ex = (Exception) ((NestedRuntimeException) ex).getMostSpecificCause();
// If no body provided we will create one
if (body == null)
body = new GenericRestException(status, ex);
return new ResponseEntity<Object>(body, headers, status);
}
}
Aggregations