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