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

Last change on this file since 3966 was 3255, checked in by jttt, 14 years ago

Fix #4764 Fehler

  • Property svn:eol-style set to native
File size: 16.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.IllegalDataException;
18import org.openstreetmap.josm.io.MissingOAuthAccessTokenException;
19import org.openstreetmap.josm.io.OsmApiException;
20import org.openstreetmap.josm.io.OsmApiInitializationException;
21import org.openstreetmap.josm.io.OsmTransferException;
22import org.openstreetmap.josm.tools.BugReportExceptionHandler;
23import org.openstreetmap.josm.tools.ExceptionUtil;
24
25/**
26 * This utility class provides static methods which explain various exceptions to the user.
27 *
28 */
29public class ExceptionDialogUtil {
30
31 /**
32 * just static utility functions. no constructor
33 */
34 private ExceptionDialogUtil() {
35 }
36
37 /**
38 * handles an exception caught during OSM API initialization
39 *
40 * @param e the exception
41 */
42 public static void explainOsmApiInitializationException(OsmApiInitializationException e) {
43 HelpAwareOptionPane.showOptionDialog(
44 Main.parent,
45 ExceptionUtil.explainOsmApiInitializationException(e),
46 tr("Error"),
47 JOptionPane.ERROR_MESSAGE,
48 ht("/ErrorMessages#OsmApiInitializationException")
49 );
50 }
51
52 /**
53 * handles a ChangesetClosedException
54 *
55 * @param e the exception
56 */
57 public static void explainChangesetClosedException(ChangesetClosedException e) {
58 HelpAwareOptionPane.showOptionDialog(
59 Main.parent,
60 ExceptionUtil.explainChangesetClosedException(e),
61 tr("Error"),
62 JOptionPane.ERROR_MESSAGE,
63 ht("/Action/Upload#ChangesetClosed")
64 );
65 }
66
67 /**
68 * Explains an upload error due to a violated precondition, i.e. a HTTP return code 412
69 *
70 * @param e the exception
71 */
72 public static void explainPreconditionFailed(OsmApiException e) {
73 HelpAwareOptionPane.showOptionDialog(
74 Main.parent,
75 ExceptionUtil.explainPreconditionFailed(e),
76 tr("Precondition violation"),
77 JOptionPane.ERROR_MESSAGE,
78 ht("/ErrorMessages#OsmApiException")
79 );
80 }
81
82 /**
83 * Explains an exception with a generic message dialog
84 *
85 * @param e the exception
86 */
87 public static void explainGeneric(Exception e) {
88 e.printStackTrace();
89 BugReportExceptionHandler.handleException(e);
90 }
91
92 /**
93 * Explains a {@see SecurityException} which has caused an {@see OsmTransferException}.
94 * This is most likely happening when user tries to access the OSM API from within an
95 * applet which wasn't loaded from the API server.
96 *
97 * @param e the exception
98 */
99
100 public static void explainSecurityException(OsmTransferException e) {
101 HelpAwareOptionPane.showOptionDialog(
102 Main.parent,
103 ExceptionUtil.explainSecurityException(e),
104 tr("Security exception"),
105 JOptionPane.ERROR_MESSAGE,
106 ht("/ErrorMessages#SecurityException")
107 );
108 }
109
110 /**
111 * Explains a {@see SocketException} which has caused an {@see OsmTransferException}.
112 * This is most likely because there's not connection to the Internet or because
113 * the remote server is not reachable.
114 *
115 * @param e the exception
116 */
117
118 public static void explainNestedSocketException(OsmTransferException e) {
119 HelpAwareOptionPane.showOptionDialog(
120 Main.parent,
121 ExceptionUtil.explainNestedSocketException(e),
122 tr("Network exception"),
123 JOptionPane.ERROR_MESSAGE,
124 ht("/ErrorMessages#NestedSocketException")
125 );
126 }
127
128 /**
129 * Explains a {@see IOException} which has caused an {@see OsmTransferException}.
130 * This is most likely happening when the communication with the remote server is
131 * interrupted for any reason.
132 *
133 * @param e the exception
134 */
135
136 public static void explainNestedIOException(OsmTransferException e) {
137 HelpAwareOptionPane.showOptionDialog(
138 Main.parent,
139 ExceptionUtil.explainNestedIOException(e),
140 tr("IO Exception"),
141 JOptionPane.ERROR_MESSAGE,
142 ht("/ErrorMessages#NestedIOException")
143 );
144 }
145
146 /**
147 * Explains a {@see IllegalDataException} which has caused an {@see OsmTransferException}.
148 * This is most likely happening when JOSM tries to load data in in an unsupported format.
149 *
150 * @param e the exception
151 */
152
153 public static void explainNestedIllegalDataException(OsmTransferException e) {
154 HelpAwareOptionPane.showOptionDialog(
155 Main.parent,
156 ExceptionUtil.explainNestedIllegalDataException(e),
157 tr("Illegal Data"),
158 JOptionPane.ERROR_MESSAGE,
159 ht("/ErrorMessages#IllegalDataException")
160 );
161 }
162
163 /**
164 * Explains a {@see InvocationTargetException }
165 *
166 * @param e the exception
167 */
168
169 public static void explainNestedInvocationTargetException(Exception e) {
170 InvocationTargetException ex = getNestedException(e, InvocationTargetException.class);
171 if (ex != null) {
172 // Users should be able to submit a bug report for an invocation target exception
173 //
174 BugReportExceptionHandler.handleException(ex);
175 return;
176 }
177 }
178
179 /**
180 * Explains a {@see OsmApiException} which was thrown because of an internal server
181 * error in the OSM API server.
182 *
183 * @param e the exception
184 */
185
186 public static void explainInternalServerError(OsmTransferException e) {
187 HelpAwareOptionPane.showOptionDialog(
188 Main.parent,
189 ExceptionUtil.explainInternalServerError(e),
190 tr("Internal Server Error"),
191 JOptionPane.ERROR_MESSAGE,
192 ht("/ErrorMessages#InternalServerError")
193 );
194 }
195
196 /**
197 * Explains a {@see OsmApiException} which was thrown because of a bad
198 * request
199 *
200 * @param e the exception
201 */
202 public static void explainBadRequest(OsmApiException e) {
203 HelpAwareOptionPane.showOptionDialog(
204 Main.parent,
205 ExceptionUtil.explainBadRequest(e),
206 tr("Bad Request"),
207 JOptionPane.ERROR_MESSAGE,
208 ht("/ErrorMessages#BadRequest")
209 );
210 }
211
212 /**
213 * Explains a {@see OsmApiException} which was thrown because a resource wasn't found
214 * on the server
215 *
216 * @param e the exception
217 */
218 public static void explainNotFound(OsmApiException e) {
219 HelpAwareOptionPane.showOptionDialog(
220 Main.parent,
221 ExceptionUtil.explainNotFound(e),
222 tr("Not Found"),
223 JOptionPane.ERROR_MESSAGE,
224 ht("/ErrorMessages#NotFound")
225 );
226 }
227
228 /**
229 * Explains a {@see OsmApiException} which was thrown because of a conflict
230 *
231 * @param e the exception
232 */
233 public static void explainConflict(OsmApiException e) {
234 HelpAwareOptionPane.showOptionDialog(
235 Main.parent,
236 ExceptionUtil.explainConflict(e),
237 tr("Conflict"),
238 JOptionPane.ERROR_MESSAGE,
239 ht("/ErrorMessages#Conflict")
240 );
241 }
242
243 /**
244 * Explains a {@see OsmApiException} which was thrown because the authentication at
245 * the OSM server failed
246 *
247 * @param e the exception
248 */
249 public static void explainAuthenticationFailed(OsmApiException e) {
250 String authMethod = Main.pref.get("osm-server.auth-method", "basic");
251 String msg;
252 if (authMethod.equals("oauth")) {
253 msg = ExceptionUtil.explainFailedOAuthAuthentication(e);
254 } else {
255 msg = ExceptionUtil.explainFailedBasicAuthentication(e);
256 }
257
258 HelpAwareOptionPane.showOptionDialog(
259 Main.parent,
260 msg,
261 tr("Authentication Failed"),
262 JOptionPane.ERROR_MESSAGE,
263 ht("/ErrorMessages#AuthenticationFailed")
264 );
265 }
266
267 /**
268 * Explains a {@see OsmApiException} which was thrown because accessing a protected
269 * resource was forbidden.
270 *
271 * @param e the exception
272 */
273 public static void explainAuthorizationFailed(OsmApiException e) {
274 HelpAwareOptionPane.showOptionDialog(
275 Main.parent,
276 ExceptionUtil.explainFailedOAuthAuthorisation(e),
277 tr("Authorisation Failed"),
278 JOptionPane.ERROR_MESSAGE,
279 ht("/ErrorMessages#AuthenticationFailed")
280 );
281 }
282
283 /**
284 * Explains a {@see OsmApiException} which was thrown because of a
285 * client timeout (HTTP 408)
286 *
287 * @param e the exception
288 */
289 public static void explainClientTimeout(OsmApiException e) {
290 HelpAwareOptionPane.showOptionDialog(
291 Main.parent,
292 ExceptionUtil.explainClientTimeout(e),
293 tr("Client Time Out"),
294 JOptionPane.ERROR_MESSAGE,
295 ht("/ErrorMessages#ClientTimeOut")
296 );
297 }
298
299 /**
300 * Explains a {@see OsmApiException} with a generic error
301 * message.
302 *
303 * @param e the exception
304 */
305 public static void explainGenericHttpException(OsmApiException e) {
306 HelpAwareOptionPane.showOptionDialog(
307 Main.parent,
308 ExceptionUtil.explainClientTimeout(e),
309 tr("Communication with OSM server failed"),
310 JOptionPane.ERROR_MESSAGE,
311 ht("/ErrorMessages#GenericCommunicationError")
312 );
313 }
314
315 /**
316 * Explains a {@see OsmApiException} which was thrown because accessing a protected
317 * resource was forbidden.
318 *
319 * @param e the exception
320 */
321 public static void explainMissingOAuthAccessTokenException(MissingOAuthAccessTokenException e) {
322 HelpAwareOptionPane.showOptionDialog(
323 Main.parent,
324 ExceptionUtil.explainMissingOAuthAccessTokenException(e),
325 tr("Authentication failed"),
326 JOptionPane.ERROR_MESSAGE,
327 ht("/ErrorMessages#MissingOAuthAccessToken")
328 );
329 }
330
331 /**
332 * Explains a {@see UnknownHostException} which has caused an {@see OsmTransferException}.
333 * This is most likely happening when there is an error in the API URL or when
334 * local DNS services are not working.
335 *
336 * @param e the exception
337 */
338
339 public static void explainNestedUnkonwnHostException(OsmTransferException e) {
340 HelpAwareOptionPane.showOptionDialog(
341 Main.parent,
342 ExceptionUtil.explainNestedUnknownHostException(e),
343 tr("Unknown host"),
344 JOptionPane.ERROR_MESSAGE,
345 ht("/ErrorMessages#UnknownHost")
346 );
347 }
348
349 /**
350 * Replies the first nested exception of type <code>nestedClass</code> (including
351 * the root exception <code>e</code>) or null, if no such exception is found.
352 *
353 * @param <T>
354 * @param e the root exception
355 * @param nestedClass the type of the nested exception
356 * @return the first nested exception of type <code>nestedClass</code> (including
357 * the root exception <code>e</code>) or null, if no such exception is found.
358 */
359 protected static <T> T getNestedException(Exception e, Class<T> nestedClass) {
360 Throwable t = e;
361 while (t != null && !(nestedClass.isInstance(t))) {
362 t = t.getCause();
363 }
364 if (t == null)
365 return null;
366 else if (nestedClass.isInstance(t))
367 return nestedClass.cast(t);
368 return null;
369 }
370
371 /**
372 * Explains an {@see OsmTransferException} to the user.
373 *
374 * @param e the {@see OsmTransferException}
375 */
376 public static void explainOsmTransferException(OsmTransferException e) {
377 if (getNestedException(e, SecurityException.class) != null) {
378 explainSecurityException(e);
379 return;
380 }
381 if (getNestedException(e, SocketException.class) != null) {
382 explainNestedSocketException(e);
383 return;
384 }
385 if (getNestedException(e, UnknownHostException.class) != null) {
386 explainNestedUnkonwnHostException(e);
387 return;
388 }
389 if (getNestedException(e, IOException.class) != null) {
390 explainNestedIOException(e);
391 return;
392 }
393 if (getNestedException(e, IllegalDataException.class) != null) {
394 explainNestedIllegalDataException(e);
395 return;
396 }
397 if (e instanceof OsmApiInitializationException) {
398 explainOsmApiInitializationException((OsmApiInitializationException) e);
399 return;
400 }
401
402 if (e instanceof ChangesetClosedException) {
403 explainChangesetClosedException((ChangesetClosedException)e);
404 return;
405 }
406
407 if (e instanceof MissingOAuthAccessTokenException) {
408 explainMissingOAuthAccessTokenException((MissingOAuthAccessTokenException)e);
409 return;
410 }
411
412 if (e instanceof OsmApiException) {
413 OsmApiException oae = (OsmApiException) e;
414 switch(oae.getResponseCode()) {
415 case HttpURLConnection.HTTP_PRECON_FAILED:
416 explainPreconditionFailed(oae);
417 return;
418 case HttpURLConnection.HTTP_GONE:
419 explainGoneForUnknownPrimitive(oae);
420 return;
421 case HttpURLConnection.HTTP_INTERNAL_ERROR:
422 explainInternalServerError(oae);
423 return;
424 case HttpURLConnection.HTTP_BAD_REQUEST:
425 explainBadRequest(oae);
426 return;
427 case HttpURLConnection.HTTP_NOT_FOUND:
428 explainNotFound(oae);
429 return;
430 case HttpURLConnection.HTTP_CONFLICT:
431 explainConflict(oae);
432 return;
433 case HttpURLConnection.HTTP_UNAUTHORIZED:
434 explainAuthenticationFailed(oae);
435 return;
436 case HttpURLConnection.HTTP_FORBIDDEN:
437 explainAuthorizationFailed(oae);
438 return;
439 case HttpURLConnection.HTTP_CLIENT_TIMEOUT:
440 explainClientTimeout(oae);
441 return;
442 default:
443 explainGenericHttpException(oae);
444 return;
445 }
446 }
447 explainGeneric(e);
448 }
449
450 /**
451 * explains the case of an error due to a delete request on an already deleted
452 * {@see OsmPrimitive}, i.e. a HTTP response code 410, where we don't know which
453 * {@see OsmPrimitive} is causing the error.
454 *
455 * @param e the exception
456 */
457 public static void explainGoneForUnknownPrimitive(OsmApiException e) {
458 HelpAwareOptionPane.showOptionDialog(
459 Main.parent,
460 ExceptionUtil.explainGoneForUnknownPrimitive(e),
461 tr("Object deleted"),
462 JOptionPane.ERROR_MESSAGE,
463 ht("/ErrorMessages#GoneForUnknownPrimitive")
464 );
465 }
466
467 /**
468 * Explains an {@see Exception} to the user.
469 *
470 * @param e the {@see Exception}
471 */
472 public static void explainException(Exception e) {
473 if (getNestedException(e, InvocationTargetException.class) != null) {
474 explainNestedInvocationTargetException(e);
475 return;
476 }
477 if (e instanceof OsmTransferException) {
478 explainOsmTransferException((OsmTransferException) e);
479 return;
480 }
481 explainGeneric(e);
482 }
483}
Note: See TracBrowser for help on using the repository browser.