diff --git a/src/datatokens/Datatokens.ts b/src/datatokens/Datatokens.ts index 8759a91c..bd625424 100644 --- a/src/datatokens/Datatokens.ts +++ b/src/datatokens/Datatokens.ts @@ -449,7 +449,7 @@ export class DataTokens { return trxReceipt } catch (e) { this.logger.error(`ERROR: Failed to start order : ${e.message}`) - return null + throw new Error(`Failed to start order: ${e.message}`) } } diff --git a/src/ocean/Assets.ts b/src/ocean/Assets.ts index 32a3e4a4..46fe5490 100644 --- a/src/ocean/Assets.ts +++ b/src/ocean/Assets.ts @@ -488,7 +488,10 @@ export class Assets extends Instantiable { serviceIndex, service.serviceEndpoint ) - if (!providerData) return null + if (!providerData) + throw new Error( + `Order asset failed, Failed to initialize service to compute totalCost for ordering` + ) if (searchPreviousOrders) { const previousOrder = await this.ocean.datatokens.getPreviousValidOrders( providerData.dataToken, @@ -510,7 +513,12 @@ export class Assets extends Instantiable { ' but balance is ' + balance.toString() ) - return null + throw new Error( + 'ERROR: Not enough funds Needed ' + + totalCost.toString() + + ' but balance is ' + + balance.toString() + ) } const txid = await this.ocean.datatokens.startOrder( providerData.dataToken, @@ -522,9 +530,9 @@ export class Assets extends Instantiable { ) if (txid) return txid.transactionHash } catch (e) { - this.logger.error(`ERROR: Failed to order: ${e.message}`) + this.logger.error(`ERROR: Failed to order a service : ${e.message}`) + throw new Error(`Failed to order a service: ${e.message}`) } - return null } // marketplace flow diff --git a/src/ocean/Compute.ts b/src/ocean/Compute.ts index cb807d2d..4c7c4f9d 100644 --- a/src/ocean/Compute.ts +++ b/src/ocean/Compute.ts @@ -516,21 +516,30 @@ export class Compute extends Instantiable { return new SubscribablePromise(async (observer) => { // first check if we can order this const allowed = await this.isOrderable(datasetDid, serviceIndex, algorithm) - if (!allowed) return null + if (!allowed) + throw new Error( + `Dataset order failed, dataset is not orderable with the specified algorithm` + ) const ddo: DDO = await this.ocean.assets.resolve(datasetDid) // const service: Service = ddo.findServiceByType('compute') const service: Service = ddo.findServiceById(serviceIndex) - if (!service) return null - const order = await this.ocean.assets.order( - datasetDid, - service.type, - consumerAccount, - -1, - mpAddress, - computeAddress, - searchPreviousOrders - ) - return order + if (!service) + throw new Error(`Dataset order failed, Could not find service for the DDO`) + try { + const order = await this.ocean.assets.order( + datasetDid, + service.type, + consumerAccount, + -1, + mpAddress, + computeAddress, + searchPreviousOrders + ) + return order + } catch (error) { + this.logger.error(`ERROR: Failed to order: ${error.message}`) + throw new Error(`Failed to order dataset: ${error.message}`) + } }) } @@ -554,15 +563,20 @@ export class Compute extends Instantiable { searchPreviousOrders = true ): Promise { // this is only a convienince function, which calls ocean.assets.order - return await this.ocean.assets.order( - did, - serviceType, - payerAddress, - serviceIndex, - mpAddress, - consumerAddress, - searchPreviousOrders - ) + try { + return await this.ocean.assets.order( + did, + serviceType, + payerAddress, + serviceIndex, + mpAddress, + consumerAddress, + searchPreviousOrders + ) + } catch (error) { + this.logger.error(`ERROR: Failed to orderAlgorithm: ${error.message}`) + throw new Error(`Failed to order algorithm: ${error.message}`) + } } /** diff --git a/test/integration/ComputeFlow.test.ts b/test/integration/ComputeFlow.test.ts index 65329b00..fbdb7589 100644 --- a/test/integration/ComputeFlow.test.ts +++ b/test/integration/ComputeFlow.test.ts @@ -934,15 +934,19 @@ describe('Compute flow', () => { const algoDefinition: ComputeAlgorithm = { meta: algorithmMeta } - const order = await ocean.compute.orderAsset( - bob.getId(), - datasetNoRawAlgo.id, - service1.index, - algoDefinition, - null, // no marketplace fee - computeAddress // CtD is the consumer of the dataset - ) - assert(order === null, 'Order should be null') + try { + const order = await ocean.compute.orderAsset( + bob.getId(), + datasetNoRawAlgo.id, + service1.index, + algoDefinition, + null, // no marketplace fee + computeAddress // CtD is the consumer of the dataset + ) + assert(order === null, 'Order should be null') + } catch (error) { + assert(error != null, 'Order should throw error') + } }) it('should not allow order the compute service with algoDid != "did:op:1234" for dataset that allows only "did:op:1234" as algo', async () => { const service1 = datasetWithTrustedAlgo.findServiceByType('compute') @@ -965,15 +969,19 @@ describe('Compute flow', () => { assert(allowed === false) // try even futher, since we now this should fail - const order = await ocean.compute.orderAsset( - bob.getId(), - datasetWithTrustedAlgo.id, - service1.index, - algoDefinition, - null, // no marketplace fee - computeAddress // CtD is the consumer of the dataset - ) - assert(order === null, 'Order should be null') + try { + const order = await ocean.compute.orderAsset( + bob.getId(), + datasetWithTrustedAlgo.id, + service1.index, + algoDefinition, + null, // no marketplace fee + computeAddress // CtD is the consumer of the dataset + ) + assert(order === null, 'Order should be null') + } catch (error) { + assert(error != null, 'Order should throw error') + } }) it('should not allow a compute job with a published algo because asset does not allow allowAllPublishedAlgorithms', async () => { @@ -990,15 +998,20 @@ describe('Compute flow', () => { algoDefinition ) assert(allowed === false) - const order = await ocean.compute.orderAsset( - bob.getId(), - ddo.id, - computeService.index, - algoDefinition, - null, // no marketplace fee - computeAddress // CtD is the consumer of the dataset - ) - assert(order === null, 'Order should be null') + + try { + const order = await ocean.compute.orderAsset( + bob.getId(), + ddo.id, + computeService.index, + algoDefinition, + null, // no marketplace fee + computeAddress // CtD is the consumer of the dataset + ) + assert(order === null, 'Order should be null') + } catch (error) { + assert(error != null, 'Order should throw error') + } }) it('Alice updates Compute Privacy', async () => { @@ -1084,15 +1097,19 @@ describe('Compute flow', () => { algoDefinition ) assert(allowed === false) - const order = await ocean.compute.orderAsset( - bob.getId(), - ddo.id, - computeService.index, - algoDefinition, - null, // no marketplace fee - computeAddress // CtD is the consumer of the dataset - ) - assert(order === null, 'Order should be null') + try { + const order = await ocean.compute.orderAsset( + bob.getId(), + ddo.id, + computeService.index, + algoDefinition, + null, // no marketplace fee + computeAddress // CtD is the consumer of the dataset + ) + assert(order === null, 'Order should be null') + } catch (error) { + assert(error != null, 'Order should throw error') + } }) it('should start a compute job with a published algo that has a compute service', async () => { const output = {} @@ -1543,15 +1560,21 @@ describe('Compute flow', () => { ) assert(allowed === false) // we know that it is not Orderable, but we are trying to force it - computeOrderId = await ocean.compute.orderAsset( - bob.getId(), - datasetWithBogusProvider.id, - computeService.index, - algoDefinition, - null, // no marketplace fee - computeAddress // CtD is the consumer of the dataset - ) - assert(computeOrderId === null, 'computeOrderId !== null') + + try { + const order = await ocean.compute.orderAsset( + bob.getId(), + datasetWithBogusProvider.id, + computeService.index, + algoDefinition, + null, // no marketplace fee + computeAddress // CtD is the consumer of the dataset + ) + assert(order === null, 'Order should be null') + } catch (error) { + assert(error != null, 'Order should throw error') + } + // we are forcing a bogus orderId computeOrderId = '1234' const response = await ocean.compute.start( diff --git a/test/integration/Marketplaceflow.test.ts b/test/integration/Marketplaceflow.test.ts index 61dd7d2b..984fef49 100644 --- a/test/integration/Marketplaceflow.test.ts +++ b/test/integration/Marketplaceflow.test.ts @@ -540,12 +540,17 @@ describe('Marketplace flow', () => { }) it('Bob tries to consumes asset with bad URL, but tokens are not deducted', async () => { const balance = await datatoken.balance(tokenAddressForBadUrlAsset, bob.getId()) - const txid = await ocean.assets.order( - ddoWithBadUrl.id, - accessService.type, - bob.getId() - ) - assert(txid === null) + try { + const order = await ocean.assets.order( + ddoWithBadUrl.id, + accessService.type, + bob.getId() + ) + assert(order === null, 'Order should be null') + } catch (error) { + assert(error != null, 'Order should throw error') + } + const balanceAfterOrder = await datatoken.balance( tokenAddressForBadUrlAsset, bob.getId()