Can we return Error and trap

I was wondering how we could return an Error variant and then trap so the canister would roll back to its previous state safety.

Returning a Result is important for nice error handling form the consumer side but a Rust error is just an enum and the update to the state that occurred before the error are kept.

If it is not possible right now, might be cool to have a way to do it in the cdk.

P.S. : I know I can do it from my side by using temporary states and only commit the state at the end of my call, when nothing else can fail. But still, this feature looks nice and shiny in my developer experience dreamland.

You can trap by calling by ic_cdk::api::trap(&str), but the state won’t rollback.

I meant combining the Err variant of a Result enum with the trap api call.
Effectively returning this Err like a normal Rust function do for nice error handling and trap behind the scene.

FYI, From the documentation :

A trap will only revoke changes made since the last commit point.

So trap roll back the state unless it’s used in multiple async calls, where its behavior need to be treated carefully.

@ulan @diegop @dsarlis Maybe one of you guys will know :slight_smile:

Let me ask folks internally.

According to @roman-kashitsyn , it’s not possible at the moment.

2 Likes