1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.lang.reflect.InvocationTargetException;
|
---|
9 | import java.net.HttpURLConnection;
|
---|
10 | import java.net.SocketException;
|
---|
11 | import java.net.UnknownHostException;
|
---|
12 |
|
---|
13 | import javax.swing.JOptionPane;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.Main;
|
---|
16 | import org.openstreetmap.josm.io.ChangesetClosedException;
|
---|
17 | import org.openstreetmap.josm.io.OsmApiException;
|
---|
18 | import org.openstreetmap.josm.io.OsmApiInitializationException;
|
---|
19 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
20 | import org.openstreetmap.josm.tools.BugReportExceptionHandler;
|
---|
21 | import org.openstreetmap.josm.tools.ExceptionUtil;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * This utility class provides static methods which explain various exceptions to the user.
|
---|
25 | *
|
---|
26 | */
|
---|
27 | public 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 OsmApiException} which was thrown because the authentication at
|
---|
232 | * the OSM server failed
|
---|
233 | *
|
---|
234 | * @param e the exception
|
---|
235 | */
|
---|
236 | public static void explainAuthenticationFailed(OsmApiException e) {
|
---|
237 | String authMethod = Main.pref.get("osm-server.auth-method", "basic");
|
---|
238 | String msg;
|
---|
239 | if (authMethod.equals("oauth")) {
|
---|
240 | msg = ExceptionUtil.explainFailedOAuthAuthentication(e);
|
---|
241 | } else {
|
---|
242 | msg = ExceptionUtil.explainFailedBasicAuthentication(e);
|
---|
243 | }
|
---|
244 |
|
---|
245 | HelpAwareOptionPane.showOptionDialog(
|
---|
246 | Main.parent,
|
---|
247 | msg,
|
---|
248 | tr("Authentication Failed"),
|
---|
249 | JOptionPane.ERROR_MESSAGE,
|
---|
250 | ht("/ErrorMessages#AuthenticationFailed")
|
---|
251 | );
|
---|
252 | }
|
---|
253 |
|
---|
254 | /**
|
---|
255 | * Explains a {@see OsmApiException} which was thrown because accessing a protected
|
---|
256 | * resource was forbidden.
|
---|
257 | *
|
---|
258 | * @param e the exception
|
---|
259 | */
|
---|
260 | public static void explainAuthorizationFailed(OsmApiException e) {
|
---|
261 | HelpAwareOptionPane.showOptionDialog(
|
---|
262 | Main.parent,
|
---|
263 | ExceptionUtil.explainFailedOAuthAuthorisation(e),
|
---|
264 | tr("Authorisation Failed"),
|
---|
265 | JOptionPane.ERROR_MESSAGE,
|
---|
266 | ht("/ErrorMessages#AuthenticationFailed")
|
---|
267 | );
|
---|
268 | }
|
---|
269 |
|
---|
270 |
|
---|
271 | /**
|
---|
272 | * Explains a {@see UnknownHostException} which has caused an {@see OsmTransferException}.
|
---|
273 | * This is most likely happening when there is an error in the API URL or when
|
---|
274 | * local DNS services are not working.
|
---|
275 | *
|
---|
276 | * @param e the exception
|
---|
277 | */
|
---|
278 |
|
---|
279 | public static void explainNestedUnkonwnHostException(OsmTransferException e) {
|
---|
280 | HelpAwareOptionPane.showOptionDialog(
|
---|
281 | Main.parent,
|
---|
282 | ExceptionUtil.explainNestedUnkonwnHostException(e),
|
---|
283 | tr("Unknown host"),
|
---|
284 | JOptionPane.ERROR_MESSAGE,
|
---|
285 | ht("/ErrorMessages#UnknownHost")
|
---|
286 | );
|
---|
287 | }
|
---|
288 |
|
---|
289 | /**
|
---|
290 | * Replies the first nested exception of type <code>nestedClass</code> (including
|
---|
291 | * the root exception <code>e</code>) or null, if no such exception is found.
|
---|
292 | *
|
---|
293 | * @param <T>
|
---|
294 | * @param e the root exception
|
---|
295 | * @param nestedClass the type of the nested exception
|
---|
296 | * @return the first nested exception of type <code>nestedClass</code> (including
|
---|
297 | * the root exception <code>e</code>) or null, if no such exception is found.
|
---|
298 | */
|
---|
299 | protected static <T> T getNestedException(Exception e, Class<T> nestedClass) {
|
---|
300 | Throwable t = e;
|
---|
301 | while (t != null && !(nestedClass.isInstance(t))) {
|
---|
302 | t = t.getCause();
|
---|
303 | }
|
---|
304 | if (t == null)
|
---|
305 | return null;
|
---|
306 | else if (nestedClass.isInstance(t))
|
---|
307 | return nestedClass.cast(t);
|
---|
308 | return null;
|
---|
309 | }
|
---|
310 |
|
---|
311 | /**
|
---|
312 | * Explains an {@see OsmTransferException} to the user.
|
---|
313 | *
|
---|
314 | * @param e the {@see OsmTransferException}
|
---|
315 | */
|
---|
316 | public static void explainOsmTransferException(OsmTransferException e) {
|
---|
317 | if (getNestedException(e, SecurityException.class) != null) {
|
---|
318 | explainSecurityException(e);
|
---|
319 | return;
|
---|
320 | }
|
---|
321 | if (getNestedException(e, SocketException.class) != null) {
|
---|
322 | explainNestedSocketException(e);
|
---|
323 | return;
|
---|
324 | }
|
---|
325 | if (getNestedException(e, UnknownHostException.class) != null) {
|
---|
326 | explainNestedUnkonwnHostException(e);
|
---|
327 | return;
|
---|
328 | }
|
---|
329 | if (getNestedException(e, IOException.class) != null) {
|
---|
330 | explainNestedIOException(e);
|
---|
331 | return;
|
---|
332 | }
|
---|
333 | if (e instanceof OsmApiInitializationException) {
|
---|
334 | explainOsmApiInitializationException((OsmApiInitializationException) e);
|
---|
335 | return;
|
---|
336 | }
|
---|
337 |
|
---|
338 | if (e instanceof ChangesetClosedException) {
|
---|
339 | explainChangesetClosedException((ChangesetClosedException)e);
|
---|
340 | return;
|
---|
341 | }
|
---|
342 |
|
---|
343 | if (e instanceof OsmApiException) {
|
---|
344 | OsmApiException oae = (OsmApiException) e;
|
---|
345 | switch(oae.getResponseCode()) {
|
---|
346 | case HttpURLConnection.HTTP_PRECON_FAILED:
|
---|
347 | explainPreconditionFailed(oae);
|
---|
348 | return;
|
---|
349 | case HttpURLConnection.HTTP_GONE:
|
---|
350 | explainGoneForUnknownPrimitive(oae);
|
---|
351 | return;
|
---|
352 | case HttpURLConnection.HTTP_INTERNAL_ERROR:
|
---|
353 | explainInternalServerError(oae);
|
---|
354 | return;
|
---|
355 | case HttpURLConnection.HTTP_BAD_REQUEST:
|
---|
356 | explainBadRequest(oae);
|
---|
357 | return;
|
---|
358 | case HttpURLConnection.HTTP_NOT_FOUND:
|
---|
359 | explainNotFound(oae);
|
---|
360 | return;
|
---|
361 | case HttpURLConnection.HTTP_CONFLICT:
|
---|
362 | explainConflict(oae);
|
---|
363 | return;
|
---|
364 | case HttpURLConnection.HTTP_UNAUTHORIZED:
|
---|
365 | explainAuthenticationFailed(oae);
|
---|
366 | return;
|
---|
367 | case HttpURLConnection.HTTP_FORBIDDEN:
|
---|
368 | explainAuthorizationFailed(oae);
|
---|
369 | return;
|
---|
370 | }
|
---|
371 | explainGeneric(e);
|
---|
372 | }
|
---|
373 | }
|
---|
374 |
|
---|
375 | /**
|
---|
376 | * explains the case of an error due to a delete request on an already deleted
|
---|
377 | * {@see OsmPrimitive}, i.e. a HTTP response code 410, where we don't know which
|
---|
378 | * {@see OsmPrimitive} is causing the error.
|
---|
379 | *
|
---|
380 | * @param e the exception
|
---|
381 | */
|
---|
382 | public static void explainGoneForUnknownPrimitive(OsmApiException e) {
|
---|
383 | HelpAwareOptionPane.showOptionDialog(
|
---|
384 | Main.parent,
|
---|
385 | ExceptionUtil.explainGoneForUnknownPrimitive(e),
|
---|
386 | tr("Object deleted"),
|
---|
387 | JOptionPane.ERROR_MESSAGE,
|
---|
388 | ht("/ErrorMessages#GoneForUnknownPrimitive")
|
---|
389 | );
|
---|
390 | }
|
---|
391 |
|
---|
392 | /**
|
---|
393 | * Explains an {@see Exception} to the user.
|
---|
394 | *
|
---|
395 | * @param e the {@see Exception}
|
---|
396 | */
|
---|
397 | public static void explainException(Exception e) {
|
---|
398 | if (getNestedException(e, InvocationTargetException.class) != null) {
|
---|
399 | explainNestedInvocationTargetException(e);
|
---|
400 | return;
|
---|
401 | }
|
---|
402 | if (e instanceof OsmTransferException) {
|
---|
403 | explainOsmTransferException((OsmTransferException) e);
|
---|
404 | return;
|
---|
405 | }
|
---|
406 | explainGeneric(e);
|
---|
407 | }
|
---|
408 | }
|
---|