In the script, how to abort following steps of an action?

Previously in drafts 4, I used “throw” to raise an exception to abort following steps of an action, and modify the draft content to show exception info.

But now, the throw code will cause modify draft content code to be invalid.

like this:

if (allText.length == 0) {
draft.content = “…” // show help info, this worked in drafts 4 but not in drafts 5!!!
draft.update()
throw(“No text, show help info!”)
}

It sounds to me like what you are looking for is the context object.

1 Like

Great, but I still can’t modify the draft.conent property before called context.cancel() or context.fail().

Are you calling draft.update() after making your changes?

Yes I called. And it’s ok in drafts 4, but not work in drafts 5.

Is the condition valid ?
‘allText.length == 0’ ?

I think it is being modified, but for a cancel or fail the implicit reload of the draft into the editor won’t kick in, so you’ll need to do it manually.

See if this example helps.

draft.content = draft.content + "\n// Test Line //"
draft.update()
editor.load(draft)
context.fail("Did it still update the draft?")
1 Like

This works! Thanks. The editor has to be loaded manually.