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

Last change on this file since 7853 was 7205, checked in by Don-vip, 10 years ago

fix #10052 - Wrong server URL in HTTP 400 error message

  • Property svn:eol-style set to native
File size: 7.3 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, errorHeader, errorBody, null);
39 }
40
41 /**
42 * Constructs an {@code OsmApiException} with the specified detail message.
43 * The cause is not initialized, and may subsequently be initialized by a call to {@link #initCause}.
44 *
45 * @param message The detail message (which is saved for later retrieval by the {@link #getMessage} method)
46 */
47 public OsmApiException(String message) {
48 super(message);
49 }
50
51 /**
52 * Constructs an {@code OsmApiException} with the specified cause and a detail message of
53 * <tt>(cause==null ? null : cause.toString())</tt>
54 * (which typically contains the class and detail message of <tt>cause</tt>).
55 *
56 * @param cause the cause (which is saved for later retrieval by the {@link #getCause} method).
57 * A <tt>null</tt> value is permitted, and indicates that the cause is nonexistent or unknown.
58 */
59 public OsmApiException(Throwable cause) {
60 super(cause);
61 }
62
63 /**
64 * Constructs an {@code OsmApiException} with the specified detail message and cause.
65 *
66 * <p> Note that the detail message associated with {@code cause} is <i>not</i> automatically incorporated
67 * into this exception's detail message.
68 *
69 * @param message The detail message (which is saved for later retrieval by the {@link #getMessage} method)
70 * @param cause The cause (which is saved for later retrieval by the {@link #getCause} method).
71 * A null value is permitted, and indicates that the cause is nonexistent or unknown.
72 *
73 */
74 public OsmApiException(String message, Throwable cause) {
75 super(message, cause);
76 }
77
78 /**
79 * Replies the HTTP response code.
80 * @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.
81 */
82 public int getResponseCode() {
83 return responseCode;
84 }
85
86 /**
87 * Sets the HTTP response code.
88 * @param responseCode The HTTP response code replied by the OSM server. See {@link java.net.HttpURLConnection HttpURLConnection} for predefined HTTP response code values
89 */
90 public void setResponseCode(int responseCode) {
91 this.responseCode = responseCode;
92 }
93
94 /**
95 * Replies the error header.
96 * @return the error header, as transmitted in the {@code Error} field of the HTTP response header
97 */
98 public String getErrorHeader() {
99 return errorHeader;
100 }
101
102 /**
103 * Sets the error header.
104 * @param errorHeader the error header, as transmitted in the {@code Error} field of the HTTP response header
105 */
106 public void setErrorHeader(String errorHeader) {
107 this.errorHeader = errorHeader;
108 }
109
110 /**
111 * Replies the error body.
112 * @return The error body, as transmitted in the HTTP response body
113 */
114 public String getErrorBody() {
115 return errorBody;
116 }
117
118 /**
119 * Sets the error body.
120 * @param errorBody The error body, as transmitted in the HTTP response body
121 */
122 public void setErrorBody(String errorBody) {
123 this.errorBody = errorBody;
124 }
125
126 @Override
127 public String getMessage() {
128 StringBuilder sb = new StringBuilder();
129 sb.append("ResponseCode=")
130 .append(responseCode);
131 String eh = "";
132 try
133 {
134 if(errorHeader != null)
135 eh = tr(errorHeader.trim());
136 if (!eh.isEmpty()) {
137 sb.append(", Error Header=<")
138 .append(eh)
139 .append(">");
140 }
141 }
142 catch (Exception e) {
143 // Ignored
144 }
145 try
146 {
147 String eb = errorBody != null ? tr(errorBody.trim()) : "";
148 if (!eb.isEmpty() && !eb.equals(eh)) {
149 sb.append(", Error Body=<")
150 .append(eb)
151 .append(">");
152 }
153 }
154 catch (Exception e) {
155 // Ignored
156 }
157 return sb.toString();
158 }
159
160 /**
161 * Replies a message suitable to be displayed in a message dialog
162 *
163 * @return a message which is suitable to be displayed in a message dialog
164 */
165 public String getDisplayMessage() {
166 StringBuilder sb = new StringBuilder();
167 if (errorHeader != null) {
168 sb.append(tr(errorHeader));
169 sb.append(tr("(Code={0})", responseCode));
170 } else if (errorBody != null && !errorBody.trim().isEmpty()) {
171 errorBody = errorBody.trim();
172 sb.append(tr(errorBody));
173 sb.append(tr("(Code={0})", responseCode));
174 } else {
175 sb.append(tr("The server replied an error with code {0}.", responseCode));
176 }
177 return sb.toString();
178 }
179
180 /**
181 * 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.
182 * @param url the complete URL accessed when this error occured.
183 */
184 public void setAccessedUrl(String url) {
185 this.accessedUrl = url;
186 }
187
188 /**
189 * 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.
190 * @return the complete URL accessed when this error occured.
191 */
192 public String getAccessedUrl() {
193 return accessedUrl;
194 }
195}
Note: See TracBrowser for help on using the repository browser.