use of org.folio.rest.core.exceptions.HttpException in project mod-orders by folio-org.
the class PieceDeleteFlowManager method isDeletePieceRequestValid.
private CompletableFuture<Void> isDeletePieceRequestValid(PieceDeletionHolder holder, RequestContext requestContext) {
List<Error> combinedErrors = new ArrayList<>();
if (holder.getPieceToDelete().getItemId() != null) {
return inventoryManager.getNumberOfRequestsByItemId(holder.getPieceToDelete().getItemId(), requestContext).thenAccept(numOfRequests -> {
if (numOfRequests != null && numOfRequests > 0) {
combinedErrors.add(ErrorCodes.REQUEST_FOUND.toError());
}
}).thenAccept(numOfRequests -> {
if (CollectionUtils.isNotEmpty(combinedErrors)) {
Errors errors = new Errors().withErrors(combinedErrors).withTotalRecords(combinedErrors.size());
logger.error("Validation error : " + JsonObject.mapFrom(errors).encodePrettily());
throw new HttpException(RestConstants.VALIDATION_ERROR, errors);
}
});
}
return completedFuture(null);
}
use of org.folio.rest.core.exceptions.HttpException in project mod-orders by folio-org.
the class AbstractHelper method handleProcessingError.
protected int handleProcessingError(Throwable throwable) {
final Throwable cause = throwable.getCause();
logger.error("Exception encountered", cause);
final Error error;
final int code;
if (cause instanceof HttpException) {
code = ((HttpException) cause).getCode();
error = ((HttpException) cause).getError();
} else {
code = INTERNAL_SERVER_ERROR.getStatusCode();
error = GENERIC_ERROR_CODE.toError().withAdditionalProperty(ERROR_CAUSE, cause.getMessage());
}
if (getErrors().isEmpty()) {
addProcessingError(error);
}
return code;
}
use of org.folio.rest.core.exceptions.HttpException in project mod-orders by folio-org.
the class PurchaseOrderHelper method checkOrderApprovalRequired.
/**
* If an order is transitioning to OPEN, checks if approval is required and throws an error if it is not approved
*
* @param compPO composite purchase order
*/
private void checkOrderApprovalRequired(CompositePurchaseOrder compPO, JsonObject tenantConfig, RequestContext requestContext) {
boolean isApprovalRequired = isApprovalRequiredConfiguration(tenantConfig);
if (isApprovalRequired && !compPO.getApproved().equals(Boolean.TRUE)) {
throw new HttpException(400, APPROVAL_REQUIRED_TO_OPEN);
}
compPO.setApprovedById(getCurrentUserId(requestContext));
compPO.setApprovalDate(new Date());
}
use of org.folio.rest.core.exceptions.HttpException in project mod-orders by folio-org.
the class PurchaseOrderLineHelper method createPoLine.
/**
* Creates PO Line if its content is valid and all restriction checks passed
* @param compPOL {@link CompositePoLine} to be created
* @return completable future which might hold {@link CompositePoLine} on success, {@code null} if validation fails or an exception if any issue happens
*/
public CompletableFuture<CompositePoLine> createPoLine(CompositePoLine compPOL, RequestContext requestContext) {
// Validate PO Line content and retrieve order only if this operation is allowed
JsonObject cachedTenantConfiguration = new JsonObject();
return configurationEntriesService.loadConfiguration(ORDER_CONFIG_MODULE_NAME, requestContext).thenApply(tenantConfiguration -> cachedTenantConfiguration.mergeIn(tenantConfiguration, true)).thenCompose(tenantConfiguration -> setTenantDefaultCreateInventoryValues(compPOL, tenantConfiguration)).thenCompose(v -> validateNewPoLine(compPOL, cachedTenantConfiguration, requestContext)).thenCompose(validationErrors -> {
if (CollectionUtils.isEmpty(validationErrors)) {
return getCompositePurchaseOrder(compPOL.getPurchaseOrderId(), requestContext).thenApply(this::validateOrderState).thenCompose(po -> protectionService.isOperationRestricted(po.getAcqUnitIds(), ProtectedOperationType.CREATE, requestContext).thenApply(vVoid -> po)).thenCompose(po -> createPoLine(compPOL, po, requestContext));
} else {
Errors errors = new Errors().withErrors(validationErrors).withTotalRecords(validationErrors.size());
logger.error("Create POL validation error : " + JsonObject.mapFrom(errors).encodePrettily());
throw new HttpException(RestConstants.VALIDATION_ERROR, errors);
}
});
}
use of org.folio.rest.core.exceptions.HttpException in project mod-orders by folio-org.
the class PurchaseOrderLineHelper method handleSubObjsOperation.
private CompletableFuture<Void> handleSubObjsOperation(String prop, JsonObject updatedLine, JsonObject lineFromStorage, RequestContext requestContext) {
List<CompletableFuture<String>> futures = new ArrayList<>();
JsonArray idsInStorage = lineFromStorage.getJsonArray(prop);
JsonArray jsonObjects = updatedLine.getJsonArray(prop);
// Handle updated sub-objects content
if (jsonObjects != null && !jsonObjects.isEmpty()) {
// Clear array of object which will be replaced with array of id's
updatedLine.remove(prop);
for (int i = 0; i < jsonObjects.size(); i++) {
JsonObject subObj = jsonObjects.getJsonObject(i);
if (subObj != null && subObj.getString(ID) != null) {
String id = idsInStorage.remove(subObj.getString(ID)) ? subObj.getString(ID) : null;
futures.add(handleSubObjOperation(prop, subObj, id, requestContext).exceptionally(throwable -> {
Error error = handleProcessingError(throwable, prop, id);
throw new HttpException(500, error);
}));
}
}
}
// The remaining unprocessed objects should be removed
for (int i = 0; i < idsInStorage.size(); i++) {
String id = idsInStorage.getString(i);
if (id != null) {
futures.add(handleSubObjOperation(prop, null, id, requestContext).exceptionally(throwable -> {
handleProcessingError(throwable, prop, id);
// In case the object is not deleted, still keep reference to old id
return id;
}));
}
}
return collectResultsOnSuccess(futures).thenAccept(newIds -> updatedLine.put(prop, newIds));
}
Aggregations