Search in sources :

Example 41 with ExceptionHandler

use of org.springframework.web.bind.annotation.ExceptionHandler in project free-framework by a601942905git.

the class GlobalExceptionHandler method defaultErrorHandler.

/**
 * 当控制器方法抛出exception异常,执行该方法
 * 根据异常类型执行对应的方法
 * @param request
 * @param e
 * @return
 */
@ExceptionHandler(value = Exception.class)
public ModelAndView defaultErrorHandler(HttpServletRequest request, Exception e) {
    ModelAndView mav = new ModelAndView();
    mav.addObject("exception", e);
    mav.addObject("url", request.getRequestURL());
    mav.setViewName(DEFAULT_ERROR_VIEW);
    return mav;
}
Also used : ModelAndView(org.springframework.web.servlet.ModelAndView) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 42 with ExceptionHandler

use of org.springframework.web.bind.annotation.ExceptionHandler in project data-prep by Talend.

the class TDPExceptionController method handleInvalidArguments.

/**
 * This method is invoked when Spring throw an MethodArgumentNotValidException. This happen because @RequestMapping annotated
 * methods may have @Valid annotated arguments and validation may fail.
 */
@ExceptionHandler({ MethodArgumentNotValidException.class })
public ResponseEntity<String> handleInvalidArguments(MethodArgumentNotValidException e) throws JsonProcessingException {
    final HttpHeaders httpHeaders = new HttpHeaders();
    final Map<String, Object> context = new HashMap<>();
    httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
    context.put("binding_results", e.getBindingResult().getAllErrors());
    TdpExceptionDto exceptionDto = new TdpExceptionDto(CommonErrorCodes.UNEXPECTED_CONTENT.getCode(), null, e.getMessage(), e.getLocalizedMessage(), "Invalid argument", context);
    return new ResponseEntity<>(objectMapper.writeValueAsString(exceptionDto), httpHeaders, NOT_ACCEPTABLE);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) HashMap(java.util.HashMap) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 43 with ExceptionHandler

use of org.springframework.web.bind.annotation.ExceptionHandler in project POC by rajadilipkolli.

the class RestExceptionHandler method handleEntityNotFound.

/**
 * Handles EntityNotFoundException. Created to encapsulate errors with more detail
 * than javax.persistence.EntityNotFoundException.
 *
 * @param ex the EntityNotFoundException
 * @return the ApiError object
 */
@ExceptionHandler(EntityNotFoundException.class)
protected ResponseEntity<Object> handleEntityNotFound(EntityNotFoundException ex) {
    final ApiError apiError = new ApiError(NOT_FOUND);
    apiError.setMessage(ex.getMessage());
    return buildResponseEntity(apiError);
}
Also used : ApiError(com.poc.restfulpoc.config.ApiError) ResponseEntityExceptionHandler(org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 44 with ExceptionHandler

use of org.springframework.web.bind.annotation.ExceptionHandler in project metalnx-web by irods-contrib.

the class TicketClientExceptionController method handleTicketFileNotFound.

@ExceptionHandler({ DataGridTicketDownloadException.class })
public ModelAndView handleTicketFileNotFound(DataGridTicketDownloadException e) {
    ticketClientService.deleteTempTicketDir();
    String path = e.getPath();
    String objName = path.substring(path.lastIndexOf(IRODS_PATH_SEPARATOR) + 1, path.length());
    ModelAndView mav = new ModelAndView();
    mav.addObject("error", e.getMessage());
    mav.addObject("objName", objName);
    mav.addObject("path", path);
    mav.addObject("ticketString", e.getTicketString());
    String viewName = "tickets/ticketclient";
    DataGridUser loggedUser = loggedUserUtils.getLoggedDataGridUser();
    if (loggedUser != null) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        UserTokenDetails userTokenDetails = (UserTokenDetails) auth.getDetails();
        mav.addObject("userDetails", userTokenDetails.getUser());
        viewName = "tickets/ticketAuthAccess";
    }
    mav.setViewName(viewName);
    return mav;
}
Also used : UserTokenDetails(com.emc.metalnx.services.auth.UserTokenDetails) DataGridUser(com.emc.metalnx.core.domain.entity.DataGridUser) Authentication(org.springframework.security.core.Authentication) ModelAndView(org.springframework.web.servlet.ModelAndView) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 45 with ExceptionHandler

use of org.springframework.web.bind.annotation.ExceptionHandler in project FP-PSP-SERVER by FundacionParaguaya.

the class ExceptionTranslatorAdvice method handleGeneralUncaughtExcption.

/**
 * <p>
 *     Any other unhandled exceptions are processed here.
 * </p>
 * <p>
 *     We also process user defined (non standard) exceptions annotated with @ResponseStatus.
 *     See {@link py.org.fundacionparaguaya.pspserver.common.exceptions.UnknownResourceException}
 * </p>
 * @param ex
 * @return
 */
@ExceptionHandler(Exception.class)
protected ResponseEntity<Object> handleGeneralUncaughtExcption(Exception ex) {
    HttpStatus status;
    ErrorDTO errorDto;
    ResponseStatus responseStatus = AnnotationUtils.findAnnotation(ex.getClass(), ResponseStatus.class);
    if (responseStatus != null) {
        status = responseStatus.value();
        errorDto = ErrorDTO.of(responseStatus.reason(), ex.getLocalizedMessage()).formatWithCode();
    } else {
        status = HttpStatus.INTERNAL_SERVER_ERROR;
        errorDto = ErrorDTO.of(ErrorCodes.INTERNAL_SERVER_ERROR.getMessage(), ex.getLocalizedMessage()).formatWithCode();
    }
    LOG.error("Error id: {}", errorDto.getErrorId());
    LOG.error(ex.getMessage(), ex);
    return generateResponseEntity(errorDto, status);
}
Also used : HttpStatus(org.springframework.http.HttpStatus) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) FieldErrorDTO(py.org.fundacionparaguaya.pspserver.common.dtos.FieldErrorDTO) ErrorDTO(py.org.fundacionparaguaya.pspserver.common.dtos.ErrorDTO) 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