Search in sources :

Example 1 with NestedRuntimeException

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);
}
Also used : NestedRuntimeException(org.springframework.core.NestedRuntimeException) RestErrorModel(com.serotonin.m2m2.web.mvc.rest.v1.model.RestErrorModel) NoSuchRequestHandlingMethodException(org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) NoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException) MissingServletRequestPartException(org.springframework.web.multipart.support.MissingServletRequestPartException) BindException(org.springframework.validation.BindException) ConversionNotSupportedException(org.springframework.beans.ConversionNotSupportedException) NestedRuntimeException(org.springframework.core.NestedRuntimeException) CSVException(com.serotonin.m2m2.web.mvc.rest.v1.csv.CSVException) MissingServletRequestParameterException(org.springframework.web.bind.MissingServletRequestParameterException) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) ServletRequestBindingException(org.springframework.web.bind.ServletRequestBindingException) TypeMismatchException(org.springframework.beans.TypeMismatchException) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) HttpMediaTypeNotSupportedException(org.springframework.web.HttpMediaTypeNotSupportedException) HttpRequestMethodNotSupportedException(org.springframework.web.HttpRequestMethodNotSupportedException) HttpMediaTypeNotAcceptableException(org.springframework.web.HttpMediaTypeNotAcceptableException)

Example 2 with NestedRuntimeException

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;
}
Also used : WrappedException(com.revolsys.util.WrappedException) NestedRuntimeException(org.springframework.core.NestedRuntimeException) SQLException(java.sql.SQLException)

Example 3 with NestedRuntimeException

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);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) NestedRuntimeException(org.springframework.core.NestedRuntimeException) ResponseEntity(org.springframework.http.ResponseEntity) User(com.serotonin.m2m2.vo.User) AbstractRestV2Exception(com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServerErrorException(com.infiniteautomation.mango.rest.v2.exception.ServerErrorException) IOException(java.io.IOException) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) GenericRestException(com.infiniteautomation.mango.rest.v2.exception.GenericRestException) AbstractRestV2Exception(com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception) ServerErrorException(com.infiniteautomation.mango.rest.v2.exception.ServerErrorException) AccessDeniedException(com.infiniteautomation.mango.rest.v2.exception.AccessDeniedException) ValidationException(com.serotonin.m2m2.vo.exception.ValidationException) ResourceNotFoundException(com.infiniteautomation.mango.rest.v2.exception.ResourceNotFoundException) NestedRuntimeException(org.springframework.core.NestedRuntimeException) NotFoundException(com.serotonin.m2m2.vo.exception.NotFoundException) IOException(java.io.IOException) GenericRestException(com.infiniteautomation.mango.rest.v2.exception.GenericRestException) ValidationFailedRestException(com.infiniteautomation.mango.rest.v2.exception.ValidationFailedRestException) NotFoundRestException(com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException)

Aggregations

NestedRuntimeException (org.springframework.core.NestedRuntimeException)3 AbstractRestV2Exception (com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception)1 AccessDeniedException (com.infiniteautomation.mango.rest.v2.exception.AccessDeniedException)1 GenericRestException (com.infiniteautomation.mango.rest.v2.exception.GenericRestException)1 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)1 ResourceNotFoundException (com.infiniteautomation.mango.rest.v2.exception.ResourceNotFoundException)1 ServerErrorException (com.infiniteautomation.mango.rest.v2.exception.ServerErrorException)1 ValidationFailedRestException (com.infiniteautomation.mango.rest.v2.exception.ValidationFailedRestException)1 WrappedException (com.revolsys.util.WrappedException)1 User (com.serotonin.m2m2.vo.User)1 NotFoundException (com.serotonin.m2m2.vo.exception.NotFoundException)1 ValidationException (com.serotonin.m2m2.vo.exception.ValidationException)1 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)1 CSVException (com.serotonin.m2m2.web.mvc.rest.v1.csv.CSVException)1 RestErrorModel (com.serotonin.m2m2.web.mvc.rest.v1.model.RestErrorModel)1 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 ConversionNotSupportedException (org.springframework.beans.ConversionNotSupportedException)1