source: josm/trunk/src/org/openstreetmap/josm/io/OsmApiException.java@ 13536

Last change on this file since 13536 was 13499, checked in by Don-vip, 6 years ago

fix #16050 - nicer display of HTTP errors from OSM API

  • Property svn:eol-style set to native
File size: 10.2 KB
RevLine 
[2512]1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3import static org.openstreetmap.josm.tools.I18n.tr;
4
[12620]5import org.openstreetmap.josm.tools.Logging;
[8513]6
[5386]7/**
8 * Exception thrown when a communication error occurs when accessing the <a href="http://wiki.openstreetmap.org/wiki/API_v0.6">OSM API</a>.
9 * @see OsmApi
10 */
[2512]11public class OsmApiException extends OsmTransferException {
12
13 private int responseCode;
[13499]14 private String contentType;
[2512]15 private String errorHeader;
16 private String errorBody;
[2748]17 private String accessedUrl;
[12992]18 private String login;
[2512]19
[5386]20 /**
21 * Constructs an {@code OsmApiException} with the specified response code, error header and error body
[8509]22 * @param responseCode The HTTP response code replied by the OSM server.
23 * See {@link java.net.HttpURLConnection HttpURLConnection} for predefined HTTP response code values
[5386]24 * @param errorHeader The error header, as transmitted in the {@code Error} field of the HTTP response header
25 * @param errorBody The error body, as transmitted in the HTTP response body
[5584]26 * @param accessedUrl The complete URL accessed when this error occured
[12992]27 * @param login the login used to connect to OSM API (can be null)
[13499]28 * @param contentType the response content-type
29 * @since 13499
[5386]30 */
[13499]31 public OsmApiException(int responseCode, String errorHeader, String errorBody, String accessedUrl, String login, String contentType) {
[5584]32 this.responseCode = responseCode;
33 this.errorHeader = errorHeader;
34 this.errorBody = errorBody;
35 this.accessedUrl = accessedUrl;
[12992]36 this.login = login;
[13499]37 this.contentType = contentType;
[5584]38 }
[6070]39
[5584]40 /**
41 * Constructs an {@code OsmApiException} with the specified response code, error header and error body
[8509]42 * @param responseCode The HTTP response code replied by the OSM server.
43 * See {@link java.net.HttpURLConnection HttpURLConnection} for predefined HTTP response code values
[5584]44 * @param errorHeader The error header, as transmitted in the {@code Error} field of the HTTP response header
45 * @param errorBody The error body, as transmitted in the HTTP response body
[12992]46 * @param accessedUrl The complete URL accessed when this error occured
[13499]47 * @param login the login used to connect to OSM API (can be null)
48 * @since 12992
49 */
50 public OsmApiException(int responseCode, String errorHeader, String errorBody, String accessedUrl, String login) {
51 this(responseCode, errorHeader, errorBody, accessedUrl, login, null);
52 }
53
54 /**
55 * Constructs an {@code OsmApiException} with the specified response code, error header and error body
56 * @param responseCode The HTTP response code replied by the OSM server.
57 * See {@link java.net.HttpURLConnection HttpURLConnection} for predefined HTTP response code values
58 * @param errorHeader The error header, as transmitted in the {@code Error} field of the HTTP response header
59 * @param errorBody The error body, as transmitted in the HTTP response body
60 * @param accessedUrl The complete URL accessed when this error occured
[12992]61 * @since 5584
[5584]62 */
[12992]63 public OsmApiException(int responseCode, String errorHeader, String errorBody, String accessedUrl) {
64 this(responseCode, errorHeader, errorBody, accessedUrl, null);
65 }
66
67 /**
68 * Constructs an {@code OsmApiException} with the specified response code, error header and error body
69 * @param responseCode The HTTP response code replied by the OSM server.
70 * See {@link java.net.HttpURLConnection HttpURLConnection} for predefined HTTP response code values
71 * @param errorHeader The error header, as transmitted in the {@code Error} field of the HTTP response header
72 * @param errorBody The error body, as transmitted in the HTTP response body
73 */
[2512]74 public OsmApiException(int responseCode, String errorHeader, String errorBody) {
[7205]75 this(responseCode, errorHeader, errorBody, null);
[2512]76 }
77
[5386]78 /**
79 * Constructs an {@code OsmApiException} with the specified detail message.
80 * The cause is not initialized, and may subsequently be initialized by a call to {@link #initCause}.
81 *
82 * @param message The detail message (which is saved for later retrieval by the {@link #getMessage} method)
83 */
[2512]84 public OsmApiException(String message) {
85 super(message);
86 }
87
[5386]88 /**
[6070]89 * Constructs an {@code OsmApiException} with the specified cause and a detail message of
[13493]90 * <code>(cause==null ? null : cause.toString())</code>
91 * (which typically contains the class and detail message of <code>cause</code>).
[5386]92 *
[6070]93 * @param cause the cause (which is saved for later retrieval by the {@link #getCause} method).
[13493]94 * A <code>null</code> value is permitted, and indicates that the cause is nonexistent or unknown.
[5386]95 */
[2512]96 public OsmApiException(Throwable cause) {
97 super(cause);
98 }
99
[5386]100 /**
101 * Constructs an {@code OsmApiException} with the specified detail message and cause.
102 *
[6070]103 * <p> Note that the detail message associated with {@code cause} is <i>not</i> automatically incorporated
[5386]104 * into this exception's detail message.
105 *
106 * @param message The detail message (which is saved for later retrieval by the {@link #getMessage} method)
107 * @param cause The cause (which is saved for later retrieval by the {@link #getCause} method).
108 * A null value is permitted, and indicates that the cause is nonexistent or unknown.
109 *
110 */
111 public OsmApiException(String message, Throwable cause) {
112 super(message, cause);
113 }
114
115 /**
116 * Replies the HTTP response code.
[8509]117 * @return The HTTP response code replied by the OSM server. Refer to
118 * <a href="http://wiki.openstreetmap.org/wiki/API_v0.6">OSM API</a> to see the list of response codes returned by the API for each call.
[5386]119 */
[2512]120 public int getResponseCode() {
121 return responseCode;
122 }
123
[5386]124 /**
125 * Sets the HTTP response code.
[8509]126 * @param responseCode The HTTP response code replied by the OSM server.
127 * See {@link java.net.HttpURLConnection HttpURLConnection} for predefined HTTP response code values
[5386]128 */
[2512]129 public void setResponseCode(int responseCode) {
130 this.responseCode = responseCode;
131 }
132
[5386]133 /**
134 * Replies the error header.
135 * @return the error header, as transmitted in the {@code Error} field of the HTTP response header
136 */
[2512]137 public String getErrorHeader() {
138 return errorHeader;
139 }
140
[5386]141 /**
142 * Sets the error header.
143 * @param errorHeader the error header, as transmitted in the {@code Error} field of the HTTP response header
144 */
[2512]145 public void setErrorHeader(String errorHeader) {
146 this.errorHeader = errorHeader;
147 }
148
[5386]149 /**
150 * Replies the error body.
151 * @return The error body, as transmitted in the HTTP response body
152 */
[2512]153 public String getErrorBody() {
154 return errorBody;
155 }
156
[5386]157 /**
158 * Sets the error body.
159 * @param errorBody The error body, as transmitted in the HTTP response body
160 */
[2512]161 public void setErrorBody(String errorBody) {
162 this.errorBody = errorBody;
163 }
164
165 @Override
166 public String getMessage() {
167 StringBuilder sb = new StringBuilder();
168 sb.append("ResponseCode=")
169 .append(responseCode);
[3511]170 String eh = "";
[8342]171 try {
[8510]172 if (errorHeader != null)
[3511]173 eh = tr(errorHeader.trim());
174 if (!eh.isEmpty()) {
175 sb.append(", Error Header=<")
176 .append(eh)
[8390]177 .append('>');
[3511]178 }
[10212]179 } catch (IllegalArgumentException e) {
[5386]180 // Ignored
[12620]181 Logging.trace(e);
[3511]182 }
[8342]183 try {
[3511]184 String eb = errorBody != null ? tr(errorBody.trim()) : "";
185 if (!eb.isEmpty() && !eb.equals(eh)) {
[2512]186 sb.append(", Error Body=<")
[3511]187 .append(eb)
[8390]188 .append('>');
[2512]189 }
[10212]190 } catch (IllegalArgumentException e) {
[5386]191 // Ignored
[12620]192 Logging.trace(e);
[3511]193 }
[2512]194 return sb.toString();
195 }
196
197 /**
198 * Replies a message suitable to be displayed in a message dialog
199 *
200 * @return a message which is suitable to be displayed in a message dialog
201 */
202 public String getDisplayMessage() {
203 StringBuilder sb = new StringBuilder();
204 if (errorHeader != null) {
205 sb.append(tr(errorHeader));
206 sb.append(tr("(Code={0})", responseCode));
[6087]207 } else if (errorBody != null && !errorBody.trim().isEmpty()) {
[2512]208 errorBody = errorBody.trim();
209 sb.append(tr(errorBody));
210 sb.append(tr("(Code={0})", responseCode));
211 } else {
212 sb.append(tr("The server replied an error with code {0}.", responseCode));
213 }
214 return sb.toString();
215 }
[2748]216
[5386]217 /**
[8509]218 * Sets the complete URL accessed when this error occured.
219 * This is distinct from the one set with {@link #setUrl}, which is generally only the base URL of the server.
[5386]220 * @param url the complete URL accessed when this error occured.
221 */
[2748]222 public void setAccessedUrl(String url) {
223 this.accessedUrl = url;
224 }
225
[5386]226 /**
[8509]227 * Replies the complete URL accessed when this error occured.
228 * This is distinct from the one returned by {@link #getUrl}, which is generally only the base URL of the server.
[5386]229 * @return the complete URL accessed when this error occured.
230 */
[2748]231 public String getAccessedUrl() {
232 return accessedUrl;
233 }
[12992]234
235 /**
236 * Sets the login used to connect to OSM API.
237 * @param login the login used to connect to OSM API
238 * @since 12992
239 */
240 public void setLogin(String login) {
241 this.login = login;
242 }
243
244 /**
245 * Replies the login used to connect to OSM API.
246 * @return the login used to connect to OSM API, or {@code null}
247 * @since 12992
248 */
249 public String getLogin() {
250 return login;
251 }
[13499]252
253 /**
254 * Sets the response content-type.
255 * @param contentType the response content-type.
256 * @since 13499
257 */
258 public final void setContentType(String contentType) {
259 this.contentType = contentType;
260 }
261
262 /**
263 * Replies the response content-type.
264 * @return the response content-type
265 * @since 13499
266 */
267 public final String getContentType() {
268 return contentType;
269 }
[2512]270}
Note: See TracBrowser for help on using the repository browser.