Search in sources :

Example 16 with ExceptionHandler

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

the class RestExceptionHandler method processRestRourceNotFoundEx.

@ExceptionHandler(value = RestRourceNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
@ResponseBody
public RestResponse processRestRourceNotFoundEx(RestRourceNotFoundException ex) {
    logger.debug("Handling {} error", ex.getClass().getSimpleName());
    RestResponse response = null;
    if (null != ex.getBindingResult()) {
        BindingResult result = ex.getBindingResult();
        response = processAllErrors(result);
    } else {
        response = new RestResponse();
        RestError error = new RestError(ex.getErrorCode(), this.resolveLocalizedErrorMessage("NOT_FOUND", new Object[] { ex.getObjectName(), ex.getObjectCode() }));
        List<RestError> errors = new ArrayList<>();
        errors.add(error);
        response.setErrors(errors);
        response.setMetaData(new HashMap<>());
    }
    return response;
}
Also used : BindingResult(org.springframework.validation.BindingResult) RestResponse(org.entando.entando.web.common.model.RestResponse) RestError(org.entando.entando.web.common.model.RestError) ArrayList(java.util.ArrayList) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 17 with ExceptionHandler

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

the class RestExceptionHandler method processValidationError.

@ExceptionHandler(value = ValidationGenericException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public RestResponse processValidationError(ValidationGenericException 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)

Example 18 with ExceptionHandler

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

the class RestExceptionHandler method processEntandoTokenException.

@ExceptionHandler(value = EntandoTokenException.class)
@ResponseStatus(HttpStatus.FORBIDDEN)
@ResponseBody
public RestResponse processEntandoTokenException(EntandoAuthorizationException ex) {
    logger.debug("Handling {} error", ex.getClass().getSimpleName());
    RestResponse response = new RestResponse();
    RestError error = new RestError(RestErrorCodes.UNAUTHORIZED, this.resolveLocalizedErrorMessage("UNAUTHORIZED", new Object[] { ex.getUsername(), ex.getRequestURI(), ex.getMethod() }));
    List<RestError> errors = new ArrayList<>();
    errors.add(error);
    response.setErrors(errors);
    return response;
}
Also used : RestResponse(org.entando.entando.web.common.model.RestResponse) RestError(org.entando.entando.web.common.model.RestError) ArrayList(java.util.ArrayList) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 19 with ExceptionHandler

use of org.springframework.web.bind.annotation.ExceptionHandler in project nikita-noark5-core by HiOA-ABI.

the class RestResponseEntityExceptionHandler method handleMalformedHeaderException.

@ExceptionHandler(NikitaMalformedHeaderException.class)
public ResponseEntity<Object> handleMalformedHeaderException(final RuntimeException ex, final WebRequest request) {
    logger.error("400 Status Code", ex);
    logger.error(request.getDescription(true), ex);
    return handleExceptionInternal(ex, message(HttpStatus.BAD_REQUEST, ex), new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntityExceptionHandler(org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 20 with ExceptionHandler

use of org.springframework.web.bind.annotation.ExceptionHandler in project CzechIdMng by bcvsolutions.

the class ExceptionControllerAdvice method handle.

@ExceptionHandler(DataIntegrityViolationException.class)
ResponseEntity<ResultModels> handle(DataIntegrityViolationException ex) {
    ErrorModel errorModel = null;
    // 
    if (ex.getCause() != null && ex.getCause() instanceof ConstraintViolationException) {
        ConstraintViolationException constraintEx = (ConstraintViolationException) ex.getCause();
        // TODO: registrable constraint error codes
        if (constraintEx.getConstraintName() != null && constraintEx.getConstraintName().contains("name")) {
            errorModel = new DefaultErrorModel(CoreResultCode.NAME_CONFLICT, ImmutableMap.of("name", constraintEx.getConstraintName()));
        } else if (constraintEx.getConstraintName() != null && constraintEx.getConstraintName().contains("code")) {
            errorModel = new DefaultErrorModel(CoreResultCode.CODE_CONFLICT, ImmutableMap.of("name", constraintEx.getConstraintName()));
        } else {
            errorModel = new DefaultErrorModel(CoreResultCode.CONFLICT, ImmutableMap.of("name", constraintEx.getConstraintName()));
        }
    } else {
        errorModel = new DefaultErrorModel(CoreResultCode.CONFLICT, ex.getMostSpecificCause().getMessage());
    }
    log.error("[" + errorModel.getId() + "] ", ex);
    return new ResponseEntity<>(new ResultModels(errorModel), new HttpHeaders(), errorModel.getStatus());
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) DefaultErrorModel(eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel) ErrorModel(eu.bcvsolutions.idm.core.api.exception.ErrorModel) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) RepositoryConstraintViolationException(org.springframework.data.rest.core.RepositoryConstraintViolationException) DefaultErrorModel(eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel) ResultModels(eu.bcvsolutions.idm.core.api.dto.ResultModels) 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