source: josm/trunk/src/org/openstreetmap/josm/tools/ExceptionUtil.java@ 7060

Last change on this file since 7060 was 7024, checked in by Don-vip, 10 years ago

sonar - Variables should not be declared and then immediately returned or thrown

  • Property svn:eol-style set to native
File size: 30.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5import static org.openstreetmap.josm.tools.I18n.trn;
6
7import java.io.IOException;
8import java.net.HttpURLConnection;
9import java.net.MalformedURLException;
10import java.net.SocketException;
11import java.net.URL;
12import java.net.UnknownHostException;
13import java.text.DateFormat;
14import java.text.ParseException;
15import java.text.SimpleDateFormat;
16import java.util.Collection;
17import java.util.Date;
18import java.util.Locale;
19import java.util.TreeSet;
20import java.util.regex.Matcher;
21import java.util.regex.Pattern;
22
23import org.openstreetmap.josm.Main;
24import org.openstreetmap.josm.data.osm.Node;
25import org.openstreetmap.josm.data.osm.OsmPrimitive;
26import org.openstreetmap.josm.data.osm.Relation;
27import org.openstreetmap.josm.data.osm.Way;
28import org.openstreetmap.josm.gui.preferences.server.OAuthAccessTokenHolder;
29import org.openstreetmap.josm.io.ChangesetClosedException;
30import org.openstreetmap.josm.io.IllegalDataException;
31import org.openstreetmap.josm.io.MissingOAuthAccessTokenException;
32import org.openstreetmap.josm.io.OsmApi;
33import org.openstreetmap.josm.io.OsmApiException;
34import org.openstreetmap.josm.io.OsmApiInitializationException;
35import org.openstreetmap.josm.io.OsmTransferException;
36import org.openstreetmap.josm.io.auth.CredentialsManager;
37
38@SuppressWarnings("CallToThreadDumpStack")
39public final class ExceptionUtil {
40
41 private ExceptionUtil() {
42 // Hide default constructor for utils classes
43 }
44
45 /**
46 * handles an exception caught during OSM API initialization
47 *
48 * @param e the exception
49 */
50 public static String explainOsmApiInitializationException(OsmApiInitializationException e) {
51 Main.error(e);
52 return tr(
53 "<html>Failed to initialize communication with the OSM server {0}.<br>"
54 + "Check the server URL in your preferences and your internet connection.",
55 Main.pref.get("osm-server.url", OsmApi.DEFAULT_API_URL));
56 }
57
58
59 /**
60 * Creates the error message
61 *
62 * @param e the exception
63 */
64 public static String explainMissingOAuthAccessTokenException(MissingOAuthAccessTokenException e) {
65 Main.error(e);
66 return tr(
67 "<html>Failed to authenticate at the OSM server ''{0}''.<br>"
68 + "You are using OAuth to authenticate but currently there is no<br>"
69 + "OAuth Access Token configured.<br>"
70 + "Please open the Preferences Dialog and generate or enter an Access Token."
71 + "</html>",
72 Main.pref.get("osm-server.url", OsmApi.DEFAULT_API_URL)
73 );
74 }
75
76 public static Pair<OsmPrimitive, Collection<OsmPrimitive>> parsePreconditionFailed(String msg) {
77 final String ids = "(\\d+(?:,\\d+)*)";
78 final Collection<OsmPrimitive> refs = new TreeSet<>(); // error message can contain several times the same way
79 Matcher m;
80 m = Pattern.compile(".*Node (\\d+) is still used by relations " + ids + ".*").matcher(msg);
81 if (m.matches()) {
82 OsmPrimitive n = new Node(Long.parseLong(m.group(1)));
83 for (String s : m.group(2).split(",")) {
84 refs.add(new Relation(Long.parseLong(s)));
85 }
86 return Pair.create(n, refs);
87 }
88 m = Pattern.compile(".*Node (\\d+) is still used by ways " + ids + ".*").matcher(msg);
89 if (m.matches()) {
90 OsmPrimitive n = new Node(Long.parseLong(m.group(1)));
91 for (String s : m.group(2).split(",")) {
92 refs.add(new Way(Long.parseLong(s)));
93 }
94 return Pair.create(n, refs);
95 }
96 m = Pattern.compile(".*The relation (\\d+) is used in relations? " + ids + ".*").matcher(msg);
97 if (m.matches()) {
98 OsmPrimitive n = new Relation(Long.parseLong(m.group(1)));
99 for (String s : m.group(2).split(",")) {
100 refs.add(new Relation(Long.parseLong(s)));
101 }
102 return Pair.create(n, refs);
103 }
104 m = Pattern.compile(".*Way (\\d+) is still used by relations " + ids + ".*").matcher(msg);
105 if (m.matches()) {
106 OsmPrimitive n = new Way(Long.parseLong(m.group(1)));
107 for (String s : m.group(2).split(",")) {
108 refs.add(new Relation(Long.parseLong(s)));
109 }
110 return Pair.create(n, refs);
111 }
112 m = Pattern.compile(".*Way (\\d+) requires the nodes with id in " + ids + ".*").matcher(msg); // ... ", which either do not exist, or are not visible"
113 if (m.matches()) {
114 OsmPrimitive n = new Way(Long.parseLong(m.group(1)));
115 for (String s : m.group(2).split(",")) {
116 refs.add(new Node(Long.parseLong(s)));
117 }
118 return Pair.create(n, refs);
119 }
120 return null;
121 }
122
123 /**
124 * Explains an upload error due to a violated precondition, i.e. a HTTP return code 412
125 *
126 * @param e the exception
127 */
128 public static String explainPreconditionFailed(OsmApiException e) {
129 Main.error(e);
130 Pair<OsmPrimitive, Collection<OsmPrimitive>> conflict = parsePreconditionFailed(e.getErrorHeader());
131 if (conflict != null) {
132 OsmPrimitive firstRefs = conflict.b.iterator().next();
133 String objId = Long.toString(conflict.a.getId());
134 Collection<Long> refIds= Utils.transform(conflict.b, new Utils.Function<OsmPrimitive, Long>() {
135
136 @Override
137 public Long apply(OsmPrimitive x) {
138 return x.getId();
139 }
140 });
141 String refIdsString = refIds.size() == 1 ? refIds.iterator().next().toString() : refIds.toString();
142 if (conflict.a instanceof Node) {
143 if (firstRefs instanceof Node) {
144 return "<html>" + trn(
145 "<strong>Failed</strong> to delete <strong>node {0}</strong>."
146 + " It is still referred to by node {1}.<br>"
147 + "Please load the node, remove the reference to the node, and upload again.",
148 "<strong>Failed</strong> to delete <strong>node {0}</strong>."
149 + " It is still referred to by nodes {1}.<br>"
150 + "Please load the nodes, remove the reference to the node, and upload again.",
151 conflict.b.size(), objId, refIdsString) + "</html>";
152 } else if (firstRefs instanceof Way) {
153 return "<html>" + trn(
154 "<strong>Failed</strong> to delete <strong>node {0}</strong>."
155 + " It is still referred to by way {1}.<br>"
156 + "Please load the way, remove the reference to the node, and upload again.",
157 "<strong>Failed</strong> to delete <strong>node {0}</strong>."
158 + " It is still referred to by ways {1}.<br>"
159 + "Please load the ways, remove the reference to the node, and upload again.",
160 conflict.b.size(), objId, refIdsString) + "</html>";
161 } else if (firstRefs instanceof Relation) {
162 return "<html>" + trn(
163 "<strong>Failed</strong> to delete <strong>node {0}</strong>."
164 + " It is still referred to by relation {1}.<br>"
165 + "Please load the relation, remove the reference to the node, and upload again.",
166 "<strong>Failed</strong> to delete <strong>node {0}</strong>."
167 + " It is still referred to by relations {1}.<br>"
168 + "Please load the relations, remove the reference to the node, and upload again.",
169 conflict.b.size(), objId, refIdsString) + "</html>";
170 } else {
171 throw new IllegalStateException();
172 }
173 } else if (conflict.a instanceof Way) {
174 if (firstRefs instanceof Node) {
175 return "<html>" + trn(
176 "<strong>Failed</strong> to delete <strong>way {0}</strong>."
177 + " It is still referred to by node {1}.<br>"
178 + "Please load the node, remove the reference to the way, and upload again.",
179 "<strong>Failed</strong> to delete <strong>way {0}</strong>."
180 + " It is still referred to by nodes {1}.<br>"
181 + "Please load the nodes, remove the reference to the way, and upload again.",
182 conflict.b.size(), objId, refIdsString) + "</html>";
183 } else if (firstRefs instanceof Way) {
184 return "<html>" + trn(
185 "<strong>Failed</strong> to delete <strong>way {0}</strong>."
186 + " It is still referred to by way {1}.<br>"
187 + "Please load the way, remove the reference to the way, and upload again.",
188 "<strong>Failed</strong> to delete <strong>way {0}</strong>."
189 + " It is still referred to by ways {1}.<br>"
190 + "Please load the ways, remove the reference to the way, and upload again.",
191 conflict.b.size(), objId, refIdsString) + "</html>";
192 } else if (firstRefs instanceof Relation) {
193 return "<html>" + trn(
194 "<strong>Failed</strong> to delete <strong>way {0}</strong>."
195 + " It is still referred to by relation {1}.<br>"
196 + "Please load the relation, remove the reference to the way, and upload again.",
197 "<strong>Failed</strong> to delete <strong>way {0}</strong>."
198 + " It is still referred to by relations {1}.<br>"
199 + "Please load the relations, remove the reference to the way, and upload again.",
200 conflict.b.size(), objId, refIdsString) + "</html>";
201 } else {
202 throw new IllegalStateException();
203 }
204 } else if (conflict.a instanceof Relation) {
205 if (firstRefs instanceof Node) {
206 return "<html>" + trn(
207 "<strong>Failed</strong> to delete <strong>relation {0}</strong>."
208 + " It is still referred to by node {1}.<br>"
209 + "Please load the node, remove the reference to the relation, and upload again.",
210 "<strong>Failed</strong> to delete <strong>relation {0}</strong>."
211 + " It is still referred to by nodes {1}.<br>"
212 + "Please load the nodes, remove the reference to the relation, and upload again.",
213 conflict.b.size(), objId, refIdsString) + "</html>";
214 } else if (firstRefs instanceof Way) {
215 return "<html>" + trn(
216 "<strong>Failed</strong> to delete <strong>relation {0}</strong>."
217 + " It is still referred to by way {1}.<br>"
218 + "Please load the way, remove the reference to the relation, and upload again.",
219 "<strong>Failed</strong> to delete <strong>relation {0}</strong>."
220 + " It is still referred to by ways {1}.<br>"
221 + "Please load the ways, remove the reference to the relation, and upload again.",
222 conflict.b.size(), objId, refIdsString) + "</html>";
223 } else if (firstRefs instanceof Relation) {
224 return "<html>" + trn(
225 "<strong>Failed</strong> to delete <strong>relation {0}</strong>."
226 + " It is still referred to by relation {1}.<br>"
227 + "Please load the relation, remove the reference to the relation, and upload again.",
228 "<strong>Failed</strong> to delete <strong>relation {0}</strong>."
229 + " It is still referred to by relations {1}.<br>"
230 + "Please load the relations, remove the reference to the relation, and upload again.",
231 conflict.b.size(), objId, refIdsString) + "</html>";
232 } else {
233 throw new IllegalStateException();
234 }
235 } else {
236 throw new IllegalStateException();
237 }
238 } else {
239 return tr(
240 "<html>Uploading to the server <strong>failed</strong> because your current<br>"
241 + "dataset violates a precondition.<br>" + "The error message is:<br>" + "{0}" + "</html>",
242 escapeReservedCharactersHTML(e.getMessage()));
243 }
244 }
245
246 public static String explainFailedBasicAuthentication(OsmApiException e) {
247 Main.error(e);
248 return tr("<html>"
249 + "Authentication at the OSM server with the username ''{0}'' failed.<br>"
250 + "Please check the username and the password in the JOSM preferences."
251 + "</html>",
252 CredentialsManager.getInstance().getUsername()
253 );
254 }
255
256 public static String explainFailedOAuthAuthentication(OsmApiException e) {
257 Main.error(e);
258 return tr("<html>"
259 + "Authentication at the OSM server with the OAuth token ''{0}'' failed.<br>"
260 + "Please launch the preferences dialog and retrieve another OAuth token."
261 + "</html>",
262 OAuthAccessTokenHolder.getInstance().getAccessTokenKey()
263 );
264 }
265
266 public static String explainFailedAuthorisation(OsmApiException e) {
267 Main.error(e);
268 String header = e.getErrorHeader();
269 String body = e.getErrorBody();
270 String msg = null;
271 if (header != null) {
272 if (body != null && !header.equals(body)) {
273 msg = header + " (" + body + ")";
274 } else {
275 msg = header;
276 }
277 } else {
278 msg = body;
279 }
280
281 if (msg != null && !msg.isEmpty()) {
282 return tr("<html>"
283 + "Authorisation at the OSM server failed.<br>"
284 + "The server reported the following error:<br>"
285 + "''{0}''"
286 + "</html>",
287 msg
288 );
289 } else {
290 return tr("<html>"
291 + "Authorisation at the OSM server failed.<br>"
292 + "</html>"
293 );
294 }
295 }
296
297 public static String explainFailedOAuthAuthorisation(OsmApiException e) {
298 Main.error(e);
299 return tr("<html>"
300 + "Authorisation at the OSM server with the OAuth token ''{0}'' failed.<br>"
301 + "The token is not authorised to access the protected resource<br>"
302 + "''{1}''.<br>"
303 + "Please launch the preferences dialog and retrieve another OAuth token."
304 + "</html>",
305 OAuthAccessTokenHolder.getInstance().getAccessTokenKey(),
306 e.getAccessedUrl() == null ? tr("unknown") : e.getAccessedUrl()
307 );
308 }
309
310 /**
311 * Explains an OSM API exception because of a client timeout (HTTP 408).
312 *
313 * @param e the exception
314 * @return the message
315 */
316 public static String explainClientTimeout(OsmApiException e) {
317 Main.error(e);
318 return tr("<html>"
319 + "Communication with the OSM server ''{0}'' timed out. Please retry later."
320 + "</html>",
321 OsmApi.getOsmApi().getBaseUrl()
322 );
323 }
324
325 /**
326 * Replies a generic error message for an OSM API exception
327 *
328 * @param e the exception
329 * @return the message
330 */
331 public static String explainGenericOsmApiException(OsmApiException e) {
332 Main.error(e);
333 String errMsg = e.getErrorHeader();
334 if (errMsg == null) {
335 errMsg = e.getErrorBody();
336 }
337 if (errMsg == null) {
338 errMsg = tr("no error message available");
339 }
340 return tr("<html>"
341 + "Communication with the OSM server ''{0}''failed. The server replied<br>"
342 + "the following error code and the following error message:<br>"
343 + "<strong>Error code:<strong> {1}<br>"
344 + "<strong>Error message (untranslated)</strong>: {2}"
345 + "</html>",
346 OsmApi.getOsmApi().getBaseUrl(),
347 e.getResponseCode(),
348 errMsg
349 );
350 }
351
352 /**
353 * Explains an error due to a 409 conflict
354 *
355 * @param e the exception
356 */
357 public static String explainConflict(OsmApiException e) {
358 Main.error(e);
359 String msg = e.getErrorHeader();
360 if (msg != null) {
361 String pattern = "The changeset (\\d+) was closed at (.*)";
362 Pattern p = Pattern.compile(pattern);
363 Matcher m = p.matcher(msg);
364 if (m.matches()) {
365 long changesetId = Long.parseLong(m.group(1));
366 // Example: "2010-09-07 14:39:41 UTC". Always parsed with US locale, regardless
367 // of the current locale in JOSM
368 DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z", Locale.US);
369 Date closeDate = null;
370 try {
371 closeDate = formatter.parse(m.group(2));
372 } catch (ParseException ex) {
373 Main.error(tr("Failed to parse date ''{0}'' replied by server.", m.group(2)));
374 Main.error(ex);
375 }
376 if (closeDate == null) {
377 msg = tr(
378 "<html>Closing of changeset <strong>{0}</strong> failed <br>because it has already been closed.",
379 changesetId
380 );
381 } else {
382 SimpleDateFormat dateFormat = new SimpleDateFormat();
383 msg = tr(
384 "<html>Closing of changeset <strong>{0}</strong> failed<br>"
385 +" because it has already been closed on {1}.",
386 changesetId,
387 dateFormat.format(closeDate)
388 );
389 }
390 return msg;
391 }
392 msg = tr(
393 "<html>The server reported that it has detected a conflict.<br>" +
394 "Error message (untranslated):<br>{0}</html>",
395 msg
396 );
397 } else {
398 msg = tr(
399 "<html>The server reported that it has detected a conflict.");
400 }
401 return msg;
402 }
403
404 /**
405 * Explains an exception thrown during upload because the changeset which data is
406 * uploaded to is already closed.
407 *
408 * @param e the exception
409 */
410 public static String explainChangesetClosedException(ChangesetClosedException e) {
411 SimpleDateFormat dateFormat = new SimpleDateFormat();
412 Main.error(e);
413 return tr(
414 "<html>Failed to upload to changeset <strong>{0}</strong><br>"
415 +"because it has already been closed on {1}.",
416 e.getChangesetId(),
417 e.getClosedOn() == null ? "?" : dateFormat.format(e.getClosedOn())
418 );
419 }
420
421 /**
422 * Explains an exception with a generic message dialog
423 *
424 * @param e the exception
425 */
426 public static String explainGeneric(Exception e) {
427 String msg = e.getMessage();
428 if (msg == null || msg.trim().isEmpty()) {
429 msg = e.toString();
430 }
431 Main.error(e);
432 return escapeReservedCharactersHTML(msg);
433 }
434
435 /**
436 * Explains a {@link SecurityException} which has caused an {@link OsmTransferException}.
437 * This is most likely happening when user tries to access the OSM API from within an
438 * applet which wasn't loaded from the API server.
439 *
440 * @param e the exception
441 */
442
443 public static String explainSecurityException(OsmTransferException e) {
444 String apiUrl = e.getUrl();
445 String host = tr("unknown");
446 try {
447 host = new URL(apiUrl).getHost();
448 } catch (MalformedURLException ex) {
449 // shouldn't happen
450 }
451
452 return tr("<html>Failed to open a connection to the remote server<br>" + "''{0}''<br>"
453 + "for security reasons. This is most likely because you are running<br>"
454 + "in an applet and because you did not load your applet from ''{1}''.", apiUrl, host);
455 }
456
457 /**
458 * Explains a {@link SocketException} which has caused an {@link OsmTransferException}.
459 * This is most likely because there's not connection to the Internet or because
460 * the remote server is not reachable.
461 *
462 * @param e the exception
463 */
464 public static String explainNestedSocketException(OsmTransferException e) {
465 Main.error(e);
466 return tr("<html>Failed to open a connection to the remote server<br>" + "''{0}''.<br>"
467 + "Please check your internet connection.", e.getUrl());
468 }
469
470 /**
471 * Explains a {@link IOException} which has caused an {@link OsmTransferException}.
472 * This is most likely happening when the communication with the remote server is
473 * interrupted for any reason.
474 *
475 * @param e the exception
476 */
477 public static String explainNestedIOException(OsmTransferException e) {
478 IOException ioe = getNestedException(e, IOException.class);
479 Main.error(e);
480 return tr("<html>Failed to upload data to or download data from<br>" + "''{0}''<br>"
481 + "due to a problem with transferring data.<br>"
482 + "Details (untranslated): {1}</html>", e.getUrl(), ioe
483 .getMessage());
484 }
485
486 /**
487 * Explains a {@link IllegalDataException} which has caused an {@link OsmTransferException}.
488 * This is most likely happening when JOSM tries to load data in in an unsupported format.
489 *
490 * @param e the exception
491 */
492 public static String explainNestedIllegalDataException(OsmTransferException e) {
493 IllegalDataException ide = getNestedException(e, IllegalDataException.class);
494 Main.error(e);
495 return tr("<html>Failed to download data. "
496 + "Its format is either unsupported, ill-formed, and/or inconsistent.<br>"
497 + "<br>Details (untranslated): {0}</html>", ide.getMessage());
498 }
499
500 /**
501 * Explains a {@link OsmApiException} which was thrown because of an internal server
502 * error in the OSM API server..
503 *
504 * @param e the exception
505 */
506 public static String explainInternalServerError(OsmTransferException e) {
507 Main.error(e);
508 return tr("<html>The OSM server<br>" + "''{0}''<br>" + "reported an internal server error.<br>"
509 + "This is most likely a temporary problem. Please try again later.", e.getUrl());
510 }
511
512 /**
513 * Explains a {@link OsmApiException} which was thrown because of a bad
514 * request
515 *
516 * @param e the exception
517 */
518 public static String explainBadRequest(OsmApiException e) {
519 String apiUrl = OsmApi.getOsmApi().getBaseUrl();
520 String message = tr("The OSM server ''{0}'' reported a bad request.<br>", apiUrl);
521 if (e.getErrorHeader() != null &&
522 (e.getErrorHeader().startsWith("The maximum bbox") ||
523 e.getErrorHeader().startsWith("You requested too many nodes"))) {
524 message += "<br>"
525 + tr("The area you tried to download is too big or your request was too large."
526 + "<br>Either request a smaller area or use an export file provided by the OSM community.");
527 } else if (e.getErrorHeader() != null) {
528 message += tr("<br>Error message(untranslated): {0}", e.getErrorHeader());
529 }
530 Main.error(e);
531 return "<html>" + message + "</html>";
532 }
533
534 /**
535 * Explains a {@link OsmApiException} which was thrown because of
536 * bandwidth limit exceeded (HTTP error 509)
537 *
538 * @param e the exception
539 */
540 public static String explainBandwidthLimitExceeded(OsmApiException e) {
541 Main.error(e);
542 // TODO: Write a proper error message
543 return explainGenericOsmApiException(e);
544 }
545
546 /**
547 * Explains a {@link OsmApiException} which was thrown because a resource wasn't found.
548 *
549 * @param e the exception
550 */
551 public static String explainNotFound(OsmApiException e) {
552 String apiUrl = OsmApi.getOsmApi().getBaseUrl();
553 String message = tr("The OSM server ''{0}'' does not know about an object<br>"
554 + "you tried to read, update, or delete. Either the respective object<br>"
555 + "does not exist on the server or you are using an invalid URL to access<br>"
556 + "it. Please carefully check the server''s address ''{0}'' for typos."
557 , apiUrl);
558 Main.error(e);
559 return "<html>" + message + "</html>";
560 }
561
562 /**
563 * Explains a {@link UnknownHostException} which has caused an {@link OsmTransferException}.
564 * This is most likely happening when there is an error in the API URL or when
565 * local DNS services are not working.
566 *
567 * @param e the exception
568 */
569
570 public static String explainNestedUnknownHostException(OsmTransferException e) {
571 String apiUrl = e.getUrl();
572 String host = tr("unknown");
573 try {
574 host = new URL(apiUrl).getHost();
575 } catch (MalformedURLException ex) {
576 // shouldn't happen
577 }
578
579 Main.error(e);
580 return tr("<html>Failed to open a connection to the remote server<br>" + "''{0}''.<br>"
581 + "Host name ''{1}'' could not be resolved. <br>"
582 + "Please check the API URL in your preferences and your internet connection.", apiUrl, host);
583 }
584
585 /**
586 * Replies the first nested exception of type <code>nestedClass</code> (including
587 * the root exception <code>e</code>) or null, if no such exception is found.
588 *
589 * @param <T>
590 * @param e the root exception
591 * @param nestedClass the type of the nested exception
592 * @return the first nested exception of type <code>nestedClass</code> (including
593 * the root exception <code>e</code>) or null, if no such exception is found.
594 */
595 protected static <T> T getNestedException(Exception e, Class<T> nestedClass) {
596 Throwable t = e;
597 while (t != null && !(nestedClass.isInstance(t))) {
598 t = t.getCause();
599 }
600 if (t == null)
601 return null;
602 else if (nestedClass.isInstance(t))
603 return nestedClass.cast(t);
604 return null;
605 }
606
607 /**
608 * Explains an {@link OsmTransferException} to the user.
609 *
610 * @param e the {@link OsmTransferException}
611 */
612 public static String explainOsmTransferException(OsmTransferException e) {
613 if (getNestedException(e, SecurityException.class) != null)
614 return explainSecurityException(e);
615 if (getNestedException(e, SocketException.class) != null)
616 return explainNestedSocketException(e);
617 if (getNestedException(e, UnknownHostException.class) != null)
618 return explainNestedUnknownHostException(e);
619 if (getNestedException(e, IOException.class) != null)
620 return explainNestedIOException(e);
621 if (e instanceof OsmApiInitializationException)
622 return explainOsmApiInitializationException((OsmApiInitializationException) e);
623
624 if (e instanceof ChangesetClosedException)
625 return explainChangesetClosedException((ChangesetClosedException)e);
626
627 if (e instanceof OsmApiException) {
628 OsmApiException oae = (OsmApiException) e;
629 if (oae.getResponseCode() == HttpURLConnection.HTTP_PRECON_FAILED)
630 return explainPreconditionFailed(oae);
631 if (oae.getResponseCode() == HttpURLConnection.HTTP_GONE)
632 return explainGoneForUnknownPrimitive(oae);
633 if (oae.getResponseCode() == HttpURLConnection.HTTP_INTERNAL_ERROR)
634 return explainInternalServerError(oae);
635 if (oae.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST)
636 return explainBadRequest(oae);
637 if (oae.getResponseCode() == 509)
638 return explainBandwidthLimitExceeded(oae);
639 }
640 return explainGeneric(e);
641 }
642
643 /**
644 * explains the case of an error due to a delete request on an already deleted
645 * {@link OsmPrimitive}, i.e. a HTTP response code 410, where we don't know which
646 * {@link OsmPrimitive} is causing the error.
647 *
648 * @param e the exception
649 */
650 public static String explainGoneForUnknownPrimitive(OsmApiException e) {
651 return tr(
652 "<html>The server reports that an object is deleted.<br>"
653 + "<strong>Uploading failed</strong> if you tried to update or delete this object.<br> "
654 + "<strong>Downloading failed</strong> if you tried to download this object.<br>"
655 + "<br>"
656 + "The error message is:<br>" + "{0}"
657 + "</html>", escapeReservedCharactersHTML(e.getMessage()));
658 }
659
660 /**
661 * Explains an {@link Exception} to the user.
662 *
663 * @param e the {@link Exception}
664 */
665 public static String explainException(Exception e) {
666 Main.error(e);
667 if (e instanceof OsmTransferException) {
668 return explainOsmTransferException((OsmTransferException) e);
669 } else {
670 return explainGeneric(e);
671 }
672 }
673
674 /**
675 * Replaces some HTML reserved characters (&lt;, &gt; and &amp;) by their equivalent entity (&amp;lt;, &amp;gt; and &amp;amp;);
676 * @param s The unescaped string
677 * @return The escaped string
678 */
679 public static String escapeReservedCharactersHTML(String s) {
680 return s == null ? "" : s.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;");
681 }
682}
Note: See TracBrowser for help on using the repository browser.