-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feature request?] redspot-chai - detect Errors returned from blockchain #78
Comments
Have you tried
|
const result = await contract.tx.someFunc(param, { signer: sender1 })
result.error // Is it possible to get the result? Can `Error::AccessDenied ` be determined by `result.error`? |
Hi @ii-ii-ii Thanks for the suggestions. I was trying to figure this out about a month ago, and tried these methods but, unfortunately, they do not work. So I cheated and tried checking I tried a bunch of stuff. To set the scene: // Types Definations ..
Error: {
_enum: [ 'AccessDenied', 'OtherError']
}, First Try// ..... Testing Template Code in smartcontract.test.ts
it('test error catching', async() => {
const { contract, sender1, sender2 } = await setup();
await expect(contract.tx.someFunc(...parms, { signer: unauthorizedSigner }))
.to.not.emit(contract, 'Success')
}) Result Second Try// ..... Testing Template Code in smartcontract.test.ts
it('test error catching', async() => {
const { contract, sender1, sender2 } = await setup();
try {
const result = await contract.tx.someFunc(...parms, { signer: unauthorizedSigner })
console.log(result)
} catch(err) {
console.error('error', err)
}
}) Result
Nothing is thrown. Third Try// ..... Testing Template Code in smartcontract.test.ts
it('test error catching', async() => {
const { contract, sender1, sender2 } = await setup();
try {
const result = await contract.query.someFunc(...parms, { signer: unauthorizedSigner })
console.log('unauthorizedSigner', result.output?.toHuman())
} catch(err) {
console.error('error', err)
}
try {
const result = await contract.query.someFunc(...parms, { signer: authorizedSigner })
console.log('authorizedSigner', result.output?.toHuman())
} catch (err) {
console.error('error', err)
}
}) Result
However, the blockchain state is not mutated. No changes is written to the smart contract storage. Conclusion?My guess is that because of the So, I guess two possible solutions:
|
This is related to use-ink/ink#641 Thus currently chai could only do like solidity testcase. As a common convention, we only print the event of the contract in the branch where the logic is executed correctly. Exceptions generally use Thus if call current transaction do not print the related event, then it must meet an error. However this way do not know the reason of the error, and not like ethereum EVM, the assert string do not print in Wasm executor. The final way to resolve this thing is waiting ink fix issue#641, need a protocol for We do not think the mock call should be integrated into chai, the developers should write this logic by themself requirements. And we advice this:
|
Dirty method it is. Closing this issue for now. |
It seems like the only way to catch an error thrown by blockchain is by the fact that the blockchain does not emit a success event.
For the following code: if
someFunc
is executed successfully, it will emit an eventSomeSuccessEvent
. If failed, depends on the underlying logics ofsomeFunc
, it can throwError::AccessDenied
orError::NotFound
For instance, when
sender1
is not authorized to executesomeFunc
, the smart contract shall emitError::AccessDenied
, but the only way to capture it seems to be trying to verify that the blockchain does not emitSomeSuccessEvent
.Let me know if I'm mistaken and how to do it the right way. Otherwise, I would very much to see redspot-chai implement such functionality.
The text was updated successfully, but these errors were encountered: