Debian Codegraph @

src/java/org/astrogrid/samp/ErrInfo.java

raw
1 package org.astrogrid.samp;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.IOException;
5 import java.io.PrintStream;
6 import java.util.Map;
7
8 /**
9 * Represents the error information associated with a SAMP response.
10 * This corresponds to the <code>samp.error</code> entry in a response map.
11 *
12 * @author Mark Taylor
13 * @since 14 Jul 2008
14 */
15 public class ErrInfo extends SampMap {
16
17 /** Key for short description of what went wrong. */
18 public static final String ERRORTXT_KEY = "samp.errortxt";
19
20 /** Key for free-form text given more information about the error. */
21 public static final String USERTXT_KEY = "samp.usertxt";
22
23 /** Key for debugging information such as a stack trace. */
24 public static final String DEBUGTXT_KEY = "samp.debugtxt";
25
26 /** Key for a numeric or textual code identifying the error. */
27 public static final String CODE_KEY = "samp.code";
28
29 private static final String[] KNOWN_KEYS = new String[] {
30 ERRORTXT_KEY,
31 USERTXT_KEY,
32 DEBUGTXT_KEY,
33 CODE_KEY,
34 };
35
36 /**
37 * Constructs an empty ErrInfo.
38 */
39 public ErrInfo() {
40 super( KNOWN_KEYS );
41 }
42
43 /**
44 * Constructs an ErrInfo based on a given Throwable.
45 *
46 * @param e error
47 */
48 public ErrInfo( Throwable e ) {
49 this();
50 String txt = e.getMessage();
51 if ( txt == null || txt.trim().length() == 0 ) {
52 txt = e.getClass().getName();
53 }
54 put( ERRORTXT_KEY, txt );
55 put( USERTXT_KEY, e.toString() );
56 put( DEBUGTXT_KEY, getStackTrace( e ) );
57 put( CODE_KEY, e.getClass().getName() );
58 }
59
60 /**
61 * Constructs an ErrInfo based on an existing map.
62 *
63 * @param map map containing initial data for this object
64 */
65 public ErrInfo( Map map ) {
66 this();
67 putAll( map );
68 }
69
70 /**
71 * Constructs an ErrInfo with a given {@link #ERRORTXT_KEY} value.
72 *
73 * @param errortxt short string describing what went wrong
74 */
75 public ErrInfo( String errortxt ) {
76 this();
77 put( ERRORTXT_KEY, errortxt );
78 }
79
80 /**
81 * Sets the value for the {@link #ERRORTXT_KEY} key.
82 *
83 * @param errortxt short string describing what went wrong
84 */
85 public void setErrortxt( String errortxt ) {
86 put( ERRORTXT_KEY, errortxt );
87 }
88
89 /**
90 * Returns the value for the {@link #ERRORTXT_KEY} key.
91 *
92 * @return short string describing what went wrong
93 */
94 public String getErrortxt() {
95 return getString( ERRORTXT_KEY );
96 }
97
98 /**
99 * Sets the value for the {@link #USERTXT_KEY} key.
100 *
101 * @param usertxt free-form string giving more detail on the error
102 */
103 public void setUsertxt( String usertxt ) {
104 put( USERTXT_KEY, usertxt );
105 }
106
107 /**
108 * Returns the value for the {@link #USERTXT_KEY} key.
109 *
110 * @return free-form string giving more detail on the error
111 */
112 public String getUsertxt() {
113 return getString( USERTXT_KEY );
114 }
115
116 /**
117 * Sets the value for the {@link #DEBUGTXT_KEY} key.
118 *
119 * @param debugtxt string containing debugging information, such as a
120 * a stack trace
121 */
122 public void setDebugtxt( String debugtxt ) {
123 put( DEBUGTXT_KEY, debugtxt );
124 }
125
126 /**
127 * Returns the value for the {@link #DEBUGTXT_KEY} key.
128 *
129 * @return string containing debugging information, such as a stack trace
130 */
131 public String getDebugtxt() {
132 return getString( DEBUGTXT_KEY );
133 }
134
135 /**
136 * Sets the value for the {@link #CODE_KEY} key.
137 *
138 * @param code numeric or textual code identifying the error
139 */
140 public void setCode( String code ) {
141 put( CODE_KEY, code );
142 }
143
144 /**
145 * Returns the value for the {@link #CODE_KEY} key.
146 *
147 * @return numeric or textual code identifying the error
148 */
149 public String getCode() {
150 return getString( CODE_KEY );
151 }
152
153 public void check() {
154 super.check();
155 checkHasKeys( new String[] { ERRORTXT_KEY, } );
156 }
157
158 /**
159 * Returns a given map as an ErrInfo object.
160 *
161 * @param map map
162 * @return errInfo
163 */
164 public static ErrInfo asErrInfo( Map map ) {
165 return ( map instanceof ErrInfo || map == null )
166 ? (ErrInfo) map
167 : new ErrInfo( map );
168 }
169
170 /**
171 * Generates a string containing a stack trace for a given throwable.
172 *
173 * @param e error
174 * @return stacktrace
175 */
176 private static String getStackTrace( Throwable e ) {
177 byte[] bbuf;
178 try {
179 ByteArrayOutputStream bOut = new ByteArrayOutputStream();
180 e.printStackTrace( new PrintStream( bOut ) );
181 bOut.close();
182 bbuf = bOut.toByteArray();
183 }
184 catch ( IOException ioex ) {
185 assert false;
186 return "error generating stacktrace";
187 }
188 StringBuffer sbuf = new StringBuffer( bbuf.length );
189 for ( int ic = 0; ic < bbuf.length; ic++ ) {
190 char c = (char) bbuf[ ic ];
191 if ( SampUtils.isStringChar( c ) ) {
192 sbuf.append( c );
193 }
194 }
195 return sbuf.toString();
196 }
197 }