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.openecomp.sdc.exception;
23 import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
24 import org.openecomp.sdc.common.log.wrappers.Logger;
26 import java.util.Arrays;
27 import java.util.Formatter;
28 import java.util.regex.Matcher;
29 import java.util.regex.Pattern;
31 public abstract class AbstractSdncException {
33 private String messageId;
37 private String[] variables;
39 private static Logger log = Logger.getLogger(AbstractSdncException.class.getName());
41 private static final Pattern ERROR_PARAM_PATTERN = Pattern.compile("%\\d");
43 public AbstractSdncException() {
46 public AbstractSdncException(String messageId, String text, String[] variables) {
48 this.messageId = messageId;
50 this.variables = validateParameters(messageId, text, variables);
53 private String[] validateParameters(String messageId, String text, String[] variables) {
55 Matcher m = ERROR_PARAM_PATTERN.matcher(text);
56 int expectedParamsNum = 0;
58 expectedParamsNum += 1;
60 int actualParamsNum = (variables != null) ? variables.length : 0;
61 if (actualParamsNum < expectedParamsNum) {
62 log.warn(EcompLoggerErrorCode.UNKNOWN_ERROR, "", "",
63 "Received less parameters than expected for error with messageId {}, expected: {}, actual: {}. Missing parameters are padded with null values.",
64 messageId, expectedParamsNum, actualParamsNum);
65 } else if (actualParamsNum > expectedParamsNum) {
66 log.warn(EcompLoggerErrorCode.UNKNOWN_ERROR, "", "", "",
67 "Received more parameters than expected for error with messageId {}, expected: {}, actual: {}. Extra parameters are ignored.",
68 messageId, expectedParamsNum, actualParamsNum);
70 if (variables != null) {
71 res = Arrays.copyOf(variables, expectedParamsNum);
77 public String getMessageId() {
78 return this.messageId;
81 public String getText() {
85 public String[] getVariables() {
89 public void setMessageId(String messageId) {
90 this.messageId = messageId;
93 public void setText(String text) {
97 public void setVariables(String[] variables) {
98 this.variables = variables;
101 public String getFormattedErrorMessage() {
103 if (variables != null && variables.length > 0) {
104 Formatter formatter = new Formatter();
106 res = formatter.format(this.text.replaceAll("%\\d", "%s"), (Object[]) this.variables).toString();