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 |
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.net.HttpURLConnection;
|
---|
8 | import java.net.MalformedURLException;
|
---|
9 | import java.net.SocketException;
|
---|
10 | import java.net.URL;
|
---|
11 | import java.net.UnknownHostException;
|
---|
12 | import java.util.regex.Matcher;
|
---|
13 | import java.util.regex.Pattern;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.Main;
|
---|
16 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
17 | import org.openstreetmap.josm.io.OsmApi;
|
---|
18 | import org.openstreetmap.josm.io.OsmApiException;
|
---|
19 | import org.openstreetmap.josm.io.OsmApiInitializationException;
|
---|
20 | import org.openstreetmap.josm.io.OsmChangesetCloseException;
|
---|
21 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
22 |
|
---|
23 | public class ExceptionUtil {
|
---|
24 | private ExceptionUtil() {
|
---|
25 | }
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * handles an exception caught during OSM API initialization
|
---|
29 | *
|
---|
30 | * @param e the exception
|
---|
31 | */
|
---|
32 | public static String explainOsmApiInitializationException(OsmApiInitializationException e) {
|
---|
33 | e.printStackTrace();
|
---|
34 | String msg = tr(
|
---|
35 | "<html>Failed to initialize communication with the OSM server {0}.<br>"
|
---|
36 | + "Check the server URL in your preferences and your internet connection.</html>", Main.pref.get(
|
---|
37 | "osm-server.url", "http://api.openstreetmap.org/api"));
|
---|
38 | return msg;
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * handles an exception caught during OSM API initialization
|
---|
43 | *
|
---|
44 | * @param e the exception
|
---|
45 | */
|
---|
46 | public static String explainOsmChangesetCloseException(OsmChangesetCloseException e) {
|
---|
47 | e.printStackTrace();
|
---|
48 | String changsetId = e.getChangeset() == null ? tr("unknown") : Long.toString(e.getChangeset().getId());
|
---|
49 | String msg = tr(
|
---|
50 | "<html>Failed to close changeset ''{0}'' on the OSM server ''{1}''.<br>"
|
---|
51 | + "The changeset will automatically be closed by the server after a timeout.</html>", changsetId,
|
---|
52 | Main.pref.get("osm-server.url", "http://api.openstreetmap.org/api"));
|
---|
53 | return msg;
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Explains a precondition exception when a child relation could not be deleted because
|
---|
58 | * it is still referred to by an undeleted parent relation.
|
---|
59 | *
|
---|
60 | * @param e the exception
|
---|
61 | * @param childRelation the child relation
|
---|
62 | * @param parentRelation the parent relation
|
---|
63 | * @return
|
---|
64 | */
|
---|
65 | public static String explainDeletedRelationStillInUse(OsmApiException e, long childRelation, long parentRelation) {
|
---|
66 | String msg = tr(
|
---|
67 | "<html><strong>Failed</strong> to delete <strong>relation {0}</strong>."
|
---|
68 | + " It is still referred to by relation {1}.<br>"
|
---|
69 | + "Please load relation {1}, remove the reference to relation {0}, and upload again.</html>",
|
---|
70 | childRelation,parentRelation
|
---|
71 | );
|
---|
72 | return msg;
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Explains an upload error due to a violated precondition, i.e. a HTTP return code 412
|
---|
77 | *
|
---|
78 | * @param e the exception
|
---|
79 | */
|
---|
80 | public static String explainPreconditionFailed(OsmApiException e) {
|
---|
81 | e.printStackTrace();
|
---|
82 | String msg = e.getErrorHeader();
|
---|
83 | if (msg != null) {
|
---|
84 | String pattern = "Precondition failed: The relation (\\d+) is used in relation (\\d+)\\.";
|
---|
85 | Pattern p = Pattern.compile(pattern);
|
---|
86 | Matcher m = p.matcher(msg);
|
---|
87 | if (m.matches()) {
|
---|
88 | long childRelation = Long.parseLong(m.group(1));
|
---|
89 | long parentRelation = Long.parseLong(m.group(2));
|
---|
90 | return explainDeletedRelationStillInUse(e, childRelation, parentRelation);
|
---|
91 | }
|
---|
92 | }
|
---|
93 | msg = tr(
|
---|
94 | "<html>Uploading to the server <strong>failed</strong> because your current<br>"
|
---|
95 | + "dataset violates a precondition.<br>" + "The error message is:<br>" + "{0}" + "</html>", e
|
---|
96 | .getMessage().replace("&", "&").replace("<", "<").replace(">", ">"));
|
---|
97 | return msg;
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Explains an exception with a generic message dialog
|
---|
102 | *
|
---|
103 | * @param e the exception
|
---|
104 | */
|
---|
105 | public static String explainGeneric(Exception e) {
|
---|
106 | String msg = e.getMessage();
|
---|
107 | if (msg == null || msg.trim().equals("")) {
|
---|
108 | msg = e.toString();
|
---|
109 | }
|
---|
110 | e.printStackTrace();
|
---|
111 | return msg;
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Explains a {@see SecurityException} which has caused an {@see OsmTransferException}.
|
---|
116 | * This is most likely happening when user tries to access the OSM API from within an
|
---|
117 | * applet which wasn't loaded from the API server.
|
---|
118 | *
|
---|
119 | * @param e the exception
|
---|
120 | */
|
---|
121 |
|
---|
122 | public static String explainSecurityException(OsmTransferException e) {
|
---|
123 | String apiUrl = OsmApi.getOsmApi().getBaseUrl();
|
---|
124 | String host = tr("unknown");
|
---|
125 | try {
|
---|
126 | host = new URL(apiUrl).getHost();
|
---|
127 | } catch (MalformedURLException ex) {
|
---|
128 | // shouldn't happen
|
---|
129 | }
|
---|
130 |
|
---|
131 | String message = tr("<html>Failed to open a connection to the remote server<br>" + "''{0}''<br>"
|
---|
132 | + "for security reasons. This is most likely because you are running<br>"
|
---|
133 | + "in an applet and because you didn''t load your applet from ''{1}''.</html>", apiUrl, host);
|
---|
134 | return message;
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Explains a {@see SocketException} which has caused an {@see OsmTransferException}.
|
---|
139 | * This is most likely because there's not connection to the Internet or because
|
---|
140 | * the remote server is not reachable.
|
---|
141 | *
|
---|
142 | * @param e the exception
|
---|
143 | */
|
---|
144 |
|
---|
145 | public static String explainNestedSocketException(OsmTransferException e) {
|
---|
146 | String apiUrl = OsmApi.getOsmApi().getBaseUrl();
|
---|
147 | String message = tr("<html>Failed to open a connection to the remote server<br>" + "''{0}''.<br>"
|
---|
148 | + "Please check your internet connection.</html>", apiUrl);
|
---|
149 | e.printStackTrace();
|
---|
150 | return message;
|
---|
151 | }
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Explains a {@see IOException} which has caused an {@see OsmTransferException}.
|
---|
155 | * This is most likely happening when the communication with the remote server is
|
---|
156 | * interrupted for any reason.
|
---|
157 | *
|
---|
158 | * @param e the exception
|
---|
159 | */
|
---|
160 |
|
---|
161 | public static String explainNestedIOException(OsmTransferException e) {
|
---|
162 | IOException ioe = getNestedException(e, IOException.class);
|
---|
163 | String apiUrl = OsmApi.getOsmApi().getBaseUrl();
|
---|
164 | String message = tr("<html>Failed to upload data to or download data from<br>" + "''{0}''<br>"
|
---|
165 | + "due to a problem with transferring data.<br>" + "Details(untranslated): {1}</html>", apiUrl, ioe
|
---|
166 | .getMessage());
|
---|
167 | e.printStackTrace();
|
---|
168 | return message;
|
---|
169 | }
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Explains a {@see OsmApiException} which was thrown because of an internal server
|
---|
173 | * error in the OSM API server..
|
---|
174 | *
|
---|
175 | * @param e the exception
|
---|
176 | */
|
---|
177 |
|
---|
178 | public static String explainInternalServerError(OsmTransferException e) {
|
---|
179 | String apiUrl = OsmApi.getOsmApi().getBaseUrl();
|
---|
180 | String message = tr("<html>The OSM server<br>" + "''{0}''<br>" + "reported an internal server error.<br>"
|
---|
181 | + "This is most likely a temporary problem. Please try again later.</html>", apiUrl);
|
---|
182 | e.printStackTrace();
|
---|
183 | return message;
|
---|
184 | }
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Explains a {@see OsmApiException} which was thrown because of a bad
|
---|
188 | * request
|
---|
189 | *
|
---|
190 | * @param e the exception
|
---|
191 | */
|
---|
192 | public static String explainBadRequest(OsmApiException e) {
|
---|
193 | String apiUrl = OsmApi.getOsmApi().getBaseUrl();
|
---|
194 | String message = tr("The OSM server ''{0}'' reported a bad request.<br>", apiUrl);
|
---|
195 | if (e.getErrorHeader() != null && e.getErrorHeader().startsWith("The maximum bbox")) {
|
---|
196 | message += "<br>"
|
---|
197 | + tr("The area you tried to download is too big or your request was too large."
|
---|
198 | + "<br>Either request a smaller area or use an export file provided by the OSM community.");
|
---|
199 | } else if (e.getErrorHeader() != null) {
|
---|
200 | message += tr("<br>Error message(untranslated): {0}", e.getErrorHeader());
|
---|
201 | }
|
---|
202 | message = "<html>" + message + "</html>";
|
---|
203 | e.printStackTrace();
|
---|
204 | return message;
|
---|
205 | }
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Explains a {@see OsmApiException} which was thrown because a resource wasn't found.
|
---|
209 | *
|
---|
210 | * @param e the exception
|
---|
211 | */
|
---|
212 | public static String explainNotFound(OsmApiException e) {
|
---|
213 | String apiUrl = OsmApi.getOsmApi().getBaseUrl();
|
---|
214 | String message = tr("The OSM server ''{0}'' doesn''t know about an object<br>"
|
---|
215 | + "you tried to read, update, or delete. Either the respective object<br>"
|
---|
216 | + "doesn''t exist on the server or you''re using an invalid URL to access<br>"
|
---|
217 | + "it. Please carefully check the servers address ''{0}'' for typos."
|
---|
218 | , apiUrl);
|
---|
219 | message = "<html>" + message + "</html>";
|
---|
220 | e.printStackTrace();
|
---|
221 | return message;
|
---|
222 | }
|
---|
223 |
|
---|
224 | /**
|
---|
225 | * Explains a {@see UnknownHostException} which has caused an {@see OsmTransferException}.
|
---|
226 | * This is most likely happening when there is an error in the API URL or when
|
---|
227 | * local DNS services are not working.
|
---|
228 | *
|
---|
229 | * @param e the exception
|
---|
230 | */
|
---|
231 |
|
---|
232 | public static String explainNestedUnkonwnHostException(OsmTransferException e) {
|
---|
233 | String apiUrl = OsmApi.getOsmApi().getBaseUrl();
|
---|
234 | String host = tr("unknown");
|
---|
235 | try {
|
---|
236 | host = new URL(apiUrl).getHost();
|
---|
237 | } catch (MalformedURLException ex) {
|
---|
238 | // shouldn't happen
|
---|
239 | }
|
---|
240 |
|
---|
241 | String message = tr("<html>Failed to open a connection to the remote server<br>" + "''{0}''.<br>"
|
---|
242 | + "Host name ''{1}'' couldn''t be resolved. <br>"
|
---|
243 | + "Please check the API URL in your preferences and your internet connection.</html>", apiUrl, host);
|
---|
244 | e.printStackTrace();
|
---|
245 | return message;
|
---|
246 | }
|
---|
247 |
|
---|
248 | /**
|
---|
249 | * Replies the first nested exception of type <code>nestedClass</code> (including
|
---|
250 | * the root exception <code>e</code>) or null, if no such exception is found.
|
---|
251 | *
|
---|
252 | * @param <T>
|
---|
253 | * @param e the root exception
|
---|
254 | * @param nestedClass the type of the nested exception
|
---|
255 | * @return the first nested exception of type <code>nestedClass</code> (including
|
---|
256 | * the root exception <code>e</code>) or null, if no such exception is found.
|
---|
257 | */
|
---|
258 | protected static <T> T getNestedException(Exception e, Class<T> nestedClass) {
|
---|
259 | Throwable t = e;
|
---|
260 | while (t != null && !(nestedClass.isInstance(t))) {
|
---|
261 | t = t.getCause();
|
---|
262 | }
|
---|
263 | if (t == null)
|
---|
264 | return null;
|
---|
265 | else if (nestedClass.isInstance(t))
|
---|
266 | return nestedClass.cast(t);
|
---|
267 | return null;
|
---|
268 | }
|
---|
269 |
|
---|
270 | /**
|
---|
271 | * Explains an {@see OsmTransferException} to the user.
|
---|
272 | *
|
---|
273 | * @param e the {@see OsmTransferException}
|
---|
274 | */
|
---|
275 | public static String explainOsmTransferException(OsmTransferException e) {
|
---|
276 | if (getNestedException(e, SecurityException.class) != null)
|
---|
277 | return explainSecurityException(e);
|
---|
278 | if (getNestedException(e, SocketException.class) != null)
|
---|
279 | return explainNestedSocketException(e);
|
---|
280 | if (getNestedException(e, UnknownHostException.class) != null)
|
---|
281 | return explainNestedUnkonwnHostException(e);
|
---|
282 | if (getNestedException(e, IOException.class) != null)
|
---|
283 | return explainNestedIOException(e);
|
---|
284 | if (e instanceof OsmApiInitializationException)
|
---|
285 | return explainOsmApiInitializationException((OsmApiInitializationException) e);
|
---|
286 | if (e instanceof OsmChangesetCloseException)
|
---|
287 | return explainOsmChangesetCloseException((OsmChangesetCloseException) e);
|
---|
288 |
|
---|
289 | if (e instanceof OsmApiException) {
|
---|
290 | OsmApiException oae = (OsmApiException) e;
|
---|
291 | if (oae.getResponseCode() == HttpURLConnection.HTTP_PRECON_FAILED)
|
---|
292 | return explainPreconditionFailed(oae);
|
---|
293 | if (oae.getResponseCode() == HttpURLConnection.HTTP_GONE)
|
---|
294 | return explainGoneForUnknownPrimitive(oae);
|
---|
295 | if (oae.getResponseCode() == HttpURLConnection.HTTP_INTERNAL_ERROR)
|
---|
296 | return explainInternalServerError(oae);
|
---|
297 | if (oae.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST)
|
---|
298 | return explainBadRequest(oae);
|
---|
299 | }
|
---|
300 | return explainGeneric(e);
|
---|
301 | }
|
---|
302 |
|
---|
303 | /**
|
---|
304 | * explains the case of an error due to a delete request on an already deleted
|
---|
305 | * {@see OsmPrimitive}, i.e. a HTTP response code 410, where we don't know which
|
---|
306 | * {@see OsmPrimitive} is causing the error.
|
---|
307 | *
|
---|
308 | * @param e the exception
|
---|
309 | */
|
---|
310 | public static String explainGoneForUnknownPrimitive(OsmApiException e) {
|
---|
311 | String msg = tr(
|
---|
312 | "<html>The server reports that an object is deleted.<br>"
|
---|
313 | + "<strong>Uploading failed</strong> if you tried to update or delete this object.<br> "
|
---|
314 | + "<strong>Downloading failed</strong> if you tried to download this object.<br>"
|
---|
315 | + "<br>"
|
---|
316 | + "The error message is:<br>" + "{0}"
|
---|
317 | + "</html>", e.getMessage().replace("&", "&").replace("<", "<").replace(">", ">"));
|
---|
318 | return msg;
|
---|
319 |
|
---|
320 | }
|
---|
321 |
|
---|
322 | /**
|
---|
323 | * Explains an {@see Exception} to the user.
|
---|
324 | *
|
---|
325 | * @param e the {@see Exception}
|
---|
326 | */
|
---|
327 | public static String explainException(Exception e) {
|
---|
328 | String msg = "";
|
---|
329 | if (e instanceof OsmTransferException) {
|
---|
330 | msg = explainOsmTransferException((OsmTransferException) e);
|
---|
331 | } else {
|
---|
332 | msg = explainGeneric(e);
|
---|
333 | }
|
---|
334 | e.printStackTrace();
|
---|
335 | return msg;
|
---|
336 | }
|
---|
337 | }
|
---|