Raising an incident via inline script vs incident generated by exception

Hi, we have an http connector with an inline script to process the response:

var response = connector.getVariable(“response”);
var statusCode = connector.getVariable(“statusCode”);

if (statusCode != 200) {
throw new Error(connector.getVariable(“response”));
}
else {
JSON.parse(response).value;
}

If the statusCode is not 200 this results in an incident and the workflow stops, which is what we want. However there is a lot of information logged in via a ScriptEvaluationException that is very verbose and not really needed.

I had a look at: Error creating incident via javascript - #3 by StephenOTT but this seems to have been classified as a “hack”.

Is there a way to create an incident and avoid having to throw the Error in order to get the workflow to stop?

Look at the delegate execution API and the comments further down in the mentioned forum post: the execution.createIncident() method replaced the need to use the internal API.

https://docs.camunda.org/javadoc/camunda-bpm-platform/7.10/org/camunda/bpm/engine/delegate/DelegateExecution.html See the createIncident() methods

Hi

@StephenOTT, thank you very much for the suggestion.
We have attempted to use the createIncident method in the output parameter of http-connector.

var statusCode = connector.getVariable("statusCode");
if (statusCode != 201) {
  execution.createIncident();
}

From further reading we’ve found that the execution variable is not accessible in the http-connector’s scope as described in this thread Error creating incident via javascript and JIRA https://app.camunda.com/jira/browse/CAM-8937 you posted.

I’m a bit confused by this comment “Look at the delegate execution API and the comments further down in the mentioned forum post: the execution.createIncident() method replaced the need to use the internal API.” Is there another way we can invoke " execution.createIncident()" in the http-connector’s scope?

Thanks in advance.

You can use Create and Resolve Incidents through Scripts(Javascript examples) (Internal API) to get the execution/parent scope. But this is internal API. I would recommend that you just use a end-execution listener that Evals a local variable in the connector to veal the status code. Or recreate http connector as a java delegate or look at: Replacing Http-Connector with Jsoup usage

Do you have an example on how to setup the execution listener (checking if server error status code then creating incident) with the http-connector?

I have the following configuration just to check if an incident gets created but the process just moves on and the incident is never visible in the cockpit.

@StephenOTT I have found your post stating that execution.createIncident() is non-blocking.

So I tried to throw an exception with JavaScript:

throw new Exception();

Then I got the error message:

Unable to evaluate script while executing..

When I used throw new java.lang.Exception(“exception message”); I got the message:

Unable to evaluate script while executing activity 'X' in the process definition with id 'X':<eval>:1:30 Expected an operand but found error throw new java.lang.Exception(“exception message”); ^ in <eval> at line number 1 at column number 30

First of all how I can throw an exception in a script task (just to test it) and what exceptions can be thrown?

Hi adiii4,
Here’s a working throwing :slight_smile:

I tried your example but unfortunately it didn’t work. I even created a new process with only a script task and it still gives me the following error message:

Unable to evaluate script while executing activity 'Task_0k44fns' in the process definition with id 'IncidientExample:1:d31f7ba8-3064-11ea-90fb-005056031c08':org.camunda.bpm.engine.ScriptEvaluationException: Incident happened in <eval> at line number 1 at column number 0

here is the created process and the script task configuration:

The weird thing is that this error only shows when I try to throw some kind of exception. Why could that be?