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(DelegateExecution execution, int errorCode, Exception exception) {
66 String msg = "Exception in %s.%s ";
68 msoLogger.error(exception);
70 String errorVariable = "Error%s%s";
72 StackTraceElement[] trace = Thread.currentThread().getStackTrace();
73 for (StackTraceElement traceElement : trace) {
74 if (!traceElement.getClassName().equals(this.getClass().getName()) && !traceElement.getClassName().equals(Thread.class.getName())) {
75 msg = String.format(msg, traceElement.getClassName(), traceElement.getMethodName());
76 String shortClassName = traceElement.getClassName().substring(traceElement.getClassName().lastIndexOf(".") + 1);
77 errorVariable = String.format(errorVariable, shortClassName, traceElement.getMethodName());
81 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, msg, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, msg.toString());
82 execution.setVariable(errorVariable, exception.getMessage());
83 } catch (Exception ex){
84 //log trace, allow process to complete gracefully
88 if (exception.getMessage() != null)
89 msg = msg.concat(exception.getMessage());
90 buildAndThrowWorkflowException(execution, errorCode, msg);
93 public void buildAndThrowWorkflowException(BuildingBlockExecution execution, int errorCode, String errorMessage) {
94 if (execution instanceof DelegateExecutionImpl) {
95 buildAndThrowWorkflowException(((DelegateExecutionImpl) execution).getDelegateExecution(), errorCode, errorMessage);
99 public void buildAndThrowWorkflowException(DelegateExecution execution, int errorCode, String errorMessage) {
100 String processKey = getProcessKey(execution);
101 msoLogger.info("Building a WorkflowException for Subflow");
103 WorkflowException exception = new WorkflowException(processKey, errorCode, errorMessage);
104 execution.setVariable("WorkflowException", exception);
105 execution.setVariable("WorkflowExceptionErrorMessage", errorMessage);
106 msoLogger.info("Outgoing WorkflowException is " + exception);
107 msoLogger.info("Throwing MSOWorkflowException");
108 throw new BpmnError("MSOWorkflowException");
111 public void buildAndThrowWorkflowException(DelegateExecution execution, String errorCode, String errorMessage) {
112 execution.setVariable("WorkflowExceptionErrorMessage", errorMessage);
113 throw new BpmnError(errorCode,errorMessage);
116 public String getProcessKey(DelegateExecution execution) {
117 String testKey = (String) execution.getVariable("testProcessKey");
118 if (testKey != null) {
121 return execution.getProcessEngineServices().getRepositoryService()
122 .getProcessDefinition(execution.getProcessDefinitionId()).getKey();