Removed MsoLogger class
[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()) && !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());
53                                         break;
54                                 }
55                         }
56
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);
63                 }
64
65                 if (exception.getMessage() != null)
66                         msg = msg.concat(exception.getMessage());
67                 buildAndThrowWorkflowException(execution, errorCode, msg);
68         }
69         
70         public void buildAndThrowWorkflowException(DelegateExecution execution, int errorCode, Exception exception) {
71                 String msg = "Exception in %s.%s ";
72                 try{
73                         logger.error("Exception occurred", exception);
74
75                         String errorVariable = "Error%s%s";
76
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());
83                                         break;
84                                 }
85                         }
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);
92                 }
93
94                 if (exception.getMessage() != null)
95                         msg = msg.concat(exception.getMessage());
96                 buildAndThrowWorkflowException(execution, errorCode, msg);
97         }
98
99         public void buildAndThrowWorkflowException(BuildingBlockExecution execution, int errorCode, String errorMessage) {
100                 if (execution instanceof DelegateExecutionImpl) {
101                         buildAndThrowWorkflowException(((DelegateExecutionImpl) execution).getDelegateExecution(), errorCode, errorMessage);
102                 }
103         }
104
105         public void buildAndThrowWorkflowException(DelegateExecution execution, int errorCode, String errorMessage) {
106                 String processKey = getProcessKey(execution);
107                 logger.info("Building a WorkflowException for Subflow");
108
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");
115         }
116         
117         public void buildAndThrowWorkflowException(DelegateExecution execution, String errorCode, String errorMessage) {
118                 execution.setVariable("WorkflowExceptionErrorMessage", errorMessage);
119                 throw new BpmnError(errorCode,errorMessage);
120         }
121
122         public String getProcessKey(DelegateExecution execution) {
123                 String testKey = (String) execution.getVariable("testProcessKey");
124                 if (testKey != null) {
125                         return testKey;
126                 }
127                 return execution.getProcessEngineServices().getRepositoryService()
128                                 .getProcessDefinition(execution.getProcessDefinitionId()).getKey();
129         }
130 }