org.camunda.bpm.engine.delegate.BpmnError Java Examples

The following examples show how to use org.camunda.bpm.engine.delegate.BpmnError. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FailingOnLastRetryAspect.java    From flowing-retail with Apache License 2.0 6 votes vote down vote up
@Around("@annotation(FailingOnLastRetry)")
public Object guardedExecute(ProceedingJoinPoint joinPoint) throws Throwable {
  JobExecutorContext jobExecutorContext = Context.getJobExecutorContext();
  if (jobExecutorContext!=null && jobExecutorContext.getCurrentJob()!=null) {
    // this is called from a Job
    if (jobExecutorContext.getCurrentJob().getRetries()<=1) {
      // and the job will run out of retries when it fails again
      try {
        return joinPoint.proceed();
      } catch (Exception ex) {
        // Probably save the exception somewhere
        throw new BpmnError(NO_RETRIES_ERROR);
      }
    }      
  }
  // otherwise normal behavior (including retries possibly)
  return joinPoint.proceed();
}
 
Example #2
Source File: ChargeCreditCardAdapter.java    From flowing-retail with Apache License 2.0 6 votes vote down vote up
public void execute(DelegateExecution ctx) throws Exception {
  CreateChargeRequest request = new CreateChargeRequest();
  request.amount = (int) ctx.getVariable("remainingAmount");

  CreateChargeResponse response = rest.postForObject( //
      stripeChargeUrl, //
      request, //
      CreateChargeResponse.class);
  
  if (response.errorCode != null && response.errorCode.length() > 0) {
    // raise error to be handled in BPMN model in case there was an error in credit card handling
    ctx.setVariable("creditCardErrorCode", response.errorCode);
    throw new BpmnError("Error_CreditCardError");
  }

  ctx.setVariable("paymentTransactionId", response.transactionId);
}
 
Example #3
Source File: ChargeCreditCardDegradingAdapter.java    From flowing-retail with Apache License 2.0 6 votes vote down vote up
@FailingOnLastRetry
public void execute(DelegateExecution ctx) throws Exception {
  CreateChargeRequest request = new CreateChargeRequest();
  request.amount = (int) ctx.getVariable("remainingAmount");

  CreateChargeResponse response = rest.postForObject( //
      stripeChargeUrl, //
      request, //
      CreateChargeResponse.class);
  
  if (response.errorCode != null && response.errorCode.length() > 0) {
    // raise error to be handled in BPMN model in case there was an error in credit card handling
    ctx.setVariable("creditCardErrorCode", response.errorCode);
    throw new BpmnError("Error_CreditCardError");
  }

  ctx.setVariable("paymentTransactionId", response.transactionId);
}
 
Example #4
Source File: ChargeCreditCardAdapter.java    From flowing-retail with Apache License 2.0 6 votes vote down vote up
public void execute(DelegateExecution ctx) throws Exception {
  CreateChargeRequest request = new CreateChargeRequest();
  request.amount = (int) ctx.getVariable("remainingAmount");

  CreateChargeResponse response = rest.postForObject( //
      stripeChargeUrl, //
      request, //
      CreateChargeResponse.class);   
  
  // TODO Add error scenarios to StripeFake and then raise "Error_CreditCardError" here
  if (response.errorCode!=null) {
    ctx.setVariable("errorCode", response.errorCode);
    throw new BpmnError("Error_PaymentError");
  }

  ctx.setVariable("paymentTransactionId", response.transactionId);
}
 
Example #5
Source File: SourceExecutableScript.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
@Override
public Object evaluate(ScriptEngine engine, VariableScope variableScope, Bindings bindings) {
  if (shouldBeCompiled) {
    compileScript(engine);
  }

  if (getCompiledScript() != null) {
    return super.evaluate(engine, variableScope, bindings);
  }
  else {
    try {
      return evaluateScript(engine, bindings);
    } catch (ScriptException e) {
      if (e.getCause() instanceof BpmnError) {
        throw (BpmnError) e.getCause();
      }
      String activityIdMessage = getActivityIdExceptionMessage(variableScope);
      throw new ScriptEvaluationException("Unable to evaluate script" + activityIdMessage + ":" + e.getMessage(), e);
    }
  }
}
 
