Best answer

Javascript code action - is it possible to throw a HaltException?

  • 23 February 2023
  • 11 replies
  • 263 views

If I have a javascript code step in a Zap, can I throw an exception to get the Zap to halt? I found there was a  HaltException and/or HaltError in the CLI, but wasn’t sure if it was available in the WebUI code steps.

Here’s my scenario - I have a javascript code block that maps inputs to a location ID. Works great, when the location is found. When the location isn’t found, I can return -1 or something that causes an error in one of the later steps… but I know at the current step that I have a problem - and I’d like to just stop the Zap at that point so I can investigate or fix the location ID mapping.

Oh - I guess I could get the Zap to stop processing by doing something silly like this:

if( locationID == null ) {

  someObviousSyntaxException();

}

It will cause the execution to fail because the method isn’t found, but I hope there is a better way to stop the Zap from continuing.

icon

Best answer by Troy Tessalone 23 February 2023, 22:34

View original

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

11 replies

Userlevel 6
Badge +8

@Jason G. Sounds like you’ve come up with a fine solution :)

Still, one way around the auto-shutoff issue is to set up a second Zap with a Zapier Manager trigger for a “Zap Turned Off” event and a subsequent action to toggle it back on (as long as the error it’s hitting is the one you created and not an authentication issue accessing one of your connected apps).

Yes @Todd Harper, I thought of that, it does show up at Stopped/Errored status.

It looks like stopped/errored is a problem for the Zap if it exceeds 95% of runs, which I would be way way below… but I’m not thrilled with that approach. I wouldn’t want the Zap to be disabled if I missed the error notifications...

What I decided on was to make it easier to run the subsequent steps in my zap, so I wouldn’t have such a strong desire to re-run from the code step. 

Userlevel 6
Badge +8

Hi @Jason G.,

Have you tried throwing an error in your code? I haven’t tested this, so not sure it would do what you need, but it could be one way of effectively using your code to halt the Zap (though, I suppose it would actually result in a Stopped/Errored status instead of a Stopped/Halted status...)

Ok, thanks for the answer - there is no way to Halt in a Code step.

 

Userlevel 7
Badge +14

@Jason G.

Maybe have Paths in your Zap that act as a success path and a halt path.

Paths (and the Steps within a Path) can be duplicated.

 

Or a possible workaround, is to have a duplicate Zap, then you can take the data from the Zap Run and paste it into the alternative Zap to rerun.

 

Userlevel 7
Badge +14

@Jason G.

A Code step will either result in a success or error.

You can’t Halt a Zap from continuing to run via a Code step.

If the Zap Code step results in an error, only Zap steps that rely on the output from the Code step will not try to run.

Thanks for all the ideas of working around this, but I’m actually still trying to figure out if I can do a Halt or not from within my javascript code. There are benefits to Halting - I can change code and re-run with auto-replay, among others.

 

I would not use the Formatter > Text > Default Value -- I can just set a default value in my code if I wanted that.

I would not use a Zapier Tables - I already have the equivalent of a table in my code.

I would not use Manual Approval - I want this to run automatically the 99% of the time that the location mapping works just fine.

Userlevel 7
Badge +14

@Jason G.

Otherwise you may want to look into using Zapier Tables: https://zapier.com/early-access

Help article about how to manually approve Zap Runs: https://help.zapier.com/hc/en-us/articles/8496295323277

Userlevel 7
Badge +14

@Jason G. 

Perhaps add a following Formatter > Text > Default Value step instead.

Thanks - I had considered that. But I actually want the Zap to halt.

If it halts at that step, I can change the code (to add a new location mapping) and re-run that step with auto-replay.

I can also more easily find it in the Zap History. If it’s filtered out, it might be easier to overlook it as an error condition.

Userlevel 7
Badge +14

Hi @Jason G. 

Good question.

Try adding a Filter step after the Code step: https://zapier.com/apps/filter/help