Search in sources :

Example 61 with ExceptionHandler

use of org.springframework.web.bind.annotation.ExceptionHandler in project arch-playground by BeneStem.

the class ExceptionHandlerHtmlControllerAdvice method handleConstraintViolation.

@ExceptionHandler(ConstraintViolationException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ModelAndView handleConstraintViolation(final ConstraintViolationException exception) {
    final MappingJackson2JsonView view = new CustomMappingJackson2JsonView();
    final var modelAndView = new ModelAndView(view);
    modelAndView.addObject(exception.getConstraintViolations().stream().map(constraintViolation -> new ValidationError(((PathImpl) constraintViolation.getPropertyPath()).getLeafNode().getName(), constraintViolation.getMessage())).collect(toList()));
    return modelAndView;
}
Also used : ModelAndView(org.springframework.web.servlet.ModelAndView) ValidationError(com.breuninger.arch.playground.common.domain.ValidationError) MappingJackson2JsonView(org.springframework.web.servlet.view.json.MappingJackson2JsonView) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus)

Example 62 with ExceptionHandler

use of org.springframework.web.bind.annotation.ExceptionHandler in project rpki-validator-3 by RIPE-NCC.

the class ApiErrorHandler method handleDataRetrievalFailureException.

@ExceptionHandler(value = DataRetrievalFailureException.class)
protected ResponseEntity<ApiResponse<?>> handleDataRetrievalFailureException(DataRetrievalFailureException ex, WebRequest request) {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.valueOf(Api.API_MIME_TYPE));
    return new ResponseEntity<>(ApiResponse.error(ApiError.of(HttpStatus.NOT_FOUND)), headers, HttpStatus.NOT_FOUND);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) ResponseEntityExceptionHandler(org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 63 with ExceptionHandler

use of org.springframework.web.bind.annotation.ExceptionHandler in project SONG by overture-stack.

the class ServerExceptionHandler method handleAccessDeniedException.

@ExceptionHandler(AccessDeniedException.class)
public ResponseEntity<String> handleAccessDeniedException(HttpServletRequest request, HttpServletResponse response, AccessDeniedException ex) {
    val rootCause = getRootCause(ex);
    val error = SongError.builder().requestUrl(generateRequestUrlWithParams(request)).timestamp(System.currentTimeMillis()).httpStatusCode(UNAUTHORIZED_TOKEN.getHttpStatus().value()).httpStatusName(UNAUTHORIZED_TOKEN.getHttpStatus().name()).errorId(UNAUTHORIZED_TOKEN.getErrorId()).message(ex.getMessage()).debugMessage(format("[ROOT_CAUSE] -> %s: %s", rootCause.getClass().getName(), rootCause.getMessage())).stackTrace(getFullStackTraceList(ex)).build();
    log.error(error.toPrettyJson());
    return error.getResponseEntity();
}
Also used : lombok.val(lombok.val) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 64 with ExceptionHandler

use of org.springframework.web.bind.annotation.ExceptionHandler in project SONG by overture-stack.

the class ServerExceptionHandler method handleThrowable.

@ExceptionHandler(Throwable.class)
public ResponseEntity<String> handleThrowable(HttpServletRequest request, HttpServletResponse response, Throwable ex) {
    val rootCause = getRootCause(ex);
    val error = SongError.builder().requestUrl(generateRequestUrlWithParams(request)).timestamp(System.currentTimeMillis()).httpStatusCode(UNKNOWN_ERROR.getHttpStatus().value()).httpStatusName(UNKNOWN_ERROR.getHttpStatus().name()).errorId(UNKNOWN_ERROR.getErrorId()).message(ex.getMessage()).debugMessage(format("[ROOT_CAUSE] -> %s: %s", rootCause.getClass().getName(), rootCause.getMessage())).stackTrace(getFullStackTraceList(ex)).build();
    log.error(error.toPrettyJson());
    return error.getResponseEntity();
}
Also used : lombok.val(lombok.val) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 65 with ExceptionHandler

use of org.springframework.web.bind.annotation.ExceptionHandler in project flytecnologia-api by jullierme.

the class FlyExceptionHandler method handleDataIntegrityViolationException.

@ExceptionHandler(DataIntegrityViolationException.class)
public ResponseEntity<Object> handleDataIntegrityViolationException(DataIntegrityViolationException ex, WebRequest request) {
    String fieldError = "resource.operation-not-allowed";
    if (ex.getCause() instanceof ConstraintViolationException) {
        fieldError = ((ConstraintViolationException) ex.getCause()).getConstraintName();
    }
    List<Error> errors = getListOfErros(fieldError, ex);
    return handleExceptionInternal(ex, errors, new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) FieldError(org.springframework.validation.FieldError) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseEntityExceptionHandler(org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler)

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