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

Last change on this file since 10228 was 10212, checked in by Don-vip, 8 years ago

sonar - squid:S2221 - "Exception" should not be caught when not required by called methods

  • Property svn:eol-style set to native
File size: 7.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3import static org.openstreetmap.josm.tools.I18n.tr;
4
5import org.openstreetmap.josm.Main;
6
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 */
11public class OsmApiException extends OsmTransferException {
12
13 private int responseCode;
14 private String errorHeader;
15 private String errorBody;
16 private String accessedUrl;
17
18 /**
19 * Constructs an {@code OsmApiException} with the specified response code, error header and error body
20 * @param responseCode The HTTP response code replied by the OSM server.
21 * See {@link java.net.HttpURLConnection HttpURLConnection} for predefined HTTP response code values
22 * @param errorHeader The error header, as transmitted in the {@code Error} field of the HTTP response header
23 * @param errorBody The error body, as transmitted in the HTTP response body
24 * @param accessedUrl The complete URL accessed when this error occured
25 * @since 5584
26 */
27 public OsmApiException(int responseCode, String errorHeader, String errorBody, String accessedUrl) {
28 this.responseCode = responseCode;
29 this.errorHeader = errorHeader;
30 this.errorBody = errorBody;
31 this.accessedUrl = accessedUrl;
32 }
33
34 /**
35 * Constructs an {@code OsmApiException} with the specified response code, error header and error body
36 * @param responseCode The HTTP response code replied by the OSM server.
37 * See {@link java.net.HttpURLConnection HttpURLConnection} for predefined HTTP response code values
38 * @param errorHeader The error header, as transmitted in the {@code Error} field of the HTTP response header
39 * @param errorBody The error body, as transmitted in the HTTP response body
40 */
41 public OsmApiException(int responseCode, String errorHeader, String errorBody) {
42 this(responseCode, errorHeader, errorBody, null);
43 }
44
45 /**
46 * Constructs an {@code OsmApiException} with the specified detail message.
47 * The cause is not initialized, and may subsequently be initialized by a call to {@link #initCause}.
48 *
49 * @param message The detail message (which is saved for later retrieval by the {@link #getMessage} method)
50 */
51 public OsmApiException(String message) {
52 super(message);
53 }
54
55 /**
56 * Constructs an {@code OsmApiException} with the specified cause and a detail message of
57 * <tt>(cause==null ? null : cause.toString())</tt>
58 * (which typically contains the class and detail message of <tt>cause</tt>).
59 *
60 * @param cause the cause (which is saved for later retrieval by the {@link #getCause} method).
61 * A <tt>null</tt> value is permitted, and indicates that the cause is nonexistent or unknown.
62 */
63 public OsmApiException(Throwable cause) {
64 super(cause);
65 }
66
67 /**
68 * Constructs an {@code OsmApiException} with the specified detail message and cause.
69 *
70 * <p> Note that the detail message associated with {@code cause} is <i>not</i> automatically incorporated
71 * into this exception's detail message.
72 *
73 * @param message The detail message (which is saved for later retrieval by the {@link #getMessage} method)
74 * @param cause The cause (which is saved for later retrieval by the {@link #getCause} method).
75 * A null value is permitted, and indicates that the cause is nonexistent or unknown.
76 *
77 */
78 public OsmApiException(String message, Throwable cause) {
79 super(message, cause);
80 }
81
82 /**
83 * Replies the HTTP response code.
84 * @return The HTTP response code replied by the OSM server. Refer to
85 * <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.
86 */
87 public int getResponseCode() {
88 return responseCode;
89 }
90
91 /**
92 * Sets the HTTP response code.
93 * @param responseCode The HTTP response code replied by the OSM server.
94 * See {@link java.net.HttpURLConnection HttpURLConnection} for predefined HTTP response code values
95 */
96 public void setResponseCode(int responseCode) {
97 this.responseCode = responseCode;
98 }
99
100 /**
101 * Replies the error header.
102 * @return the error header, as transmitted in the {@code Error} field of the HTTP response header
103 */
104 public String getErrorHeader() {
105 return errorHeader;
106 }
107
108 /**
109 * Sets the error header.
110 * @param errorHeader the error header, as transmitted in the {@code Error} field of the HTTP response header
111 */
112 public void setErrorHeader(String errorHeader) {
113 this.errorHeader = errorHeader;
114 }
115
116 /**
117 * Replies the error body.
118 * @return The error body, as transmitted in the HTTP response body
119 */
120 public String getErrorBody() {
121 return errorBody;
122 }
123
124 /**
125 * Sets the error body.
126 * @param errorBody The error body, as transmitted in the HTTP response body
127 */
128 public void setErrorBody(String errorBody) {
129 this.errorBody = errorBody;
130 }
131
132 @Override
133 public String getMessage() {
134 StringBuilder sb = new StringBuilder();
135 sb.append("ResponseCode=")
136 .append(responseCode);
137 String eh = "";
138 try {
139 if (errorHeader != null)
140 eh = tr(errorHeader.trim());
141 if (!eh.isEmpty()) {
142 sb.append(", Error Header=<")
143 .append(eh)
144 .append('>');
145 }
146 } catch (IllegalArgumentException e) {
147 // Ignored
148 if (Main.isTraceEnabled()) {
149 Main.trace(e.getMessage());
150 }
151 }
152 try {
153 String eb = errorBody != null ? tr(errorBody.trim()) : "";
154 if (!eb.isEmpty() && !eb.equals(eh)) {
155 sb.append(", Error Body=<")
156 .append(eb)
157 .append('>');
158 }
159 } catch (IllegalArgumentException e) {
160 // Ignored
161 if (Main.isTraceEnabled()) {
162 Main.trace(e.getMessage());
163 }
164 }
165 return sb.toString();
166 }
167
168 /**
169 * Replies a message suitable to be displayed in a message dialog
170 *
171 * @return a message which is suitable to be displayed in a message dialog
172 */
173 public String getDisplayMessage() {
174 StringBuilder sb = new StringBuilder();
175 if (errorHeader != null) {
176 sb.append(tr(errorHeader));
177 sb.append(tr("(Code={0})", responseCode));
178 } else if (errorBody != null && !errorBody.trim().isEmpty()) {
179 errorBody = errorBody.trim();
180 sb.append(tr(errorBody));
181 sb.append(tr("(Code={0})", responseCode));
182 } else {
183 sb.append(tr("The server replied an error with code {0}.", responseCode));
184 }
185 return sb.toString();
186 }
187
188 /**
189 * Sets the complete URL accessed when this error occured.
190 * This is distinct from the one set with {@link #setUrl}, which is generally only the base URL of the server.
191 * @param url the complete URL accessed when this error occured.
192 */
193 public void setAccessedUrl(String url) {
194 this.accessedUrl = url;
195 }
196
197 /**
198 * Replies the complete URL accessed when this error occured.
199 * This is distinct from the one returned by {@link #getUrl}, which is generally only the base URL of the server.
200 * @return the complete URL accessed when this error occured.
201 */
202 public String getAccessedUrl() {
203 return accessedUrl;
204 }
205}
Note: See TracBrowser for help on using the repository browser.