Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

okhttp - Interceptor - Stopping non-fatal exceptions from being logged to Crashlytics

I'm using Retrofit in an android app, and that in turn means im using OkHttp. I've just gone to Alpha and seeing in my crashlytics report a number of non-fatal exceptions being logged. All of these are stemming from my okhttp interceptor, and then exceptions being logged all seem to be things that are valid in situations where maybe the network is spotty or if connection drops out etc.

How can I make it so these exceptions are not logged out to crashlytics and thus cluttering up my view of exceptions occuring in the app?


Some examples of the exceptions:

> Non-fatal Exception: javax.net.ssl.SSLHandshakeException
Connection closed by peer
okhttp3.internal.connection.RealConnection.connectTls (RealConnection.java:281)
okhttp3.internal.connection.RealConnection.establishProtocol (RealConnection.java:251)
okhttp3.internal.connection.RealConnection.connect (RealConnection.java:151)
okhttp3.internal.connection.StreamAllocation.findConnection (StreamAllocation.java:192)
okhttp3.internal.connection.StreamAllocation.findHealthyConnection (StreamAllocation.java:121)
okhttp3.internal.connection.StreamAllocation.newStream (StreamAllocation.java:100)
okhttp3.internal.connection.ConnectInterceptor.intercept (ConnectInterceptor.java:42)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:92)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:67)
okhttp3.internal.cache.CacheInterceptor.intercept (CacheInterceptor.java:93)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:92)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:67)
okhttp3.internal.http.BridgeInterceptor.intercept (BridgeInterceptor.java:93)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:92)
okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept (RetryAndFollowUpInterceptor.java:120)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:92)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:67)
MY_INTERCEPTOR.intercept (AuthenticationInterceptor.java:30)

and

> Non-fatal Exception: javax.net.ssl.SSLException
Read error: ssl=0xdee45cc0: I/O error during system call, Software caused connection abort
okio.Okio$2.read (Okio.java:139)
okio.AsyncTimeout$2.read (AsyncTimeout.java:237)
okio.RealBufferedSource.indexOf (RealBufferedSource.java:345)
okio.RealBufferedSource.readUtf8LineStrict (RealBufferedSource.java:217)
okio.RealBufferedSource.readUtf8LineStrict (RealBufferedSource.java:211)
okhttp3.internal.http1.Http1Codec.readResponseHeaders (Http1Codec.java:189)
okhttp3.internal.http.CallServerInterceptor.intercept (CallServerInterceptor.java:75)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:92)
okhttp3.internal.connection.ConnectInterceptor.intercept (ConnectInterceptor.java:45)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:92)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:67)
okhttp3.internal.cache.CacheInterceptor.intercept (CacheInterceptor.java:93)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:92)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:67)
okhttp3.internal.http.BridgeInterceptor.intercept (BridgeInterceptor.java:93)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:92)
okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept (RetryAndFollowUpInterceptor.java:120)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:92)
okhttp3.internal.http.RealInterceptorChain.proceed (RealInterceptorChain.java:67)
MY_INTERCEPTOR.intercept (AuthenticationInterceptor.java:30)

This is my interceptors code:

@Override
public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();
    Response response = chain.proceed(request);

    if( response.code() == HTTP_AUTHENTICATION_ERROR_CODE ){
        Intent intent = new Intent(ACTION_LOGOUT_ACTION);
        mContext.sendBroadcast(intent);
    }
    return response;
}

I get that I could catch any IOException from the "proceed" call, but wouldnt that mess up OkHttp's proper handling of network errors etc?

like image 868
WillEllis Avatar asked Nov 29 '17 17:11

WillEllis


2 Answers

Non-fatal Exception is logged when you call Crashlytics.logException(Exception)explicitly in code

Please Read below information :

Crashlytics for Android lets you log caught exceptions in your app's catch blocks! To use this feature, simply add a call to Crashlytics.logException(Exception) to your catch block:

try {
  myMethodThatThrows();
} catch (Exception e) {
  Crashlytics.logException(e);
  // handle your exception here! 
}

All logged exceptions will appear as "non-fatal" issues in the Crashlytics dashboard. Your issue summary will contain all the state information you are use to getting from crashes along with breakdowns by Android version and hardware device.

like image 165
chandrakant sharma Avatar answered Nov 07 '22 06:11

chandrakant sharma


Crashlytics only stores Non-fatal Exception when you use
Crashlytics.logException(), see the below code for interceptor , it's throwing IOException but exception will not be visible on dashboard on Non-fatal Exception section , while we not log it.

@Override 
public Response intercept(Interceptor.Chain chain) throws IOException {
Request request = chain.request();

long t1 = System.nanoTime();
logger.info(String.format("Sending request %s on %s%n%s",
    request.url(), chain.connection(), request.headers()));

Response response = chain.proceed(request);

long t2 = System.nanoTime();
logger.info(String.format("Received response for %s in %.1fms%n%s",
    response.request().url(), (t2 - t1) / 1e6d, response.headers()));

return response;
} 

so if you want to log the exception under Non-fatal Exception , just use Crashlytics.logException();

and Crashlytics only stores the most recent 8 exceptions in a given app session. If your app throws more than 8 exceptions in a session, older exceptions are lost.

like image 41
Ashish srivastava Avatar answered Nov 07 '22 06:11

Ashish srivastava