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);
}
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()));
}
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);
}
}
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));
}
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);
}
Aggregations