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

Last change on this file since 2936 was 2895, checked in by mjulius, 14 years ago

Fix some messages

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