1
0
mirror of https://github.com/oceanprotocol/ocean.js.git synced 2024-11-26 20:39:05 +01:00

Fix/ Order method should throw error messages (#803)

* throw error when order methods fail, updated integration tests accordingly

* throw error for startOrder method & update error messages

* updated assert messages and added asserts in try block
This commit is contained in:
Bogdan Fazakas 2021-05-25 17:00:13 +03:00 committed by GitHub
parent fd01736802
commit a6c9c70e8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 127 additions and 77 deletions

View File

@ -449,7 +449,7 @@ export class DataTokens {
return trxReceipt return trxReceipt
} catch (e) { } catch (e) {
this.logger.error(`ERROR: Failed to start order : ${e.message}`) this.logger.error(`ERROR: Failed to start order : ${e.message}`)
return null throw new Error(`Failed to start order: ${e.message}`)
} }
} }

View File

@ -488,7 +488,10 @@ export class Assets extends Instantiable {
serviceIndex, serviceIndex,
service.serviceEndpoint 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) { if (searchPreviousOrders) {
const previousOrder = await this.ocean.datatokens.getPreviousValidOrders( const previousOrder = await this.ocean.datatokens.getPreviousValidOrders(
providerData.dataToken, providerData.dataToken,
@ -510,7 +513,12 @@ export class Assets extends Instantiable {
' but balance is ' + ' but balance is ' +
balance.toString() 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( const txid = await this.ocean.datatokens.startOrder(
providerData.dataToken, providerData.dataToken,
@ -522,9 +530,9 @@ export class Assets extends Instantiable {
) )
if (txid) return txid.transactionHash if (txid) return txid.transactionHash
} catch (e) { } 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 // marketplace flow

View File

@ -516,11 +516,16 @@ export class Compute extends Instantiable {
return new SubscribablePromise(async (observer) => { return new SubscribablePromise(async (observer) => {
// first check if we can order this // first check if we can order this
const allowed = await this.isOrderable(datasetDid, serviceIndex, algorithm) 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 ddo: DDO = await this.ocean.assets.resolve(datasetDid)
// const service: Service = ddo.findServiceByType('compute') // const service: Service = ddo.findServiceByType('compute')
const service: Service = ddo.findServiceById(serviceIndex) const service: Service = ddo.findServiceById(serviceIndex)
if (!service) return null if (!service)
throw new Error(`Dataset order failed, Could not find service for the DDO`)
try {
const order = await this.ocean.assets.order( const order = await this.ocean.assets.order(
datasetDid, datasetDid,
service.type, service.type,
@ -531,6 +536,10 @@ export class Compute extends Instantiable {
searchPreviousOrders searchPreviousOrders
) )
return order return order
} catch (error) {
this.logger.error(`ERROR: Failed to order: ${error.message}`)
throw new Error(`Failed to order dataset: ${error.message}`)
}
}) })
} }
@ -554,6 +563,7 @@ export class Compute extends Instantiable {
searchPreviousOrders = true searchPreviousOrders = true
): Promise<string> { ): Promise<string> {
// this is only a convienince function, which calls ocean.assets.order // this is only a convienince function, which calls ocean.assets.order
try {
return await this.ocean.assets.order( return await this.ocean.assets.order(
did, did,
serviceType, serviceType,
@ -563,6 +573,10 @@ export class Compute extends Instantiable {
consumerAddress, consumerAddress,
searchPreviousOrders searchPreviousOrders
) )
} catch (error) {
this.logger.error(`ERROR: Failed to orderAlgorithm: ${error.message}`)
throw new Error(`Failed to order algorithm: ${error.message}`)
}
} }
/** /**

View File

@ -934,6 +934,7 @@ describe('Compute flow', () => {
const algoDefinition: ComputeAlgorithm = { const algoDefinition: ComputeAlgorithm = {
meta: algorithmMeta meta: algorithmMeta
} }
try {
const order = await ocean.compute.orderAsset( const order = await ocean.compute.orderAsset(
bob.getId(), bob.getId(),
datasetNoRawAlgo.id, datasetNoRawAlgo.id,
@ -943,6 +944,9 @@ describe('Compute flow', () => {
computeAddress // CtD is the consumer of the dataset computeAddress // CtD is the consumer of the dataset
) )
assert(order === null, 'Order should be null') 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 () => { 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') const service1 = datasetWithTrustedAlgo.findServiceByType('compute')
@ -965,6 +969,7 @@ describe('Compute flow', () => {
assert(allowed === false) assert(allowed === false)
// try even futher, since we now this should fail // try even futher, since we now this should fail
try {
const order = await ocean.compute.orderAsset( const order = await ocean.compute.orderAsset(
bob.getId(), bob.getId(),
datasetWithTrustedAlgo.id, datasetWithTrustedAlgo.id,
@ -974,6 +979,9 @@ describe('Compute flow', () => {
computeAddress // CtD is the consumer of the dataset computeAddress // CtD is the consumer of the dataset
) )
assert(order === null, 'Order should be null') 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 () => { it('should not allow a compute job with a published algo because asset does not allow allowAllPublishedAlgorithms', async () => {
@ -990,6 +998,8 @@ describe('Compute flow', () => {
algoDefinition algoDefinition
) )
assert(allowed === false) assert(allowed === false)
try {
const order = await ocean.compute.orderAsset( const order = await ocean.compute.orderAsset(
bob.getId(), bob.getId(),
ddo.id, ddo.id,
@ -999,6 +1009,9 @@ describe('Compute flow', () => {
computeAddress // CtD is the consumer of the dataset computeAddress // CtD is the consumer of the dataset
) )
assert(order === null, 'Order should be null') assert(order === null, 'Order should be null')
} catch (error) {
assert(error != null, 'Order should throw error')
}
}) })
it('Alice updates Compute Privacy', async () => { it('Alice updates Compute Privacy', async () => {
@ -1084,6 +1097,7 @@ describe('Compute flow', () => {
algoDefinition algoDefinition
) )
assert(allowed === false) assert(allowed === false)
try {
const order = await ocean.compute.orderAsset( const order = await ocean.compute.orderAsset(
bob.getId(), bob.getId(),
ddo.id, ddo.id,
@ -1093,6 +1107,9 @@ describe('Compute flow', () => {
computeAddress // CtD is the consumer of the dataset computeAddress // CtD is the consumer of the dataset
) )
assert(order === null, 'Order should be null') 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 () => { it('should start a compute job with a published algo that has a compute service', async () => {
const output = {} const output = {}
@ -1543,7 +1560,9 @@ describe('Compute flow', () => {
) )
assert(allowed === false) assert(allowed === false)
// we know that it is not Orderable, but we are trying to force it // we know that it is not Orderable, but we are trying to force it
computeOrderId = await ocean.compute.orderAsset(
try {
const order = await ocean.compute.orderAsset(
bob.getId(), bob.getId(),
datasetWithBogusProvider.id, datasetWithBogusProvider.id,
computeService.index, computeService.index,
@ -1551,7 +1570,11 @@ describe('Compute flow', () => {
null, // no marketplace fee null, // no marketplace fee
computeAddress // CtD is the consumer of the dataset computeAddress // CtD is the consumer of the dataset
) )
assert(computeOrderId === null, 'computeOrderId !== null') assert(order === null, 'Order should be null')
} catch (error) {
assert(error != null, 'Order should throw error')
}
// we are forcing a bogus orderId // we are forcing a bogus orderId
computeOrderId = '1234' computeOrderId = '1234'
const response = await ocean.compute.start( const response = await ocean.compute.start(

View File

@ -540,12 +540,17 @@ describe('Marketplace flow', () => {
}) })
it('Bob tries to consumes asset with bad URL, but tokens are not deducted', async () => { it('Bob tries to consumes asset with bad URL, but tokens are not deducted', async () => {
const balance = await datatoken.balance(tokenAddressForBadUrlAsset, bob.getId()) const balance = await datatoken.balance(tokenAddressForBadUrlAsset, bob.getId())
const txid = await ocean.assets.order( try {
const order = await ocean.assets.order(
ddoWithBadUrl.id, ddoWithBadUrl.id,
accessService.type, accessService.type,
bob.getId() bob.getId()
) )
assert(txid === null) assert(order === null, 'Order should be null')
} catch (error) {
assert(error != null, 'Order should throw error')
}
const balanceAfterOrder = await datatoken.balance( const balanceAfterOrder = await datatoken.balance(
tokenAddressForBadUrlAsset, tokenAddressForBadUrlAsset,
bob.getId() bob.getId()