source: josm/trunk/src/org/openstreetmap/josm/gui/ExceptionDialogUtil.java@ 2646

Last change on this file since 2646 was 2598, checked in by Gubaer, 14 years ago

comment to follow in a later commit
Have to break up a commit because of SSL problem in SVN (filed a ticket - #4093)

File size: 12.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.io.IOException;
8import java.lang.reflect.InvocationTargetException;
9import java.net.HttpURLConnection;
10import java.net.SocketException;
11import java.net.UnknownHostException;
12
13import javax.swing.JOptionPane;
14
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.io.ChangesetClosedException;
17import org.openstreetmap.josm.io.OsmApiException;
18import org.openstreetmap.josm.io.OsmApiInitializationException;
19import org.openstreetmap.josm.io.OsmTransferException;
20import org.openstreetmap.josm.tools.BugReportExceptionHandler;
21import org.openstreetmap.josm.tools.ExceptionUtil;
22
23/**
24 * This utility class provides static methods which explain various exceptions to the user.
25 *
26 */
27public class ExceptionDialogUtil {
28
29 /**
30 * just static utility functions. no constructor
31 */
32 private ExceptionDialogUtil() {
33 }
34
35 /**
36 * handles an exception caught during OSM API initialization
37 *
38 * @param e the exception
39 */
40 public static void explainOsmApiInitializationException(OsmApiInitializationException e) {
41 HelpAwareOptionPane.showOptionDialog(
42 Main.parent,
43 ExceptionUtil.explainOsmApiInitializationException(e),
44 tr("Error"),
45 JOptionPane.ERROR_MESSAGE,
46 ht("/ErrorMessages#OsmApiInitializationException")
47 );
48 }
49
50 /**
51 * handles a ChangesetClosedException
52 *
53 * @param e the exception
54 */
55 public static void explainChangesetClosedException(ChangesetClosedException e) {
56 HelpAwareOptionPane.showOptionDialog(
57 Main.parent,
58 ExceptionUtil.explainChangesetClosedException(e),
59 tr("Error"),
60 JOptionPane.ERROR_MESSAGE,
61 ht("/Action/Upload#ChangesetClosed")
62 );
63 }
64
65 /**
66 * Explains an upload error due to a violated precondition, i.e. a HTTP return code 412
67 *
68 * @param e the exception
69 */
70 public static void explainPreconditionFailed(OsmApiException e) {
71 HelpAwareOptionPane.showOptionDialog(
72 Main.parent,
73 ExceptionUtil.explainPreconditionFailed(e),
74 tr("Precondition violation"),
75 JOptionPane.ERROR_MESSAGE,
76 ht("/ErrorMessages#OsmApiException")
77 );
78 }
79
80 /**
81 * Explains an exception with a generic message dialog
82 *
83 * @param e the exception
84 */
85 public static void explainGeneric(Exception e) {
86 e.printStackTrace();
87 HelpAwareOptionPane.showOptionDialog(
88 Main.parent,
89 ExceptionUtil.explainGeneric(e),
90 tr("Error"),
91 JOptionPane.ERROR_MESSAGE,
92 ht("/ErrorMessages#GenericException")
93 );
94 }
95
96 /**
97 * Explains a {@see SecurityException} which has caused an {@see OsmTransferException}.
98 * This is most likely happening when user tries to access the OSM API from within an
99 * applet which wasn't loaded from the API server.
100 *
101 * @param e the exception
102 */
103
104 public static void explainSecurityException(OsmTransferException e) {
105 HelpAwareOptionPane.showOptionDialog(
106 Main.parent,
107 ExceptionUtil.explainSecurityException(e),
108 tr("Security exception"),
109 JOptionPane.ERROR_MESSAGE,
110 ht("/ErrorMessages#SecurityException")
111 );
112 }
113
114 /**
115 * Explains a {@see SocketException} which has caused an {@see OsmTransferException}.
116 * This is most likely because there's not connection to the Internet or because
117 * the remote server is not reachable.
118 *
119 * @param e the exception
120 */
121
122 public static void explainNestedSocketException(OsmTransferException e) {
123 HelpAwareOptionPane.showOptionDialog(
124 Main.parent,
125 ExceptionUtil.explainNestedSocketException(e),
126 tr("Network exception"),
127 JOptionPane.ERROR_MESSAGE,
128 ht("/ErrorMessages#NestedSocketException")
129 );
130 }
131
132 /**
133 * Explains a {@see IOException} which has caused an {@see OsmTransferException}.
134 * This is most likely happening when the communication with the remote server is
135 * interrupted for any reason.
136 *
137 * @param e the exception
138 */
139
140 public static void explainNestedIOException(OsmTransferException e) {
141 HelpAwareOptionPane.showOptionDialog(
142 Main.parent,
143 ExceptionUtil.explainNestedIOException(e),
144 tr("IO Exception"),
145 JOptionPane.ERROR_MESSAGE,
146 ht("/ErrorMessages#NestedIOException")
147 );
148 }
149
150 /**
151 * Explains a {@see InvocationTargetException }
152 *
153 * @param e the exception
154 */
155
156 public static void explainNestedInvocationTargetException(Exception e) {
157 InvocationTargetException ex = getNestedException(e, InvocationTargetException.class);
158 if (ex != null) {
159 // Users should be able to submit a bug report for an invocation target exception
160 //
161 BugReportExceptionHandler.handleException(ex);
162 return;
163 }
164 }
165
166 /**
167 * Explains a {@see OsmApiException} which was thrown because of an internal server
168 * error in the OSM API server.
169 *
170 * @param e the exception
171 */
172
173 public static void explainInternalServerError(OsmTransferException e) {
174 HelpAwareOptionPane.showOptionDialog(
175 Main.parent,
176 ExceptionUtil.explainInternalServerError(e),
177 tr("Internal Server Error"),
178 JOptionPane.ERROR_MESSAGE,
179 ht("/ErrorMessages#InternalServerError")
180 );
181 }
182
183 /**
184 * Explains a {@see OsmApiException} which was thrown because of a bad
185 * request
186 *
187 * @param e the exception
188 */
189 public static void explainBadRequest(OsmApiException e) {
190 HelpAwareOptionPane.showOptionDialog(
191 Main.parent,
192 ExceptionUtil.explainBadRequest(e),
193 tr("Bad Request"),
194 JOptionPane.ERROR_MESSAGE,
195 ht("/ErrorMessages#BadRequest")
196 );
197 }
198
199 /**
200 * Explains a {@see OsmApiException} which was thrown because a resource wasn't found
201 * on the server
202 *
203 * @param e the exception
204 */
205 public static void explainNotFound(OsmApiException e) {
206 HelpAwareOptionPane.showOptionDialog(
207 Main.parent,
208 ExceptionUtil.explainNotFound(e),
209 tr("Not Found"),
210 JOptionPane.ERROR_MESSAGE,
211 ht("/ErrorMessages#NotFound")
212 );
213 }
214
215 /**
216 * Explains a {@see OsmApiException} which was thrown because of a conflict
217 *
218 * @param e the exception
219 */
220 public static void explainConflict(OsmApiException e) {
221 HelpAwareOptionPane.showOptionDialog(
222 Main.parent,
223 ExceptionUtil.explainConflict(e),
224 tr("Conflict"),
225 JOptionPane.ERROR_MESSAGE,
226 ht("/ErrorMessages#Conflict")
227 );
228 }
229
230 /**
231 * Explains a {@see UnknownHostException} which has caused an {@see OsmTransferException}.
232 * This is most likely happening when there is an error in the API URL or when
233 * local DNS services are not working.
234 *
235 * @param e the exception
236 */
237
238 public static void explainNestedUnkonwnHostException(OsmTransferException e) {
239 HelpAwareOptionPane.showOptionDialog(
240 Main.parent,
241 ExceptionUtil.explainNestedUnkonwnHostException(e),
242 tr("Unknown host"),
243 JOptionPane.ERROR_MESSAGE,
244 ht("/ErrorMessages#UnknownHost")
245 );
246 }
247
248 /**
249 * Replies the first nested exception of type <code>nestedClass</code> (including
250 * the root exception <code>e</code>) or null, if no such exception is found.
251 *
252 * @param <T>
253 * @param e the root exception
254 * @param nestedClass the type of the nested exception
255 * @return the first nested exception of type <code>nestedClass</code> (including
256 * the root exception <code>e</code>) or null, if no such exception is found.
257 */
258 protected static <T> T getNestedException(Exception e, Class<T> nestedClass) {
259 Throwable t = e;
260 while (t != null && !(nestedClass.isInstance(t))) {
261 t = t.getCause();
262 }
263 if (t == null)
264 return null;
265 else if (nestedClass.isInstance(t))
266 return nestedClass.cast(t);
267 return null;
268 }
269
270 /**
271 * Explains an {@see OsmTransferException} to the user.
272 *
273 * @param e the {@see OsmTransferException}
274 */
275 public static void explainOsmTransferException(OsmTransferException e) {
276 if (getNestedException(e, SecurityException.class) != null) {
277 explainSecurityException(e);
278 return;
279 }
280 if (getNestedException(e, SocketException.class) != null) {
281 explainNestedSocketException(e);
282 return;
283 }
284 if (getNestedException(e, UnknownHostException.class) != null) {
285 explainNestedUnkonwnHostException(e);
286 return;
287 }
288 if (getNestedException(e, IOException.class) != null) {
289 explainNestedIOException(e);
290 return;
291 }
292 if (e instanceof OsmApiInitializationException) {
293 explainOsmApiInitializationException((OsmApiInitializationException) e);
294 return;
295 }
296
297 if (e instanceof ChangesetClosedException) {
298 explainChangesetClosedException((ChangesetClosedException)e);
299 return;
300 }
301
302 if (e instanceof OsmApiException) {
303 OsmApiException oae = (OsmApiException) e;
304 if (oae.getResponseCode() == HttpURLConnection.HTTP_PRECON_FAILED) {
305 explainPreconditionFailed(oae);
306 return;
307 }
308 if (oae.getResponseCode() == HttpURLConnection.HTTP_GONE) {
309 explainGoneForUnknownPrimitive(oae);
310 return;
311 }
312 if (oae.getResponseCode() == HttpURLConnection.HTTP_INTERNAL_ERROR) {
313 explainInternalServerError(oae);
314 return;
315 }
316 if (oae.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST) {
317 explainBadRequest(oae);
318 return;
319 }
320 if (oae.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
321 explainNotFound(oae);
322 return;
323 }
324 if (oae.getResponseCode() == HttpURLConnection.HTTP_CONFLICT) {
325 explainConflict(oae);
326 return;
327 }
328
329 }
330 explainGeneric(e);
331 }
332
333 /**
334 * explains the case of an error due to a delete request on an already deleted
335 * {@see OsmPrimitive}, i.e. a HTTP response code 410, where we don't know which
336 * {@see OsmPrimitive} is causing the error.
337 *
338 * @param e the exception
339 */
340 public static void explainGoneForUnknownPrimitive(OsmApiException e) {
341 HelpAwareOptionPane.showOptionDialog(
342 Main.parent,
343 ExceptionUtil.explainGoneForUnknownPrimitive(e),
344 tr("Object deleted"),
345 JOptionPane.ERROR_MESSAGE,
346 ht("/ErrorMessages#GoneForUnknownPrimitive")
347 );
348 }
349
350 /**
351 * Explains an {@see Exception} to the user.
352 *
353 * @param e the {@see Exception}
354 */
355 public static void explainException(Exception e) {
356 if (getNestedException(e, InvocationTargetException.class) != null) {
357 explainNestedInvocationTargetException(e);
358 return;
359 }
360 if (e instanceof OsmTransferException) {
361 explainOsmTransferException((OsmTransferException) e);
362 return;
363 }
364 explainGeneric(e);
365 }
366}
Note: See TracBrowser for help on using the repository browser.