HTTPResponse handling error responses
- Contact: Roar Brænden
- Tracker:
- TLDR: HTTPResponse handling error responses
- Branch:
At the moment the HTTPClient / HTTPResponse have no good way of handling error responses from the server. They will turn up as an exception at the client.get() or client.post() calls.
My suggestion is to avoid this exception and add two methods to HTTPResponse:
-
boolean isOK()
-
InputStream getErrorStream() throws IOException
The isOK method should return true on http statusCode = 200 when the URLConnection is a HTTP connection. In case it is a FileUrlConnection it should return if the file exists.
The method getErrorStream() is for gt-http plugins that can handle an error response. The SimpleHTTPResponse would just get an exception if it tries to open a connection when the statusCode is 4xx or 5xx. Same goes for FileUrlConnection.
The method getResponseStream() should throw an exception in cases isOK would return false. The message should inform of the possibility to get the content of the error by calling getErrorResponse().
References:
Choose one of:
- Under Discussion
- In Progress
- Completed
- Rejected,
- Deferred
Voting:
- Andrea Aime:
- Ian Turton:
- Jody Garnett:
- Nuno Oliveira:
- Simone Giannecchini:
- Torben Barsballe:
- Update implementation
- Verify with test case
- Remove deprecated code
- Documentation changes
- API change make a note upgrading page.
- Update the user guide with code example
Before:
if (httpResponse.getContentType().toLowerCase().equals("application/vnd.ogc.se_xml")) {
try {
throw parseException(httpResponse.getResponseStream()); // Throws exception if status code something else than 200.
} finally {
httpResponse.dispose();
}
}
After:
if (!httpResponse.isOK()) {
try {
if (httpResponse.getContentType().toLowerCase().equals("application/vnd.ogc.se_xml")) {
throw parseException(httpResponse.getErrorStream());
} else {
throw new IOException("Server returned error on request.");
}
} finally {
httpResponse.dispose();
}
}
Before:
HTTPResponse httpResponse = httpClient.get(tile.getUrl(), headers);
try {
return ImageIOExt.readBufferedImage(httpResponse.getResponseStream());
} finally {
httpResponse.dispose();
}
After:
HTTPResponse httpResponse = httpClient.get(tile.getUrl(), headers);
try {
if (httpResponse.isOK()) {
return ImageIOExt.readBufferedImage(httpResponse.getResponseStream());
} else {
throw new IOException("Couldn't get image of tile: " + tile.getId());
}
} finally {
httpResponse.dispose();
}