2 * Copyright (c) 2016, Huawei Technologies Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.openo.baseservice.remoteservice.exception;
18 import java.text.MessageFormat;
21 * The base class for all common exception.<br/>
26 * @version SDNO 0.5 28-May-2016
28 public class ServiceException extends Exception {
31 * default exception id.
33 public static final String DEFAULT_ID = "framwork.remote.SystemError";
38 private static final long serialVersionUID = 5703294364555144738L;
43 private String id = DEFAULT_ID;
45 private Object[] args = null;
47 private int httpCode = 500;
49 private ExceptionArgs exceptionArgs = null;
52 * The default constructor<br/>
54 * This method is only used as deserialized, in other cases, use parameterized constructor.
59 public ServiceException() {
70 * @param cause: reason.
72 public ServiceException(final String id, final Throwable cause) {
83 * @param message: details.
85 public ServiceException(final String message) {
95 * @param id: exception id.
96 * @param message: details.
98 public ServiceException(final String id, final String message) {
109 * @param id: exception id.
110 * @param httpCode: http status code.
112 public ServiceException(final String id, final int httpCode) {
115 this.setHttpCode(httpCode);
124 * @param id: exception id.
125 * @param httpCode: http code.
126 * @param exceptionArgs: Exception handling frame parameters.
128 public ServiceException(final String id, final int httpCode, final ExceptionArgs exceptionArgs) {
131 this.setHttpCode(httpCode);
132 this.setExceptionArgs(exceptionArgs);
138 * Have a placeholder exception, use args formatted message.
142 * @param id: exception id.
143 * @param message: details.
144 * @param args: Placeholders for parameters
146 public ServiceException(final String id, final String message, final Object... args) {
147 super(MessageFormat.format(message, args));
155 * Have a placeholder exception, use args formatted message
159 * @param id: exception id.
160 * @param message: details.
161 * @param cause: reason.
162 * @param args: placeholder for parameters
164 public ServiceException(final String id, final String message, final Throwable cause, final Object... args) {
165 super(MessageFormat.format(message, args), cause);
176 * @param id: exception id.
177 * @param message: details.
178 * @param cause: reason.
180 public ServiceException(final String id, final String message, final Throwable cause) {
181 super(message, cause);
191 * @param cause: reason.
193 public ServiceException(final Throwable cause) {
198 * Get exceptoin id.<br/>
203 public String getId() {
204 if(id == null || id.isEmpty()) {
210 public void setId(final String id) {
214 public int getHttpCode() {
215 return this.httpCode;
218 public void setHttpCode(final int httpCode) {
219 this.httpCode = httpCode;
223 * Obtain the ROA exception handling framework parameters<br/>
225 * @return exception args.
228 public ExceptionArgs getExceptionArgs() {
229 return exceptionArgs;
232 public void setExceptionArgs(final ExceptionArgs exceptionArgs) {
233 this.exceptionArgs = exceptionArgs;
237 * Gets the parameter information<br/>
239 * @return parameter list.
242 protected Object[] getArgs() {
243 if(args == null || args.length == 0 || DEFAULT_ID.equals(getId())) {
244 return new Object[] {};
250 public String toString() {
251 return "exception.id: " + getId() + "; " + super.toString();