Example #6
Source File: ConnectProcessEnginePluginTest.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
@Deployment(resources="org/camunda/connect/plugin/ConnectProcessEnginePluginTest.testConnectorBpmnErrorThrownInScriptResourceNoAsyncAfterJobIsCreated.bpmn")
public void testConnectorBpmnErrorThrownInScriptResourceNoAsyncAfterJobIsCreated() {
  // given
  Map<String, Object> variables = new HashMap<String, Object>();
  variables.put("throwInMapping", "in");
  variables.put("exception", new BpmnError("error"));

  // when
  runtimeService.startProcessInstanceByKey("testProcess", variables);

  // then
  // we will only reach the user task if the BPMNError from the script was handled by the boundary event
  Task task = taskService.createTaskQuery().singleResult();
  assertThat(task.getName(), is("User Task"));

  // no job is created
  assertThat(Long.valueOf(managementService.createJobQuery().count()), is(0l));
}
 
Example #7
Source File: BoundaryErrorEventTest.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
@Deployment(resources = {
        "org/camunda/bpm/engine/test/bpmn/event/error/BoundaryErrorEventTest.subprocess.bpmn20.xml"
})
public void testUncaughtError() {
  runtimeService.startProcessInstanceByKey("simpleSubProcess");
  Task task = taskService.createTaskQuery().singleResult();
  assertEquals("Task in subprocess", task.getName());

  try {
    // Completing the task will reach the end error event,
    // which is never caught in the process
    taskService.complete(task.getId());
  } catch (BpmnError e) {
    assertTextPresent("No catching boundary event found for error with errorCode 'myError', neither in same process nor in parent process", e.getMessage());
  }
}
 
Example #8
Source File: ThrowBpmnErrorDelegate.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
public void execute(DelegateExecution execution) throws Exception {
  Integer executionsBeforeError = (Integer) execution.getVariable("executionsBeforeError");
  Integer executions = (Integer) execution.getVariable("executions");
  Boolean exceptionType = (Boolean) execution.getVariable("exceptionType");
  if (executions == null) {
    executions = 0;
  }
  executions++;
  if (executionsBeforeError == null || executionsBeforeError < executions) {
    if (exceptionType != null && exceptionType) {
      throw new MyBusinessException("This is a business exception, which can be caught by a BPMN Error Event.");  
    } else {
      throw new BpmnError("23", "This is a business fault, which can be caught by a BPMN Error Event.");
    }
  } else {
    execution.setVariable("executions", executions);
  }
}
 
Example #9
Source File: BoundaryErrorEventTest.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
@Deployment(resources = {
        "org/camunda/bpm/engine/test/bpmn/event/error/BoundaryErrorEventTest.testUncaughtErrorOnCallActivity-parent.bpmn20.xml",
        "org/camunda/bpm/engine/test/bpmn/event/error/BoundaryErrorEventTest.subprocess.bpmn20.xml"
})
public void testUncaughtErrorOnCallActivity() {
  runtimeService.startProcessInstanceByKey("uncaughtErrorOnCallActivity");
  Task task = taskService.createTaskQuery().singleResult();
  assertEquals("Task in subprocess", task.getName());

  try {
    // Completing the task will reach the end error event,
    // which is never caught in the process
    taskService.complete(task.getId());
  } catch (BpmnError e) {
    assertTextPresent("No catching boundary event found for error with errorCode 'myError', neither in same process nor in parent process", e.getMessage());
  }
}
 
