Search in sources :

Example 6 with ServerErrorException

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);
    }
}
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

ServerErrorException (com.infiniteautomation.mango.rest.v2.exception.ServerErrorException)6 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)5 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)3 AbstractRestV2Exception (com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception)2 AccessDeniedException (com.infiniteautomation.mango.rest.v2.exception.AccessDeniedException)2 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)2 User (com.serotonin.m2m2.vo.User)2 AbstractPointEventDetectorVO (com.serotonin.m2m2.vo.event.detector.AbstractPointEventDetectorVO)2 RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)2 ApiResponses (com.wordnik.swagger.annotations.ApiResponses)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 HttpSession (javax.servlet.http.HttpSession)2 ResponseEntity (org.springframework.http.ResponseEntity)2 GenericRestException (com.infiniteautomation.mango.rest.v2.exception.GenericRestException)1 ResourceNotFoundException (com.infiniteautomation.mango.rest.v2.exception.ResourceNotFoundException)1 ValidationFailedRestException (com.infiniteautomation.mango.rest.v2.exception.ValidationFailedRestException)1 HighPriorityTask (com.serotonin.m2m2.util.timeout.HighPriorityTask)1