Search in sources :

Example 1 with ErrorDTO

use of py.org.fundacionparaguaya.pspserver.common.dtos.ErrorDTO 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)

Example 2 with ErrorDTO

use of py.org.fundacionparaguaya.pspserver.common.dtos.ErrorDTO in project FP-PSP-SERVER by FundacionParaguaya.

the class ExceptionTranslatorAdvice method handleCustomParameterizedValidationError.

@ExceptionHandler(CustomParameterizedException.class)
public ResponseEntity<Object> handleCustomParameterizedValidationError(CustomParameterizedException ex) {
    List<FieldErrorDTO> fieldErrors = ex.getFieldErrors();
    ErrorDTO dto = ErrorDTO.of(ex.getLocalizedMessage());
    dto.addFieldErrors(fieldErrors);
    return generateResponseEntity(dto, HttpStatus.BAD_REQUEST);
}
Also used : FieldErrorDTO(py.org.fundacionparaguaya.pspserver.common.dtos.FieldErrorDTO) FieldErrorDTO(py.org.fundacionparaguaya.pspserver.common.dtos.FieldErrorDTO) ErrorDTO(py.org.fundacionparaguaya.pspserver.common.dtos.ErrorDTO) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 3 with ErrorDTO

use of py.org.fundacionparaguaya.pspserver.common.dtos.ErrorDTO in project FP-PSP-SERVER by FundacionParaguaya.

the class ExceptionTranslatorAdvice method handleMethodArgumentNotValid.

/**
 * <p>
 *   Errors thrown in Controllers methods with arguments annotated with JAX-RS @Valid.
 * </p>
 *
 * @param ex
 * @return
 */
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
    BindingResult result = ex.getBindingResult();
    List<FieldError> fieldErrors = result.getFieldErrors();
    ErrorDTO dto = ErrorDTO.of(fieldErrors, ErrorCodes.VALIDATION_FAILURE.getMessage(), ex.getLocalizedMessage());
    return generateResponseEntity(dto, HttpStatus.BAD_REQUEST);
}
Also used : BindingResult(org.springframework.validation.BindingResult) FieldErrorDTO(py.org.fundacionparaguaya.pspserver.common.dtos.FieldErrorDTO) ErrorDTO(py.org.fundacionparaguaya.pspserver.common.dtos.ErrorDTO) FieldError(org.springframework.validation.FieldError)

Example 4 with ErrorDTO

use of py.org.fundacionparaguaya.pspserver.common.dtos.ErrorDTO in project FP-PSP-SERVER by FundacionParaguaya.

the class ExceptionTranslatorAdvice method handleBusinessValidationError.

/**
 * <p>
 *   Errors thrown in service layers validations, using standard exceptions.
 * </p>
 * <p>
 *   We favor the use of standard exceptions as recommended in:
 *   https://github.com/tatsuyaoiw/effective-java/blob/master/chapter-9.md#item-60-favor-the-use-of-standard-exceptions
 * </p>
 * @param ex
 * @return
 */
@ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<Object> handleBusinessValidationError(IllegalArgumentException ex) {
    LOG.debug(ex.getMessage(), ex);
    ErrorDTO dto = ErrorDTO.of(ex.getLocalizedMessage());
    return generateResponseEntity(dto, HttpStatus.BAD_REQUEST);
}
Also used : FieldErrorDTO(py.org.fundacionparaguaya.pspserver.common.dtos.FieldErrorDTO) ErrorDTO(py.org.fundacionparaguaya.pspserver.common.dtos.ErrorDTO) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Aggregations

ErrorDTO (py.org.fundacionparaguaya.pspserver.common.dtos.ErrorDTO)4 FieldErrorDTO (py.org.fundacionparaguaya.pspserver.common.dtos.FieldErrorDTO)4 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)3 HttpStatus (org.springframework.http.HttpStatus)1 BindingResult (org.springframework.validation.BindingResult)1 FieldError (org.springframework.validation.FieldError)1 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)1