use of org.springframework.core.NestedRuntimeException in project ma-core-public by infiniteautomation.
the class RestExceptionHandler 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) {
// To strip off the double messages generated by this...
if (ex instanceof NestedRuntimeException)
ex = (Exception) ((NestedRuntimeException) ex).getMostSpecificCause();
LOG.error(ex.getMessage(), ex);
RestErrorModel error = new RestErrorModel(ex);
headers.set("Messages", "error");
headers.set("Errors", ex.getMessage());
headers.setContentType(MediaType.APPLICATION_JSON);
if (body == null)
body = error;
return super.handleExceptionInternal(ex, body, headers, status, request);
}
use of org.springframework.core.NestedRuntimeException in project com.revolsys.open by revolsys.
the class Logs method getMessageAndException.
public static Throwable getMessageAndException(final StringBuilder messageText, final String message, final Throwable e) {
Throwable logException = e;
final Set<String> messages = Sets.newLinkedHash(message);
if (Property.hasValue(message)) {
messageText.append(message);
}
while (logException instanceof WrappedException || logException instanceof NestedRuntimeException) {
if (messageText.length() > 0) {
messageText.append('\n');
}
messageText.append(logException.getClass().getName());
messageText.append(": ");
final String wrappedMessage = logException.getMessage();
if (messages.add(wrappedMessage)) {
messageText.append(wrappedMessage);
}
final Throwable cause = logException.getCause();
if (cause == null) {
break;
} else {
logException = cause;
}
}
if (logException instanceof SQLException) {
final SQLException sqlException = (SQLException) logException;
final List<Throwable> exceptions = Lists.toArray(sqlException);
final int exceptionCount = exceptions.size();
if (exceptionCount > 0) {
logException = exceptions.remove(exceptionCount - 1);
for (final Throwable throwable : exceptions) {
if (messageText.length() > 0) {
messageText.append('\n');
}
if (throwable == sqlException) {
messageText.append(sqlException.getClass().getName());
messageText.append(": ");
final String wrappedMessage = sqlException.getMessage();
if (messages.add(wrappedMessage)) {
messageText.append(wrappedMessage);
}
} else {
messageText.append(Exceptions.toString(throwable));
}
}
}
}
return logException;
}
use of org.springframework.core.NestedRuntimeException 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