Search in sources :

Example 1 with ApiError

use of ca.bc.gov.educ.penreg.api.exception.errors.ApiError in project EDUC-PEN-REG-BATCH-API by bcgov.

the class RestExceptionHandler method handleInvalidParameter.

/**
 * Handles IllegalArgumentException
 *
 * @param ex the InvalidParameterException
 * @return the ApiError object
 */
@ExceptionHandler(IllegalArgumentException.class)
protected ResponseEntity<Object> handleInvalidParameter(IllegalArgumentException ex) {
    ApiError apiError = new ApiError(BAD_REQUEST);
    apiError.setMessage(ex.getMessage());
    log.error("{} ", apiError.getMessage(), ex);
    return buildResponseEntity(apiError);
}
Also used : ApiError(ca.bc.gov.educ.penreg.api.exception.errors.ApiError) ResponseEntityExceptionHandler(org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 2 with ApiError

use of ca.bc.gov.educ.penreg.api.exception.errors.ApiError in project EDUC-PEN-REG-BATCH-API by bcgov.

the class PenRequestBatchAPIController method archiveBatchFiles.

@Override
public ResponseEntity<List<PenRequestBatch>> archiveBatchFiles(final List<PenRequestBatch> penRequestBatches) {
    val batchIds = penRequestBatches.stream().map(PenRequestBatch::getPenRequestBatchID).map(UUID::fromString).collect(Collectors.toList());
    val errorWithDupPenAssigned = this.sagaService.findDuplicatePenAssignedToDiffPRInSameBatchByBatchIds(batchIds);
    if (errorWithDupPenAssigned.isPresent()) {
        final ApiError error = ApiError.builder().timestamp(LocalDateTime.now()).message(errorWithDupPenAssigned.get()).status(BAD_REQUEST).build();
        throw new InvalidPayloadException(error);
    }
    final var sagaInProgress = !this.getSagaService().findAllByPenRequestBatchIDInAndStatusIn(batchIds, this.getStatusesFilter()).isEmpty();
    if (sagaInProgress) {
        return ResponseEntity.status(HttpStatus.CONFLICT).build();
    }
    val penRequestBatchesFromDB = this.getService().findAllByBatchIds(batchIds);
    if (penRequestBatchesFromDB.size() != batchIds.size()) {
        final ApiError error = ApiError.builder().timestamp(LocalDateTime.now()).message("Invalid Batch Id provided in the payload.").status(BAD_REQUEST).build();
        throw new InvalidPayloadException(error);
    }
    penRequestBatchesFromDB.forEach(el -> {
        el.setPenRequestBatchStatusCode(PenRequestBatchStatusCodes.ARCHIVED.getCode());
        el.setProcessDate(LocalDateTime.now());
    });
    this.getService().updateAllPenRequestBatchAttachedEntities(penRequestBatchesFromDB);
    return ResponseEntity.ok(penRequestBatchesFromDB.stream().map(PenRequestBatchMapper.mapper::toStructure).collect(Collectors.toList()));
}
Also used : lombok.val(lombok.val) ApiError(ca.bc.gov.educ.penreg.api.exception.errors.ApiError) InvalidPayloadException(ca.bc.gov.educ.penreg.api.exception.InvalidPayloadException)

Example 3 with ApiError

use of ca.bc.gov.educ.penreg.api.exception.errors.ApiError in project EDUC-PEN-REG-BATCH-API by bcgov.

the class PenRequestBatchStudentInfoRequestMacroController method validatePayload.

private void validatePayload(final PenRequestBatchStudentInfoRequestMacro penRequestMacro, final boolean isCreateOperation) {
    val validationResult = this.getPenRequestBatchMacroPayloadValidator().validatePayload(penRequestMacro, isCreateOperation);
    this.penRequestBatchMacroPayloadValidator.validatePayload(penRequestMacro, isCreateOperation);
    if (!validationResult.isEmpty()) {
        final ApiError error = ApiError.builder().timestamp(LocalDateTime.now()).message("Payload contains invalid data.").status(BAD_REQUEST).build();
        error.addValidationErrors(validationResult);
        throw new InvalidPayloadException(error);
    }
}
Also used : lombok.val(lombok.val) ApiError(ca.bc.gov.educ.penreg.api.exception.errors.ApiError) InvalidPayloadException(ca.bc.gov.educ.penreg.api.exception.InvalidPayloadException)

Example 4 with ApiError

use of ca.bc.gov.educ.penreg.api.exception.errors.ApiError in project EDUC-PEN-REG-BATCH-API by bcgov.

the class RestExceptionHandler method handleHttpMessageNotReadable.

/**
 * Handle http message not readable response entity.
 *
 * @param ex      the ex
 * @param headers the headers
 * @param status  the status
 * @param request the request
 * @return the response entity
 */
@Override
protected ResponseEntity<Object> handleHttpMessageNotReadable(HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
    String error = "Malformed JSON request";
    log.error("{} ", error, ex);
    return buildResponseEntity(new ApiError(BAD_REQUEST, error, ex));
}
Also used : ApiError(ca.bc.gov.educ.penreg.api.exception.errors.ApiError)

Example 5 with ApiError

use of ca.bc.gov.educ.penreg.api.exception.errors.ApiError in project EDUC-PEN-REG-BATCH-API by bcgov.

the class RestExceptionHandler method handleMethodArgumentNotValid.

/**
 * Handles MethodArgumentNotValidException. Triggered when an object fails @Valid validation.
 *
 * @param ex      the MethodArgumentNotValidException that is thrown when @Valid validation fails
 * @param headers HttpHeaders
 * @param status  HttpStatus
 * @param request WebRequest
 * @return the ApiError object
 */
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
    ApiError apiError = new ApiError(BAD_REQUEST);
    apiError.setMessage("Validation error");
    apiError.addValidationErrors(ex.getBindingResult().getFieldErrors());
    apiError.addValidationError(ex.getBindingResult().getGlobalErrors());
    log.error("{} ", apiError.getMessage(), ex);
    return buildResponseEntity(apiError);
}
Also used : ApiError(ca.bc.gov.educ.penreg.api.exception.errors.ApiError)

Aggregations

ApiError (ca.bc.gov.educ.penreg.api.exception.errors.ApiError)7 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)3 ResponseEntityExceptionHandler (org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler)3 InvalidPayloadException (ca.bc.gov.educ.penreg.api.exception.InvalidPayloadException)2 lombok.val (lombok.val)2