2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (c) 2019 Samsung
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.so.client.exception;
25 import org.camunda.bpm.engine.delegate.BpmnError;
26 import org.camunda.bpm.engine.delegate.DelegateExecution;
27 import org.onap.so.bpmn.common.BuildingBlockExecution;
28 import org.onap.so.bpmn.common.DelegateExecutionImpl;
29 import org.onap.so.bpmn.core.WorkflowException;
30 import org.onap.so.logger.ErrorCode;
31 import org.onap.so.logger.MessageEnum;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.stereotype.Component;
37 public class ExceptionBuilder {
38 private static final Logger logger = LoggerFactory.getLogger(ExceptionBuilder.class);
40 public void buildAndThrowWorkflowException(BuildingBlockExecution execution, int errorCode, Exception exception) {
41 String msg = "Exception in %s.%s ";
43 logger.error("Exception occurred", exception);
45 String errorVariable = "Error%s%s";
47 StackTraceElement[] trace = Thread.currentThread().getStackTrace();
48 for (StackTraceElement traceElement : trace) {
49 if (!traceElement.getClassName().equals(this.getClass().getName()) && !traceElement.getClassName().equals(Thread.class.getName())) {
50 msg = String.format(msg, traceElement.getClassName(), traceElement.getMethodName());
51 String shortClassName = traceElement.getClassName().substring(traceElement.getClassName().lastIndexOf(".") + 1);
52 errorVariable = String.format(errorVariable, shortClassName, traceElement.getMethodName());
57 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(), msg, "BPMN",
58 ErrorCode.UnknownError.getValue(), msg.toString());
59 execution.setVariable(errorVariable, exception.getMessage());
60 } catch (Exception ex){
61 //log trace, allow process to complete gracefully
62 logger.error("Exception occurred", ex);
65 if (exception.getMessage() != null)
66 msg = msg.concat(exception.getMessage());
67 buildAndThrowWorkflowException(execution, errorCode, msg);
70 public void buildAndThrowWorkflowException(DelegateExecution execution, int errorCode, Exception exception) {
71 String msg = "Exception in %s.%s ";
73 logger.error("Exception occurred", exception);
75 String errorVariable = "Error%s%s";
77 StackTraceElement[] trace = Thread.currentThread().getStackTrace();
78 for (StackTraceElement traceElement : trace) {
79 if (!traceElement.getClassName().equals(this.getClass().getName()) && !traceElement.getClassName().equals(Thread.class.getName())) {
80 msg = String.format(msg, traceElement.getClassName(), traceElement.getMethodName());
81 String shortClassName = traceElement.getClassName().substring(traceElement.getClassName().lastIndexOf(".") + 1);
82 errorVariable = String.format(errorVariable, shortClassName, traceElement.getMethodName());
86 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(), msg, "BPMN",
87 ErrorCode.UnknownError.getValue(), msg.toString());
88 execution.setVariable(errorVariable, exception.getMessage());
89 } catch (Exception ex){
90 //log trace, allow process to complete gracefully
91 logger.error("Exception occurred", ex);
94 if (exception.getMessage() != null)
95 msg = msg.concat(exception.getMessage());
96 buildAndThrowWorkflowException(execution, errorCode, msg);
99 public void buildAndThrowWorkflowException(BuildingBlockExecution execution, int errorCode, String errorMessage) {
100 if (execution instanceof DelegateExecutionImpl) {
101 buildAndThrowWorkflowException(((DelegateExecutionImpl) execution).getDelegateExecution(), errorCode, errorMessage);
105 public void buildAndThrowWorkflowException(DelegateExecution execution, int errorCode, String errorMessage) {
106 String processKey = getProcessKey(execution);
107 logger.info("Building a WorkflowException for Subflow");
109 WorkflowException exception = new WorkflowException(processKey, errorCode, errorMessage);
110 execution.setVariable("WorkflowException", exception);
111 execution.setVariable("WorkflowExceptionErrorMessage", errorMessage);
112 logger.info("Outgoing WorkflowException is {}", exception);
113 logger.info("Throwing MSOWorkflowException");
114 throw new BpmnError("MSOWorkflowException");
117 public void buildAndThrowWorkflowException(DelegateExecution execution, String errorCode, String errorMessage) {
118 execution.setVariable("WorkflowExceptionErrorMessage", errorMessage);
119 throw new BpmnError(errorCode,errorMessage);
122 public String getProcessKey(DelegateExecution execution) {
123 String testKey = (String) execution.getVariable("testProcessKey");
124 if (testKey != null) {
127 return execution.getProcessEngineServices().getRepositoryService()
128 .getProcessDefinition(execution.getProcessDefinitionId()).getKey();