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

#1

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!”)
}

0 Likes

#2

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

1 Like

#3

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

0 Likes

#4

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

0 Likes

#5

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

0 Likes

#6

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

0 Likes

#7

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

#8

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

0 Likes