Example #10
Source File: ExecutionListenerTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Test
public void testThrowBpmnErrorInStartListenerOnModificationShouldNotTriggerPropagation() {
  // expect
  thrown.expect(BpmnError.class);
  thrown.expectMessage("business error");

  // given
  BpmnModelInstance model = Bpmn.createExecutableProcess(PROCESS_KEY)
      .startEvent()
      .userTask("userTask1")
      .subProcess("sub")
        .camundaExecutionListenerClass(ExecutionListener.EVENTNAME_START, ThrowBPMNErrorDelegate.class.getName())
        .embeddedSubProcess()
        .startEvent("inSub")
        .serviceTask("throw")
          .camundaExpression("${true}")
        .boundaryEvent("errorEvent1")
        .error(ERROR_CODE)
        .subProcessDone()
      .boundaryEvent("errorEvent2")
      .error(ERROR_CODE)
      .userTask("afterCatch")
      .endEvent("endEvent")
      .moveToActivity("sub")
      .userTask("afterSub")
      .endEvent()
      .done();
  DeploymentWithDefinitions deployment = testHelper.deploy(model);
  ProcessDefinition definition = deployment.getDeployedProcessDefinitions().get(0);

  ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(PROCESS_KEY);

  // when the listeners are invoked
  runtimeService.createModification(definition.getId()).startBeforeActivity("throw").processInstanceIds(processInstance.getId()).execute();
}
 
Example #11
Source File: ThrowBpmnErrorDelegate.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public void execute(DelegateExecution execution) throws Exception {
  Integer executionsBeforeError = (Integer) execution.getVariable("executionsBeforeError");
  Integer executions = (Integer) execution.getVariable("executions");
  if (executions == null) {
    executions = 0;
  }
  executions++;
  if (executionsBeforeError == null || executionsBeforeError < executions) {
    throw new BpmnError("23", "This is a business fault, which can be caught by a BPMN Error Event.");
  } else {
    execution.setVariable("executions", executions);
  }
}
 
Example #12
Source File: InputOutputTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Deployment(resources = "org/camunda/bpm/engine/test/bpmn/iomapping/InputOutputTest.testThrowErrorInScriptInputOutputMapping.bpmn")
public void FAILING_testBpmnErrorInScriptInputMapping() {
  Map<String, Object> variables = new HashMap<String, Object>();
  variables.put("throwInMapping", "in");
  variables.put("exception", new BpmnError("error"));
  runtimeService.startProcessInstanceByKey("testProcess", variables);
  //we will only reach the user task if the BPMNError from the script was handled by the boundary event
  Task task = taskService.createTaskQuery().singleResult();
  assertThat(task.getName(), is("User Task"));
}
 
Example #13
Source File: InputOutputTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Deployment(resources = "org/camunda/bpm/engine/test/bpmn/iomapping/InputOutputTest.testThrowErrorInScriptInputOutputMapping.bpmn")
public void FAILING_testBpmnErrorInScriptOutputMapping() {
  Map<String, Object> variables = new HashMap<String, Object>();
  variables.put("throwInMapping", "out");
  variables.put("exception", new BpmnError("error"));
  runtimeService.startProcessInstanceByKey("testProcess", variables);
  //we will only reach the user task if the BPMNError from the script was handled by the boundary event
  Task task = taskService.createTaskQuery().singleResult();
  assertThat(task.getName(), is("User Task"));
}
 
Example #14
Source File: ThrowErrorDelegate.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
protected void handle(ActivityExecution execution, String action) throws Exception {
  execution.setVariable(action, true);
  String type = (String) execution.getVariable("type");
  if ("error".equalsIgnoreCase(type)) {
    throw new BpmnError("MyError");
  }
  else if ("exception".equalsIgnoreCase(type)) {
    throw new MyBusinessException("MyException");
  }
  else if ("leave".equalsIgnoreCase(type)) {
    execution.setVariable("type", null);
    leave(execution);
  }
}
 
Example #15
Source File: BoundaryErrorEventTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Deployment(resources = {
        "org/camunda/bpm/engine/test/bpmn/event/error/BoundaryErrorEventTest.testCatchErrorThrownByJavaDelegateOnCallActivity-child.bpmn20.xml"
})
public void testUncaughtErrorThrownByJavaDelegateOnServiceTask() {
  try {
    runtimeService.startProcessInstanceByKey("catchErrorThrownByJavaDelegateOnCallActivity-child");
  } catch (BpmnError e) {
    assertTextPresent("No catching boundary event found for error with errorCode '23', neither in same process nor in parent process", e.getMessage());
  }
}
 
