[android] - include request urls when logging out failed http requests

This commit is contained in:
Tobrun
2018-01-04 14:15:12 +01:00
committed by Tobrun
parent 068d2f267f
commit 610fb87c76
4 changed files with 59 additions and 23 deletions

View File

@@ -30,10 +30,16 @@ import okhttp3.Response;
import okhttp3.internal.Util;
import timber.log.Timber;
import static android.util.Log.DEBUG;
import static android.util.Log.INFO;
import static android.util.Log.WARN;
class HTTPRequest implements Callback {
private static OkHttpClient mClient = new OkHttpClient.Builder().dispatcher(getDispatcher()).build();
private static boolean logEnabled = true;
private static boolean logRequestUrl = false;
private String USER_AGENT_STRING = null;
private static final int CONNECTION_ERROR = 0;
@@ -97,7 +103,7 @@ class HTTPRequest implements Callback {
mCall = mClient.newCall(mRequest);
mCall.enqueue(this);
} catch (Exception exception) {
onFailure(exception);
handleFailure(mCall, exception);
}
}
@@ -134,7 +140,7 @@ class HTTPRequest implements Callback {
try {
body = response.body().bytes();
} catch (IOException ioException) {
onFailure(ioException);
onFailure(call, ioException);
// throw ioException;
return;
} finally {
@@ -157,29 +163,16 @@ class HTTPRequest implements Callback {
@Override
public void onFailure(Call call, IOException e) {
onFailure(e);
handleFailure(call, e);
}
private void onFailure(Exception e) {
int type = PERMANENT_ERROR;
if ((e instanceof NoRouteToHostException) || (e instanceof UnknownHostException) || (e instanceof SocketException)
|| (e instanceof ProtocolException) || (e instanceof SSLException)) {
type = CONNECTION_ERROR;
} else if ((e instanceof InterruptedIOException)) {
type = TEMPORARY_ERROR;
}
private void handleFailure(Call call, Exception e) {
String errorMessage = e.getMessage() != null ? e.getMessage() : "Error processing the request";
int type = getFailureType(e);
if (logEnabled) {
if (type == TEMPORARY_ERROR) {
Timber.d("Request failed due to a temporary error: %s", errorMessage);
} else if (type == CONNECTION_ERROR) {
Timber.i("Request failed due to a connection error: %s", errorMessage);
} else {
// PERMANENT_ERROR
Timber.w("Request failed due to a permanent error: %s", errorMessage);
}
String requestUrl = call.request().url().toString();
logFailure(type, errorMessage, requestUrl);
}
mLock.lock();
@@ -189,6 +182,26 @@ class HTTPRequest implements Callback {
mLock.unlock();
}
private int getFailureType(Exception e) {
if ((e instanceof NoRouteToHostException) || (e instanceof UnknownHostException) || (e instanceof SocketException)
|| (e instanceof ProtocolException) || (e instanceof SSLException)) {
return CONNECTION_ERROR;
} else if ((e instanceof InterruptedIOException)) {
return TEMPORARY_ERROR;
}
return PERMANENT_ERROR;
}
private void logFailure(int type, String errorMessage, String requestUrl) {
Timber.log(
type == TEMPORARY_ERROR ? DEBUG : type == CONNECTION_ERROR ? INFO : WARN,
"Request failed due to a %s error: %s %s",
type == TEMPORARY_ERROR ? "temporary" : type == CONNECTION_ERROR ? "connection" : "permanent",
errorMessage,
logRequestUrl ? requestUrl : ""
);
}
private String getUserAgent() {
if (USER_AGENT_STRING == null) {
return USER_AGENT_STRING = Util.toHumanReadableAscii(
@@ -217,4 +230,8 @@ class HTTPRequest implements Callback {
static void enableLog(boolean enabled) {
logEnabled = enabled;
}
static void enablePrintRequestUrlOnFailure(boolean enabled) {
logRequestUrl = enabled;
}
}

View File

@@ -6,7 +6,7 @@ package com.mapbox.mapboxsdk.http;
public class HttpRequestUtil {
/**
* Set the log state of HttpRequest.
* Set the log state of HttpRequest. Default value is true.
* <p>
* This configuration will outlast the lifecycle of the Map.
* </p>
@@ -16,4 +16,20 @@ public class HttpRequestUtil {
public static void setLogEnabled(boolean enabled) {
HTTPRequest.enableLog(enabled);
}
/**
* Enable printing of the request url when an error occurred. Default value is false.
* <p>
* Requires {@link #setLogEnabled(boolean)} to be activated.
* </p>
* <p>
* This configuration will outlast the lifecycle of the Map.
* </p>
*
* @param enabled True will print urls, false will disable
*/
public static void setPrintRequestUrlOnFaillure(boolean enabled) {
HTTPRequest.enablePrintRequestUrlOnFailure(enabled);
}
}