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

Last change on this file since 6985 was 6087, checked in by Don-vip, 11 years ago

see #8902 - string.equals("") => string.isEmpty() (patch by shinigami)

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