Example #16
Source File: BoundaryErrorEventTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Deployment(resources = {
        "org/camunda/bpm/engine/test/bpmn/event/error/BoundaryErrorEventTest.testUncaughtErrorThrownByJavaDelegateOnCallActivity-parent.bpmn20.xml",
        "org/camunda/bpm/engine/test/bpmn/event/error/BoundaryErrorEventTest.testCatchErrorThrownByJavaDelegateOnCallActivity-child.bpmn20.xml"
})
public void testUncaughtErrorThrownByJavaDelegateOnCallActivity() {
  try {
    runtimeService.startProcessInstanceByKey("uncaughtErrorThrownByJavaDelegateOnCallActivity-parent");
  } catch (BpmnError e) {
    assertTextPresent("No catching boundary event found for error with errorCode '23', neither in same process nor in parent process", e.getMessage());
  }
}
 
Example #17
Source File: BpmnErrorTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreation_ErrorCodeOnly() {
  // when
  BpmnError bpmnError = new BpmnError(ERROR_CODE);
  
  // then
  assertThat(bpmnError).hasMessage(null);
  assertThat(bpmnError.getErrorCode()).isEqualTo(ERROR_CODE);
}
 
Example #18
Source File: BpmnErrorTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreation_ErrorMessagePresent() {
  // when
  BpmnError bpmnError = new BpmnError(ERROR_CODE, ERROR_MESSAGE);
  
  // then
  assertThat(bpmnError).hasMessage(ERROR_MESSAGE);
  assertThat(bpmnError.getErrorCode()).isEqualTo(ERROR_CODE);
}
 
Example #19
Source File: BpmnErrorTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreation_ErrorCodeOnlyWithCause() {
  // when
  BpmnError bpmnError = new BpmnError(ERROR_CODE, CAUSE);
  
  // then
  assertThat(bpmnError)
          .hasMessage(null)
          .hasCause(CAUSE);
  assertThat(bpmnError.getErrorCode()).isEqualTo(ERROR_CODE);
}
 
Example #20
Source File: BpmnErrorTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreation_ErrorMessageAndCausePresent() {
  // when
  BpmnError bpmnError = new BpmnError(ERROR_CODE, ERROR_MESSAGE, CAUSE);
  
  // then
  assertThat(bpmnError)
          .hasMessageContaining(ERROR_MESSAGE)
          .hasCause(CAUSE);
  assertThat(bpmnError.getErrorCode()).isEqualTo(ERROR_CODE);
}
 
Example #21
Source File: ThrowBpmnErrorDelegate.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
protected void throwErrorIfRequested(DelegateExecution execution) {
  Boolean shouldThrowError = (Boolean) execution.getVariable(ERROR_INDICATOR_VARIABLE);

  if (Boolean.TRUE.equals(shouldThrowError)) {
    String errorName = (String) execution.getVariable(ERROR_NAME_VARIABLE);
    if (errorName == null) {
      errorName = DEFAULT_ERROR_NAME;
    }

    throw new BpmnError(errorName);
  }
}
 
Example #22
Source File: ThrowErrorDelegate.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
protected void handle(ActivityExecution execution, String action) throws Exception {
  execution.setVariable(action, true);
  String type = (String) execution.getVariable("type");
  if ("error".equalsIgnoreCase(type)) {
    throw new BpmnError("MyError");
  }
  else if ("exception".equalsIgnoreCase(type)) {
    throw new MyBusinessException("MyException");
  }
  else if ("leave".equalsIgnoreCase(type)) {
    execution.setVariable("type", null);
    leave(execution);
  }
}
 
Example #23
Source File: ProcessUtil.java    From Hands-On-RESTful-API-Design-Patterns-and-Best-Practices with MIT License 5 votes vote down vote up
public static void processResponse(DelegateExecution ctx, ServiceResponse serviceResponse) throws Exception {
	ctx.setVariable(ProcessConstants.VAR_RESPONSE, serviceResponse);
	if (!Response.Status.OK.toString().equals(serviceResponse.getStatusCode())) {
		ProcessContext pctx = (ProcessContext) ctx.getVariable(ProcessConstants.VAR_CTX);
		pctx.setError(serviceResponse.getErrorMessage());
		throw new BpmnError(SC_ERROR);
	}
}
 
