View Javadoc

1   /*
2    * $Id: RPCError.java 799110 2009-07-29 22:44:26Z musachy $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.struts2.json.rpc;
22  
23  import java.io.PrintWriter;
24  import java.io.StringWriter;
25  
26  import com.opensymphony.xwork2.util.logging.Logger;
27  import com.opensymphony.xwork2.util.logging.LoggerFactory;
28  
29  /*
30   * Used to serialize RPC Errors
31   */
32  public class RPCError {
33      private static final Logger LOG = LoggerFactory.getLogger(RPCError.class);
34  
35      private int code;
36      private String name;
37      private String message;
38      private String stack;
39  
40      public RPCError() {
41      }
42  
43      public RPCError(String message, int code) {
44          this.code = code;
45          this.message = message;
46  
47          LOG.error(message);
48      }
49  
50      public RPCError(String message, RPCErrorCode code) {
51          this(message, code.code());
52      }
53  
54      public RPCError(Throwable t, int code, boolean debug) {
55          while (t.getCause() != null) {
56              t = t.getCause();
57          }
58  
59          this.code = code;
60          this.message = t.getMessage();
61          this.name = t.getClass().getName();
62  
63          if (debug) {
64              StringWriter s = new StringWriter();
65              PrintWriter w = new PrintWriter(s);
66              t.printStackTrace(w);
67              w.flush();
68              this.stack = s.toString();
69          }
70  
71          LOG.error(t.getMessage(), t);
72      }
73  
74      public RPCError(Throwable t, RPCErrorCode code, boolean debug) {
75          this(t, code.code(), debug);
76      }
77  
78      public int getCode() {
79          return code;
80      }
81  
82      public void setCode(int code) {
83          this.code = code;
84      }
85  
86      public String getName() {
87          return name;
88      }
89  
90      public void setName(String name) {
91          this.name = name;
92      }
93  
94      public String getMessage() {
95          return message;
96      }
97  
98      public void setMessage(String message) {
99          this.message = message;
100     }
101 
102     public String getStack() {
103         return stack;
104     }
105 
106     public void setStack(String stack) {
107         this.stack = stack;
108     }
109 }