class ExceptionBuilder {

    public void buildAndThrowWorkflowException(BuildingBlockExecution execution, int errorCode, Exception exception) {
        String msg = "Exception in %s.%s ";
        try {
            logger.error("Exception occurred", exception);

            String errorVariable = "Error%s%s";

            StackTraceElement[] trace = Thread.currentThread().getStackTrace();
            for (StackTraceElement traceElement : trace) {
                if (!traceElement.getClassName().equals(this.getClass().getName())
                        && !traceElement.getClassName().equals(Thread.class.getName())) {
                    msg = String.format(msg, traceElement.getClassName(), traceElement.getMethodName());
                    String shortClassName =
                            traceElement.getClassName().substring(traceElement.getClassName().lastIndexOf(".") + 1);
                    errorVariable = String.format(errorVariable, shortClassName, traceElement.getMethodName());
                    break;
                }
            }

            logger.error(LoggingAnchor.FIVE, MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(), msg, "BPMN",
                    ErrorCode.UnknownError.getValue(), msg);
            execution.setVariable(errorVariable, exception.getMessage());
        } catch (Exception ex) {
            // log trace, allow process to complete gracefully
            logger.error("Exception occurred", ex);
        }

        if (exception.getMessage() != null)
            msg = msg.concat(exception.getMessage());
        buildAndThrowWorkflowException(execution, errorCode, msg);
    }

}