Example #24
Source File: ConnectProcessEnginePluginTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Deployment(resources="org/camunda/connect/plugin/ConnectProcessEnginePluginTest.testConnectorWithThrownExceptionInScriptInputOutputMapping.bpmn")
public void testConnectorBpmnErrorThrownInScriptInputMappingIsHandledByBoundaryEvent() {
  Map<String, Object> variables = new HashMap<String, Object>();
  variables.put("throwInMapping", "in");
  variables.put("exception", new BpmnError("error"));
  runtimeService.startProcessInstanceByKey("testProcess", variables);
  //we will only reach the user task if the BPMNError from the script was handled by the boundary event
  Task task = taskService.createTaskQuery().singleResult();
  assertThat(task.getName(), is("User Task"));
}
 
Example #25
Source File: CamundaEventBusTest.java    From camunda-bpm-reactor with Apache License 2.0 5 votes vote down vote up
@Test
public void raises_bpmnError() throws Exception {
  eventBus.get().on(Selectors.matchAll(), new Consumer<Event<String>>() {
    @Override
    public void accept(Event<String> event) {
      throw new BpmnError("error");
    }
  });

  thrown.expect(BpmnError.class);

  eventBus.get().notify(Selectors.$("any"), Event.wrap("event"));

}
 
Example #26
Source File: FluentJavaDelegateMock.java    From camunda-bpm-mockito with Apache License 2.0 5 votes vote down vote up
@Override
public void onExecutionThrowBpmnError(final BpmnError bpmnError) {
  doAnswer(new JavaDelegate() {

    @Override
    public void execute(final DelegateExecution execution) throws Exception {
      throw bpmnError;
    }
  });
}
 
Example #27
Source File: FluentExecutionListenerMock.java    From camunda-bpm-mockito with Apache License 2.0 5 votes vote down vote up
@Override
public void onExecutionThrowBpmnError(final BpmnError bpmnError) {
  doAnswer(new ExecutionListener() {

    @Override
    public void notify(final DelegateExecution execution) throws Exception {
      throw bpmnError;
    }
  });
}
 
Example #28
Source File: FluentTaskListenerMock.java    From camunda-bpm-mockito with Apache License 2.0 5 votes vote down vote up
@Override
public void onExecutionThrowBpmnError(final BpmnError bpmnError) {
  doAnswer(new TaskListener() {

    @Override
    public void notify(final DelegateTask delegateTask) {
      throw bpmnError;
    }
  });
}
 
Example #29
Source File: ConnectProcessEnginePluginTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Deployment(resources="org/camunda/connect/plugin/ConnectProcessEnginePluginTest.testConnectorWithThrownExceptionInScriptInputOutputMapping.bpmn")
public void testConnectorBpmnErrorThrownInScriptOutputMappingIsHandledByBoundaryEvent() {
  Map<String, Object> variables = new HashMap<String, Object>();
  variables.put("throwInMapping", "out");
  variables.put("exception", new BpmnError("error"));
  runtimeService.startProcessInstanceByKey("testProcess", variables);
  //we will only reach the user task if the BPMNError from the script was handled by the boundary event
  Task task = taskService.createTaskQuery().singleResult();
  assertThat(task.getName(), is("User Task"));
}
 
Example #30
Source File: ConnectProcessEnginePluginTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Deployment(resources="org/camunda/connect/plugin/ConnectProcessEnginePluginTest.testConnectorWithThrownExceptionInScriptResourceInputOutputMapping.bpmn")
public void testConnectorBpmnErrorThrownInScriptResourceInputMappingIsHandledByBoundaryEvent() {
  Map<String, Object> variables = new HashMap<String, Object>();
  variables.put("throwInMapping", "in");
  variables.put("exception", new BpmnError("error"));
  runtimeService.startProcessInstanceByKey("testProcess", variables);
  //we will only reach the user task if the BPMNError from the script was handled by the boundary event
  Task task = taskService.createTaskQuery().singleResult();
  assertThat(task.getName(), is("User Task"));
}