1
0
mirror of https://github.com/etesync/android synced 2025-01-23 14:10:54 +00:00

HttpException: add a way to override response error message.

Before this change exceptions would print the message of the error code.
For example, for 500 they would print "Internal Server Error".
With this change we can now override this message with something more
sensible we got from the serer, for example "User is inactive."
This commit is contained in:
Tom Hacohen 2017-02-28 23:51:11 +00:00
parent fc939a3cbe
commit 571c585292

View File

@ -43,8 +43,8 @@ public class Exceptions {
public static class HttpException extends Exception implements Serializable { public static class HttpException extends Exception implements Serializable {
public final int status; final int status;
public final String message; final String message;
public final String request, response; public final String request, response;
@ -65,10 +65,14 @@ public class Exceptions {
} }
public HttpException(Response response) { public HttpException(Response response) {
this(response, null);
}
public HttpException(Response response, String custom_message) {
super(response.code() + " " + response.message()); super(response.code() + " " + response.message());
status = response.code(); status = response.code();
message = response.message(); message = (custom_message != null) ? custom_message : response.message();
/* As we don't know the media type and character set of request and response body, /* 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 only printable ASCII characters will be shown in clear text. Other octets will
@ -104,7 +108,7 @@ public class Exceptions {
// format response // format response
formatted = new StringBuilder(); formatted = new StringBuilder();
formatted.append(response.protocol()).append(" ").append(response.code()).append(" ").append(response.message()).append("\n"); formatted.append(response.protocol()).append(" ").append(response.code()).append(" ").append(message).append("\n");
headers = response.headers(); headers = response.headers();
for (String name : headers.names()) for (String name : headers.names())
for (String value : headers.values(name)) for (String value : headers.values(name))