source: josm/trunk/src/org/openstreetmap/josm/gui/ExceptionDialogUtil.java@ 2097

Last change on this file since 2097 was 2097, checked in by Gubaer, 15 years ago

applied #3318: patch by dmuecke: josm opens modal error dialog for every block that fails to download

File size: 8.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.IOException;
7import java.net.HttpURLConnection;
8import java.net.SocketException;
9import java.net.UnknownHostException;
10
11import javax.swing.JOptionPane;
12
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.io.OsmApiException;
15import org.openstreetmap.josm.io.OsmApiInitializationException;
16import org.openstreetmap.josm.io.OsmChangesetCloseException;
17import org.openstreetmap.josm.io.OsmTransferException;
18import org.openstreetmap.josm.tools.ExceptionUtil;
19
20/**
21 * This utility class provides static methods which explain various exceptions to the user.
22 *
23 */
24public class ExceptionDialogUtil {
25
26 /**
27 * just static utility functions. no constructor
28 */
29 private ExceptionDialogUtil() {
30 }
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 JOptionPane.showMessageDialog(Main.parent, ExceptionUtil.explainOsmApiInitializationException(e), tr("Error"),
39 JOptionPane.ERROR_MESSAGE);
40 }
41
42 /**
43 * handles an exception caught during OSM API initialization
44 *
45 * @param e the exception
46 */
47 public static void explainOsmChangesetCloseException(OsmChangesetCloseException e) {
48 JOptionPane.showMessageDialog(Main.parent, ExceptionUtil.explainOsmChangesetCloseException(e), tr("Error"),
49 JOptionPane.ERROR_MESSAGE);
50 }
51
52 /**
53 * Explains an upload error due to a violated precondition, i.e. a HTTP return code 412
54 *
55 * @param e the exception
56 */
57 public static void explainPreconditionFailed(OsmApiException e) {
58 JOptionPane.showMessageDialog(Main.parent, ExceptionUtil.explainPreconditionFailed(e),
59 tr("Precondition violation"), JOptionPane.ERROR_MESSAGE);
60 }
61
62 /**
63 * Explains an exception with a generic message dialog
64 *
65 * @param e the exception
66 */
67 public static void explainGeneric(Exception e) {
68 JOptionPane.showMessageDialog(Main.parent, ExceptionUtil.explainGeneric(e), tr("Error"),
69 JOptionPane.ERROR_MESSAGE);
70 }
71
72 /**
73 * Explains a {@see SecurityException} which has caused an {@see OsmTransferException}.
74 * This is most likely happening when user tries to access the OSM API from within an
75 * applet which wasn't loaded from the API server.
76 *
77 * @param e the exception
78 */
79
80 public static void explainSecurityException(OsmTransferException e) {
81 JOptionPane.showMessageDialog(Main.parent, ExceptionUtil.explainSecurityException(e), tr("Security exception"),
82 JOptionPane.ERROR_MESSAGE);
83 }
84
85 /**
86 * Explains a {@see SocketException} which has caused an {@see OsmTransferException}.
87 * This is most likely because there's not connection to the Internet or because
88 * the remote server is not reachable.
89 *
90 * @param e the exception
91 */
92
93 public static void explainNestedSocketException(OsmTransferException e) {
94 JOptionPane.showMessageDialog(Main.parent, ExceptionUtil.explainNestedSocketException(e),
95 tr("Network exception"), JOptionPane.ERROR_MESSAGE);
96 }
97
98 /**
99 * Explains a {@see IOException} which has caused an {@see OsmTransferException}.
100 * This is most likely happening when the communication with the remote server is
101 * interrupted for any reason.
102 *
103 * @param e the exception
104 */
105
106 public static void explainNestedIOException(OsmTransferException e) {
107 JOptionPane.showMessageDialog(Main.parent, ExceptionUtil.explainNestedIOException(e), tr("IO Exception"),
108 JOptionPane.ERROR_MESSAGE);
109 }
110
111 /**
112 * Explains a {@see OsmApiException} which was thrown because of an internal server
113 * error in the OSM API server..
114 *
115 * @param e the exception
116 */
117
118 public static void explainInternalServerError(OsmTransferException e) {
119 JOptionPane.showMessageDialog(Main.parent, ExceptionUtil.explainInternalServerError(e),
120 tr("Internal Server Error"), JOptionPane.ERROR_MESSAGE);
121 }
122
123 /**
124 * Explains a {@see OsmApiException} which was thrown because of a bad
125 * request
126 *
127 * @param e the exception
128 */
129 public static void explainBadRequest(OsmApiException e) {
130 JOptionPane.showMessageDialog(Main.parent, ExceptionUtil.explainBadRequest(e), tr("Bad Request"),
131 JOptionPane.ERROR_MESSAGE);
132 }
133
134 /**
135 * Explains a {@see UnknownHostException} which has caused an {@see OsmTransferException}.
136 * This is most likely happening when there is an error in the API URL or when
137 * local DNS services are not working.
138 *
139 * @param e the exception
140 */
141
142 public static void explainNestedUnkonwnHostException(OsmTransferException e) {
143 JOptionPane.showMessageDialog(Main.parent, ExceptionUtil.explainNestedUnkonwnHostException(e),
144 tr("Unknown host"), JOptionPane.ERROR_MESSAGE);
145 }
146
147 /**
148 * Replies the first nested exception of type <code>nestedClass</code> (including
149 * the root exception <code>e</code>) or null, if no such exception is found.
150 *
151 * @param <T>
152 * @param e the root exception
153 * @param nestedClass the type of the nested exception
154 * @return the first nested exception of type <code>nestedClass</code> (including
155 * the root exception <code>e</code>) or null, if no such exception is found.
156 */
157 protected static <T> T getNestedException(Exception e, Class<T> nestedClass) {
158 Throwable t = e;
159 while (t != null && !(nestedClass.isInstance(t))) {
160 t = t.getCause();
161 }
162 if (t == null)
163 return null;
164 else if (nestedClass.isInstance(t))
165 return nestedClass.cast(t);
166 return null;
167 }
168
169 /**
170 * Explains an {@see OsmTransferException} to the user.
171 *
172 * @param e the {@see OsmTransferException}
173 */
174 public static void explainOsmTransferException(OsmTransferException e) {
175 if (getNestedException(e, SecurityException.class) != null) {
176 explainSecurityException(e);
177 return;
178 }
179 if (getNestedException(e, SocketException.class) != null) {
180 explainNestedSocketException(e);
181 return;
182 }
183 if (getNestedException(e, UnknownHostException.class) != null) {
184 explainNestedUnkonwnHostException(e);
185 return;
186 }
187 if (getNestedException(e, IOException.class) != null) {
188 explainNestedIOException(e);
189 return;
190 }
191 if (e instanceof OsmApiInitializationException) {
192 explainOsmApiInitializationException((OsmApiInitializationException) e);
193 return;
194 }
195 if (e instanceof OsmChangesetCloseException) {
196 explainOsmChangesetCloseException((OsmChangesetCloseException) e);
197 return;
198 }
199
200 if (e instanceof OsmApiException) {
201 OsmApiException oae = (OsmApiException) e;
202 if (oae.getResponseCode() == HttpURLConnection.HTTP_PRECON_FAILED) {
203 explainPreconditionFailed(oae);
204 return;
205 }
206 if (oae.getResponseCode() == HttpURLConnection.HTTP_GONE) {
207 explainGoneForUnknownPrimitive(oae);
208 return;
209 }
210 if (oae.getResponseCode() == HttpURLConnection.HTTP_INTERNAL_ERROR) {
211 explainInternalServerError(oae);
212 return;
213 }
214 if (oae.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST) {
215 explainBadRequest(oae);
216 return;
217 }
218 }
219 explainGeneric(e);
220 }
221
222 /**
223 * explains the case of an error due to a delete request on an already deleted
224 * {@see OsmPrimitive}, i.e. a HTTP response code 410, where we don't know which
225 * {@see OsmPrimitive} is causing the error.
226 *
227 * @param e the exception
228 */
229 public static void explainGoneForUnknownPrimitive(OsmApiException e) {
230 JOptionPane.showMessageDialog(Main.parent, ExceptionUtil.explainGoneForUnknownPrimitive(e),
231 tr("Primitive already deleted"), JOptionPane.ERROR_MESSAGE);
232
233 }
234
235 /**
236 * Explains an {@see Exception} to the user.
237 *
238 * @param e the {@see Exception}
239 */
240 public static void explainException(Exception e) {
241 if (e instanceof OsmTransferException) {
242 explainOsmTransferException((OsmTransferException) e);
243 return;
244 }
245 explainGeneric(e);
246 }
247}
Note: See TracBrowser for help on using the repository browser.