Search in sources :

Example 36 with ExceptionHandler

use of org.springframework.web.bind.annotation.ExceptionHandler in project nixmash-blog by mintster.

the class GlobalController method handleCategoryNotSupportedException.

@ExceptionHandler(PostCategoryNotSupportedException.class)
public ModelAndView handleCategoryNotSupportedException(HttpServletRequest request) {
    ModelAndView mav = new ModelAndView();
    mav.addObject("category", (String) request.getAttribute("category"));
    String postName = (String) request.getAttribute("postName");
    String newurl = "/post/" + postName;
    mav.addObject("newurl", newurl);
    mav.setViewName("errors/category");
    return mav;
}
Also used : ModelAndView(org.springframework.web.servlet.ModelAndView) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 37 with ExceptionHandler

use of org.springframework.web.bind.annotation.ExceptionHandler in project judge by zjnu-acm.

the class GlobalExceptionHandler method messageExceptionHandler.

@ExceptionHandler(MessageException.class)
public ModelAndView messageExceptionHandler(MessageException ex, Locale locale) {
    String message = ex.getMessage();
    HttpStatus status = ex.getHttpStatus();
    message = messageSource.getMessage(message, null, message, locale);
    return new ModelAndView("message", Collections.singletonMap("message", message), status);
}
Also used : HttpStatus(org.springframework.http.HttpStatus) ModelAndView(org.springframework.web.servlet.ModelAndView) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 38 with ExceptionHandler

use of org.springframework.web.bind.annotation.ExceptionHandler in project judge by zjnu-acm.

the class GlobalExceptionHandler method handler.

@ExceptionHandler(BusinessException.class)
public ModelAndView handler(BusinessException businessException, Locale locale) {
    BusinessCode code = businessException.getCode();
    HttpStatus status = code.getStatus();
    String message = code.getMessage();
    message = messageSource.getMessage(message, businessException.getParams(), message, locale);
    return new ModelAndView("message", Collections.singletonMap("message", message), status);
}
Also used : HttpStatus(org.springframework.http.HttpStatus) ModelAndView(org.springframework.web.servlet.ModelAndView) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 39 with ExceptionHandler

use of org.springframework.web.bind.annotation.ExceptionHandler in project wombat by PLOS.

the class ExceptionHandlerAdvisor method handleNotFound.

/**
 * Directs unhandled exceptions that indicate an invalid URL to a 404 page.
 *
 * @param request  HttpServletRequest
 * @param response HttpServletResponse
 * @return ModelAndView specifying the view
 */
@ExceptionHandler({ MissingServletRequestParameterException.class, NotFoundException.class, NotVisibleException.class, NoHandlerFoundException.class })
protected ModelAndView handleNotFound(HttpServletRequest request, HttpServletResponse response) {
    Site site = siteResolver.resolveSite(request);
    if (site == null && request.getServletPath().equals("/")) {
        response.setHeader("Content-Type", MediaType.TEXT_HTML.toString());
        return appRootPage.serveAppRoot();
    }
    response.setStatus(HttpStatus.NOT_FOUND.value());
    String viewName = (site == null) ? "//notFound" : (site.getKey() + "/ftl/error/notFound");
    return new ModelAndView(viewName);
}
Also used : Site(org.ambraproject.wombat.config.site.Site) ModelAndView(org.springframework.web.servlet.ModelAndView) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 40 with ExceptionHandler

use of org.springframework.web.bind.annotation.ExceptionHandler in project wombat by PLOS.

the class ExceptionHandlerAdvisor method handleException.

/**
 * Handler invoked for all uncaught exceptions.  Renders a "nice" 500 page.
 *
 * @param exception uncaught exception
 * @param request   HttpServletRequest
 * @param response  HttpServletResponse
 * @return ModelAndView specifying the view
 * @throws java.io.IOException
 */
@ExceptionHandler(Exception.class)
protected ModelAndView handleException(Exception exception, HttpServletRequest request, HttpServletResponse response) throws IOException {
    log.error("handleException", exception);
    response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
    Site site = siteResolver.resolveSite(request);
    // For some reason, methods decorated with @ExceptionHandler cannot accept Model parameters,
    // unlike @RequestMapping methods.  So this is a little different...
    String viewName = chooseExceptionView(site, exception);
    ModelAndView mav = new ModelAndView(viewName);
    StringWriter stackTrace = new StringWriter();
    exception.printStackTrace(new PrintWriter(stackTrace));
    mav.addObject("stackTrace", stackTrace.toString());
    return mav;
}
Also used : Site(org.ambraproject.wombat.config.site.Site) StringWriter(java.io.StringWriter) ModelAndView(org.springframework.web.servlet.ModelAndView) PrintWriter(java.io.PrintWriter) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Aggregations

ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)178 ResponseEntity (org.springframework.http.ResponseEntity)58 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)48 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)41 HttpHeaders (org.springframework.http.HttpHeaders)39 ModelAndView (org.springframework.web.servlet.ModelAndView)33 ResponseEntityExceptionHandler (org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler)31 JsonResult (com.odysseusinc.arachne.commons.api.v1.dto.util.JsonResult)17 HttpStatus (org.springframework.http.HttpStatus)13 AjaxJson (com.cdeledu.common.base.AjaxJson)12 ResultModels (eu.bcvsolutions.idm.core.api.dto.ResultModels)8 DefaultErrorModel (eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel)8 ErrorModel (eu.bcvsolutions.idm.core.api.exception.ErrorModel)8 RestResponse (org.entando.entando.web.common.model.RestResponse)8 ArrayList (java.util.ArrayList)7 BindingResult (org.springframework.validation.BindingResult)6 FieldError (org.springframework.validation.FieldError)5 ErrorInfo (com.haulmont.restapi.exception.ErrorInfo)4 PrintWriter (java.io.PrintWriter)4 StringWriter (java.io.StringWriter)4