Replaced all tabs with spaces in java and pom.xml
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / onap / so / client / exception / ExceptionBuilder.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  */
22
23 package org.onap.so.client.exception;
24
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;
35
36 @Component
37 public class ExceptionBuilder {
38     private static final Logger logger = LoggerFactory.getLogger(ExceptionBuilder.class);
39
40     public void buildAndThrowWorkflowException(BuildingBlockExecution execution, int errorCode, Exception exception) {
41         String msg = "Exception in %s.%s ";
42         try {
43             logger.error("Exception occurred", exception);
44
45             String errorVariable = "Error%s%s";
46
47             StackTraceElement[] trace = Thread.currentThread().getStackTrace();
48             for (StackTraceElement traceElement : trace) {
49                 if (!traceElement.getClassName().equals(this.getClass().getName())
50                         && !traceElement.getClassName().equals(Thread.class.getName())) {
51                     msg = String.format(msg, traceElement.getClassName(), traceElement.getMethodName());
52                     String shortClassName =
53                             traceElement.getClassName().substring(traceElement.getClassName().lastIndexOf(".") + 1);
54                     errorVariable = String.format(errorVariable, shortClassName, traceElement.getMethodName());
55                     break;
56                 }
57             }
58
59             logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(), msg, "BPMN",
60                     ErrorCode.UnknownError.getValue(), msg.toString());
61             execution.setVariable(errorVariable, exception.getMessage());
62         } catch (Exception ex) {
63             // log trace, allow process to complete gracefully
64             logger.error("Exception occurred", ex);
65         }
66
67         if (exception.getMessage() != null)
68             msg = msg.concat(exception.getMessage());
69         buildAndThrowWorkflowException(execution, errorCode, msg);
70     }
71
72     public void buildAndThrowWorkflowException(DelegateExecution execution, int errorCode, Exception exception) {
73         String msg = "Exception in %s.%s ";
74         try {
75             logger.error("Exception occurred", exception);
76
77             String errorVariable = "Error%s%s";
78
79             StackTraceElement[] trace = Thread.currentThread().getStackTrace();
80             for (StackTraceElement traceElement : trace) {
81                 if (!traceElement.getClassName().equals(this.getClass().getName())
82                         && !traceElement.getClassName().equals(Thread.class.getName())) {
83                     msg = String.format(msg, traceElement.getClassName(), traceElement.getMethodName());
84                     String shortClassName =
85                             traceElement.getClassName().substring(traceElement.getClassName().lastIndexOf(".") + 1);
86                     errorVariable = String.format(errorVariable, shortClassName, traceElement.getMethodName());
87                     break;
88                 }
89             }
90             logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(), msg, "BPMN",
91                     ErrorCode.UnknownError.getValue(), msg.toString());
92             execution.setVariable(errorVariable, exception.getMessage());
93         } catch (Exception ex) {
94             // log trace, allow process to complete gracefully
95             logger.error("Exception occurred", ex);
96         }
97
98         if (exception.getMessage() != null)
99             msg = msg.concat(exception.getMessage());
100         buildAndThrowWorkflowException(execution, errorCode, msg);
101     }
102
103     public void buildAndThrowWorkflowException(BuildingBlockExecution execution, int errorCode, String errorMessage) {
104         if (execution instanceof DelegateExecutionImpl) {
105             buildAndThrowWorkflowException(((DelegateExecutionImpl) execution).getDelegateExecution(), errorCode,
106                     errorMessage);
107         }
108     }
109
110     public void buildAndThrowWorkflowException(DelegateExecution execution, int errorCode, String errorMessage) {
111         String processKey = getProcessKey(execution);
112         logger.info("Building a WorkflowException for Subflow");
113
114         WorkflowException exception = new WorkflowException(processKey, errorCode, errorMessage);
115         execution.setVariable("WorkflowException", exception);
116         execution.setVariable("WorkflowExceptionErrorMessage", errorMessage);
117         logger.info("Outgoing WorkflowException is {}", exception);
118         logger.info("Throwing MSOWorkflowException");
119         throw new BpmnError("MSOWorkflowException");
120     }
121
122     public void buildAndThrowWorkflowException(DelegateExecution execution, String errorCode, String errorMessage) {
123         execution.setVariable("WorkflowExceptionErrorMessage", errorMessage);
124         throw new BpmnError(errorCode, errorMessage);
125     }
126
127     public String getProcessKey(DelegateExecution execution) {
128         String testKey = (String) execution.getVariable("testProcessKey");
129         if (testKey != null) {
130             return testKey;
131         }
132         return execution.getProcessEngineServices().getRepositoryService()
133                 .getProcessDefinition(execution.getProcessDefinitionId()).getKey();
134     }
135 }