1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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 }