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

Last change on this file since 4120 was 4020, checked in by bastiK, 13 years ago

generic handling of 403 - 'forbidden' in anticipation of server change that will reject uploads from users that haven't accepted or declined the license change

  • Property svn:eol-style set to native
File size: 16.1 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 // Fixme: add special handling that calls ExceptionUtil.explainFailedOAuthAuthorisation(e)
275 HelpAwareOptionPane.showOptionDialog(
276 Main.parent,
277 ExceptionUtil.explainFailedAuthorisation(e),
278 tr("Authorisation Failed"),
279 JOptionPane.ERROR_MESSAGE,
280 ht("/ErrorMessages#AuthenticationFailed")
281 );
282 }
283
284 /**
285 * Explains a {@see OsmApiException} which was thrown because of a
286 * client timeout (HTTP 408)
287 *
288 * @param e the exception
289 */
290 public static void explainClientTimeout(OsmApiException e) {
291 HelpAwareOptionPane.showOptionDialog(
292 Main.parent,
293 ExceptionUtil.explainClientTimeout(e),
294 tr("Client Time Out"),
295 JOptionPane.ERROR_MESSAGE,
296 ht("/ErrorMessages#ClientTimeOut")
297 );
298 }
299
300 /**
301 * Explains a {@see OsmApiException} with a generic error
302 * message.
303 *
304 * @param e the exception
305 */
306 public static void explainGenericHttpException(OsmApiException e) {
307 HelpAwareOptionPane.showOptionDialog(
308 Main.parent,
309 ExceptionUtil.explainClientTimeout(e),
310 tr("Communication with OSM server failed"),
311 JOptionPane.ERROR_MESSAGE,
312 ht("/ErrorMessages#GenericCommunicationError")
313 );
314 }
315
316 /**
317 * Explains a {@see OsmApiException} which was thrown because accessing a protected
318 * resource was forbidden.
319 *
320 * @param e the exception
321 */
322 public static void explainMissingOAuthAccessTokenException(MissingOAuthAccessTokenException e) {
323 HelpAwareOptionPane.showOptionDialog(
324 Main.parent,
325 ExceptionUtil.explainMissingOAuthAccessTokenException(e),
326 tr("Authentication failed"),
327 JOptionPane.ERROR_MESSAGE,
328 ht("/ErrorMessages#MissingOAuthAccessToken")
329 );
330 }
331
332 /**
333 * Explains a {@see UnknownHostException} which has caused an {@see OsmTransferException}.
334 * This is most likely happening when there is an error in the API URL or when
335 * local DNS services are not working.
336 *
337 * @param e the exception
338 */
339
340 public static void explainNestedUnkonwnHostException(OsmTransferException e) {
341 HelpAwareOptionPane.showOptionDialog(
342 Main.parent,
343 ExceptionUtil.explainNestedUnknownHostException(e),
344 tr("Unknown host"),
345 JOptionPane.ERROR_MESSAGE,
346 ht("/ErrorMessages#UnknownHost")
347 );
348 }
349
350 /**
351 * Replies the first nested exception of type <code>nestedClass</code> (including
352 * the root exception <code>e</code>) or null, if no such exception is found.
353 *
354 * @param <T>
355 * @param e the root exception
356 * @param nestedClass the type of the nested exception
357 * @return the first nested exception of type <code>nestedClass</code> (including
358 * the root exception <code>e</code>) or null, if no such exception is found.
359 */
360 protected static <T> T getNestedException(Exception e, Class<T> nestedClass) {
361 Throwable t = e;
362 while (t != null && !(nestedClass.isInstance(t))) {
363 t = t.getCause();
364 }
365 if (t == null)
366 return null;
367 else if (nestedClass.isInstance(t))
368 return nestedClass.cast(t);
369 return null;
370 }
371
372 /**
373 * Explains an {@see OsmTransferException} to the user.
374 *
375 * @param e the {@see OsmTransferException}
376 */
377 public static void explainOsmTransferException(OsmTransferException e) {
378 if (getNestedException(e, SecurityException.class) != null) {
379 explainSecurityException(e);
380 return;
381 }
382 if (getNestedException(e, SocketException.class) != null) {
383 explainNestedSocketException(e);
384 return;
385 }
386 if (getNestedException(e, UnknownHostException.class) != null) {
387 explainNestedUnkonwnHostException(e);
388 return;
389 }
390 if (getNestedException(e, IOException.class) != null) {
391 explainNestedIOException(e);
392 return;
393 }
394 if (getNestedException(e, IllegalDataException.class) != null) {
395 explainNestedIllegalDataException(e);
396 return;
397 }
398 if (e instanceof OsmApiInitializationException) {
399 explainOsmApiInitializationException((OsmApiInitializationException) e);
400 return;
401 }
402
403 if (e instanceof ChangesetClosedException) {
404 explainChangesetClosedException((ChangesetClosedException)e);
405 return;
406 }
407
408 if (e instanceof MissingOAuthAccessTokenException) {
409 explainMissingOAuthAccessTokenException((MissingOAuthAccessTokenException)e);
410 return;
411 }
412
413 if (e instanceof OsmApiException) {
414 OsmApiException oae = (OsmApiException) e;
415 switch(oae.getResponseCode()) {
416 case HttpURLConnection.HTTP_PRECON_FAILED:
417 explainPreconditionFailed(oae);
418 return;
419 case HttpURLConnection.HTTP_GONE:
420 explainGoneForUnknownPrimitive(oae);
421 return;
422 case HttpURLConnection.HTTP_INTERNAL_ERROR:
423 explainInternalServerError(oae);
424 return;
425 case HttpURLConnection.HTTP_BAD_REQUEST:
426 explainBadRequest(oae);
427 return;
428 case HttpURLConnection.HTTP_NOT_FOUND:
429 explainNotFound(oae);
430 return;
431 case HttpURLConnection.HTTP_CONFLICT:
432 explainConflict(oae);
433 return;
434 case HttpURLConnection.HTTP_UNAUTHORIZED:
435 explainAuthenticationFailed(oae);
436 return;
437 case HttpURLConnection.HTTP_FORBIDDEN:
438 explainAuthorizationFailed(oae);
439 return;
440 case HttpURLConnection.HTTP_CLIENT_TIMEOUT:
441 explainClientTimeout(oae);
442 return;
443 default:
444 explainGenericHttpException(oae);
445 return;
446 }
447 }
448 explainGeneric(e);
449 }
450
451 /**
452 * explains the case of an error due to a delete request on an already deleted
453 * {@see OsmPrimitive}, i.e. a HTTP response code 410, where we don't know which
454 * {@see OsmPrimitive} is causing the error.
455 *
456 * @param e the exception
457 */
458 public static void explainGoneForUnknownPrimitive(OsmApiException e) {
459 HelpAwareOptionPane.showOptionDialog(
460 Main.parent,
461 ExceptionUtil.explainGoneForUnknownPrimitive(e),
462 tr("Object deleted"),
463 JOptionPane.ERROR_MESSAGE,
464 ht("/ErrorMessages#GoneForUnknownPrimitive")
465 );
466 }
467
468 /**
469 * Explains an {@see Exception} to the user.
470 *
471 * @param e the {@see Exception}
472 */
473 public static void explainException(Exception e) {
474 if (getNestedException(e, InvocationTargetException.class) != null) {
475 explainNestedInvocationTargetException(e);
476 return;
477 }
478 if (e instanceof OsmTransferException) {
479 explainOsmTransferException((OsmTransferException) e);
480 return;
481 }
482 explainGeneric(e);
483 }
484}
Note: See TracBrowser for help on using the repository browser.