2 * Copyright 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 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);
121 * the exception include the httpcode and message.
125 * @param httpCode http code.
126 * @param message details.
128 public ServiceException(final int httpCode, final String message) {
130 this.setHttpCode(httpCode);
139 * @param id: exception id.
140 * @param httpCode: http code.
141 * @param exceptionArgs: Exception handling frame parameters.
143 public ServiceException(final String id, final int httpCode, final ExceptionArgs exceptionArgs) {
146 this.setHttpCode(httpCode);
147 this.setExceptionArgs(exceptionArgs);
153 * Have a placeholder exception, use args formatted message.
157 * @param id: exception id.
158 * @param message: details.
159 * @param args: Placeholders for parameters
161 public ServiceException(final String id, final String message, final Object... args) {
162 super(MessageFormat.format(message, args));
170 * Have a placeholder exception, use args formatted message
174 * @param id: exception id.
175 * @param message: details.
176 * @param cause: reason.
177 * @param args: placeholder for parameters
179 public ServiceException(final String id, final String message, final Throwable cause, final Object... args) {
180 super(MessageFormat.format(message, args), cause);
191 * @param id: exception id.
192 * @param message: details.
193 * @param cause: reason.
195 public ServiceException(final String id, final String message, final Throwable cause) {
196 super(message, cause);
206 * @param cause: reason.
208 public ServiceException(final Throwable cause) {
213 * Get exceptoin id.<br/>
218 public String getId() {
219 if(id == null || id.isEmpty()) {
225 public void setId(final String id) {
229 public int getHttpCode() {
230 return this.httpCode;
233 public void setHttpCode(final int httpCode) {
234 this.httpCode = httpCode;
238 * Obtain the ROA exception handling framework parameters<br/>
240 * @return exception args.
243 public ExceptionArgs getExceptionArgs() {
244 return exceptionArgs;
247 public void setExceptionArgs(final ExceptionArgs exceptionArgs) {
248 this.exceptionArgs = exceptionArgs;
252 * Gets the parameter information<br/>
254 * @return parameter list.
257 protected Object[] getArgs() {
258 if(args == null || args.length == 0 || DEFAULT_ID.equals(getId())) {
259 return new Object[] {};
265 public String toString() {
266 return "exception.id: " + getId() + "; " + super.toString();