Cancel a transaction

How do you cancel an app.transaction?

The documentation says that if your function fails it will back out, but it doesn’t say what constitutes a failure… A common practice would be to return false, but I’ve tried that and my error dialog shows correctly, but some of the actions executed so far persist (i.e. the transaction isn’t cancelled).

Hi @Jay, we don’t offer the “rollback” functionality in the Lua API yet. I think returning false can be a good option :+1:

2 Likes

@Jay, I was just testing, and it looks like just doing an error() cancels/rollbacks the transaction:

app.transaction(function()
    local spr = app.sprite
    local w, h = spr.width, spr.height
    spr:resize(8, 8)
    spr:resize(w, h)
    error()
  end)

That’s great - it works. I present a Dialog to the user with a description/instructions about the problem, so they can try again, and this dialog still shows even if you do the error() after presenting it, which is perfect.

I would suggest making it clear in the docs for transaction that calling error() is the correct way to cause an explicit failure. It does say “if the function fails”, but since Lua is one of those languages you can just pick up and use without having actually learned it, it’s probably not obvious to inexperienced Lua users (like me) that’s how you deliberately cause a failure. :slight_smile:

1 Like

Yes I want to update the docs given that we can use error()to abort/rollback the transaction, but I wasn’t aware of this possibility. Actually the rollback mechanism was done just in case any other error in the Lua script happen (e.g. a syntax error or a function fails inside the transaction). But it can work explicitly with error() as we saw.