2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.client.exception;
23 import org.camunda.bpm.engine.delegate.BpmnError;
24 import org.camunda.bpm.engine.delegate.DelegateExecution;
25 import org.onap.so.bpmn.common.BuildingBlockExecution;
26 import org.onap.so.bpmn.common.DelegateExecutionImpl;
27 import org.onap.so.bpmn.core.WorkflowException;
28 import org.onap.so.logger.MessageEnum;
29 import org.onap.so.logger.MsoLogger;
30 import org.springframework.stereotype.Component;
33 public class ExceptionBuilder {
34 private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, ExceptionBuilder.class);
36 public void buildAndThrowWorkflowException(BuildingBlockExecution execution, int errorCode, Exception exception) {
37 String msg = "Exception in %s.%s ";
39 msoLogger.error(exception);
41 String errorVariable = "Error%s%s";
43 StackTraceElement[] trace = Thread.currentThread().getStackTrace();
44 for (StackTraceElement traceElement : trace) {
45 if (!traceElement.getClassName().equals(this.getClass().getName()) && !traceElement.getClassName().equals(Thread.class.getName())) {
46 msg = String.format(msg, traceElement.getClassName(), traceElement.getMethodName());
47 String shortClassName = traceElement.getClassName().substring(traceElement.getClassName().lastIndexOf(".") + 1);
48 errorVariable = String.format(errorVariable, shortClassName, traceElement.getMethodName());
53 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, msg, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, msg.toString());
54 execution.setVariable(errorVariable, exception.getMessage());
55 } catch (Exception ex){
56 //log trace, allow process to complete gracefully
60 if (exception.getMessage() != null)
61 msg = msg.concat(exception.getMessage());
62 buildAndThrowWorkflowException(execution, errorCode, msg);
65 public void buildAndThrowWorkflowException(BuildingBlockExecution execution, int errorCode, String errorMessage) {
66 if (execution instanceof DelegateExecutionImpl) {
67 buildAndThrowWorkflowException(((DelegateExecutionImpl) execution).getDelegateExecution(), errorCode, errorMessage);
71 public void buildAndThrowWorkflowException(DelegateExecution execution, int errorCode, String errorMessage) {
72 String processKey = getProcessKey(execution);
73 msoLogger.info("Building a WorkflowException for Subflow");
75 WorkflowException exception = new WorkflowException(processKey, errorCode, errorMessage);
76 execution.setVariable("WorkflowException", exception);
77 msoLogger.info("Outgoing WorkflowException is " + exception);
78 msoLogger.info("Throwing MSOWorkflowException");
79 throw new BpmnError("MSOWorkflowException");
82 public String getProcessKey(DelegateExecution execution) {
83 String testKey = (String) execution.getVariable("testProcessKey");
84 if (testKey != null) {
87 return execution.getProcessEngineServices().getRepositoryService()
88 .getProcessDefinition(execution.getProcessDefinitionId()).getKey();