Commit e6c3ee6d authored by Ricki Hirner's avatar Ricki Hirner

Allow detailed exception handling

parent 8258787d
......@@ -38,6 +38,7 @@ public class DavAddressBook extends DavResource {
/* <!ELEMENT addressbook-query ((DAV:allprop |
DAV:propname |
DAV:prop)?, filter, limit?)>
<!ELEMENT filter (prop-filter*)>
*/
XmlSerializer serializer = XmlUtils.newSerializer();
StringWriter writer = new StringWriter();
......@@ -49,6 +50,8 @@ public class DavAddressBook extends DavResource {
serializer.startTag(XmlUtils.NS_WEBDAV, "getetag");
serializer.endTag(XmlUtils.NS_WEBDAV, "getetag");
serializer.endTag(XmlUtils.NS_WEBDAV, "prop");
serializer.startTag(XmlUtils.NS_CARDDAV, "filter");
serializer.endTag(XmlUtils.NS_CARDDAV, "filter");
serializer.endTag(XmlUtils.NS_CARDDAV, "addressbook-query");
serializer.endDocument();
......
......@@ -182,25 +182,25 @@ public class DavResource {
}
protected void checkStatus(int code, String message) throws HttpException {
protected void checkStatus(int code, String message, Response response) throws HttpException {
if (code/100 == 2)
// everything OK
return;
switch (code) {
case 412:
throw new PreconditionFailedException(message);
throw response != null ? new PreconditionFailedException(response) : new PreconditionFailedException(code, message);
default:
throw new HttpException(code, message);
throw response != null ? new HttpException(response) : new HttpException(code, message);
}
}
protected void checkStatus(Response response) throws HttpException {
checkStatus(response.code(), response.message());
checkStatus(response.code(), response.message(), response);
}
protected void checkStatus(StatusLine status) throws HttpException {
checkStatus(status.code, status.message);
checkStatus(status.code, status.message, null);
}
protected void assertMultiStatus(Response response) throws DavException {
......
......@@ -8,12 +8,83 @@
package at.bitfire.dav4android.exception;
import lombok.RequiredArgsConstructor;
import com.squareup.okhttp.Headers;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
public class HttpException extends Exception {
import java.io.IOException;
import java.io.Serializable;
import okio.Buffer;
public class HttpException extends Exception implements Serializable {
public final int status;
public final String message;
public final String request, response;
public HttpException(int status, String message) {
super(status + " " + message);
this.status = status;
this.message = message;
request = response = null;
}
public HttpException(Response response) {
super(response.code() + " " + response.message());
status = response.code();
message = response.message();
/* As we don't know the media type and character set of request and response body,
only printable ASCII characters will be shown in clear text. Other octets will
be shown as "[xx]" where xx is the hex value of the octet.
*/
// format request
Request request = response.request();
StringBuilder formatted = new StringBuilder();
formatted.append(request.method() + " " + request.urlString() + "\n");
Headers headers = request.headers();
for (String name : headers.names())
for (String value : headers.values(name))
formatted.append(name + ": " + value + "\n");
if (request.body() != null)
try {
formatted.append("\n");
Buffer buffer = new Buffer();
request.body().writeTo(buffer);
while (!buffer.exhausted())
appendByte(formatted, buffer.readByte());
} catch (IOException e) {
}
this.request = formatted.toString();
// format response
formatted = new StringBuilder();
formatted.append(response.protocol() + " " + response.code() + " " + response.message() + "\n");
headers = response.headers();
for (String name : headers.names())
for (String value : headers.values(name))
formatted.append(name + ": " + value + "\n");
if (response.body() != null)
try {
formatted.append("\n");
for (byte b : response.body().bytes())
appendByte(formatted, b);
} catch (IOException e) {
}
this.response = formatted.toString();
}
private static void appendByte(StringBuilder formatted, byte b) {
if (b >= 0x20 && b <= 0x7E) // printable ASCII
formatted.append((char)b);
else
formatted.append("[" + Integer.toHexString(b) + "]");
}
}
......@@ -8,10 +8,16 @@
package at.bitfire.dav4android.exception;
import com.squareup.okhttp.Response;
public class PreconditionFailedException extends HttpException {
public PreconditionFailedException(String message) {
super(412, message);
public PreconditionFailedException(Response response) {
super(response);
}
public PreconditionFailedException(int status, String message) {
super(status, message);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment