Search in sources :

Example 11 with ExceptionHandler

use of org.springframework.web.bind.annotation.ExceptionHandler in project spring-boot-starter-kit by tripsta.

the class ExceptionHandlingAdvisor method handleMethodArgumentNotValidException.

@ExceptionHandler(MethodArgumentNotValidException.class)
public HttpEntity<ApiErrorResponse> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
    logger.warn("Invalid request " + e.getMessage(), e);
    List<ApiError> errorsList = new ArrayList<ApiError>();
    ApiErrorResponse errorResponse = new ApiErrorResponse();
    for (ObjectError error : e.getBindingResult().getAllErrors()) {
        String errorMsg = createErrorMessage(error);
        errorsList.add(new ApiError(errorMsg, ExceptionType.FIELD_VALIDATION));
    }
    errorResponse.addErrors(errorsList);
    return new ResponseEntity<ApiErrorResponse>(errorResponse, HttpStatus.BAD_REQUEST);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) ObjectError(org.springframework.validation.ObjectError) ArrayList(java.util.ArrayList) ApiErrorResponse(com.sbsk.model.ApiErrorResponse) ApiError(com.sbsk.model.ApiError) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 12 with ExceptionHandler

use of org.springframework.web.bind.annotation.ExceptionHandler in project uhgroupings by uhawaii-system-its-ti-iam.

the class ErrorControllerAdvice method handleTypeMismatchException.

// todo this is for the HolidayRestControllerTest test (should we really have this behavior?)
@ExceptionHandler(TypeMismatchException.class)
public String handleTypeMismatchException(Exception ex) {
    String username = null;
    User user = userContextService.getCurrentUser();
    if (user != null) {
        username = user.getUsername();
    }
    logger.error("username: " + username + "; Exception: ", ex);
    return "redirect:/error";
}
Also used : User(edu.hawaii.its.groupings.access.User) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 13 with ExceptionHandler

use of org.springframework.web.bind.annotation.ExceptionHandler in project open-kilda by telstra.

the class NorthboundExceptionHandler method handleMessageException.

/**
 * Handles NorthboundException exception.
 *
 * @param exception the NorthboundException instance
 * @param request   the WebRequest caused exception
 * @return the ResponseEntity object instance
 */
@ExceptionHandler(MessageException.class)
protected ResponseEntity<Object> handleMessageException(MessageException exception, WebRequest request) {
    HttpStatus status;
    switch(exception.getErrorType()) {
        case NOT_FOUND:
            status = HttpStatus.NOT_FOUND;
            break;
        case DATA_INVALID:
        case PARAMETERS_INVALID:
            status = HttpStatus.BAD_REQUEST;
            break;
        case ALREADY_EXISTS:
            status = HttpStatus.CONFLICT;
            break;
        case AUTH_FAILED:
            status = HttpStatus.UNAUTHORIZED;
            break;
        case OPERATION_TIMED_OUT:
        case INTERNAL_ERROR:
        default:
            status = HttpStatus.INTERNAL_SERVER_ERROR;
            break;
    }
    MessageError error = new MessageError(request.getHeader(CORRELATION_ID), exception.getTimestamp(), exception.getErrorType().toString(), exception.getMessage(), exception.getErrorDescription());
    logger.warn(format("Error %s caught.", error), exception);
    return super.handleExceptionInternal(exception, error, new HttpHeaders(), status, request);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpStatus(org.springframework.http.HttpStatus) MessageError(org.openkilda.messaging.error.MessageError) ResponseEntityExceptionHandler(org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 14 with ExceptionHandler

use of org.springframework.web.bind.annotation.ExceptionHandler in project perry by ca-cwds.

the class LoginResourceExceptionHandler method handleException.

@ExceptionHandler(value = { Exception.class })
protected ModelAndView handleException(Exception ex, WebRequest request) {
    logger.error(ex.getMessage(), ex);
    ModelAndView modelAndView = new ModelAndView(ERROR_CONTROLLER);
    request.setAttribute(ERROR_MESSAGE, ex, SCOPE_REQUEST);
    return modelAndView;
}
Also used : ModelAndView(org.springframework.web.servlet.ModelAndView) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 15 with ExceptionHandler

use of org.springframework.web.bind.annotation.ExceptionHandler in project entando-core by entando.

the class RestExceptionHandler method processValidationError.

@ExceptionHandler(value = ValidationUpdateSelfException.class)
@ResponseStatus(HttpStatus.FORBIDDEN)
@ResponseBody
public RestResponse processValidationError(ValidationUpdateSelfException ex) {
    logger.debug("Handling {} error", ex.getClass().getSimpleName());
    BindingResult result = ex.getBindingResult();
    RestResponse response = processAllErrors(result);
    return response;
}
Also used : BindingResult(org.springframework.validation.BindingResult) RestResponse(org.entando.entando.web.common.model.RestResponse) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

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