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