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

Last change on this file since 8418 was 8390, checked in by Don-vip, 9 years ago

Sonar - various performance improvements

  • 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 if(errorHeader != null)
134 eh = tr(errorHeader.trim());
135 if (!eh.isEmpty()) {
136 sb.append(", Error Header=<")
137 .append(eh)
138 .append('>');
139 }
140 } catch (Exception e) {
141 // Ignored
142 }
143 try {
144 String eb = errorBody != null ? tr(errorBody.trim()) : "";
145 if (!eb.isEmpty() && !eb.equals(eh)) {
146 sb.append(", Error Body=<")
147 .append(eb)
148 .append('>');
149 }
150 } catch (Exception e) {
151 // Ignored
152 }
153 return sb.toString();
154 }
155
156 /**
157 * Replies a message suitable to be displayed in a message dialog
158 *
159 * @return a message which is suitable to be displayed in a message dialog
160 */
161 public String getDisplayMessage() {
162 StringBuilder sb = new StringBuilder();
163 if (errorHeader != null) {
164 sb.append(tr(errorHeader));
165 sb.append(tr("(Code={0})", responseCode));
166 } else if (errorBody != null && !errorBody.trim().isEmpty()) {
167 errorBody = errorBody.trim();
168 sb.append(tr(errorBody));
169 sb.append(tr("(Code={0})", responseCode));
170 } else {
171 sb.append(tr("The server replied an error with code {0}.", responseCode));
172 }
173 return sb.toString();
174 }
175
176 /**
177 * 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.
178 * @param url the complete URL accessed when this error occured.
179 */
180 public void setAccessedUrl(String url) {
181 this.accessedUrl = url;
182 }
183
184 /**
185 * 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.
186 * @return the complete URL accessed when this error occured.
187 */
188 public String getAccessedUrl() {
189 return accessedUrl;
190 }
191}
Note: See TracBrowser for help on using the repository browser.