1

I'm using 'Retrofit' for http transferation.

And I made a custom ErrorHandler like below code.

public class CustomErrorHandler implements ErrorHandler {

    @Override
    public Throwable handleError( RetrofitError cause ) {

        if ( RetrofitError.Kind.NETWORK == cause.getKind() ) {

            Throwable throwable = cause.getCause();

            if ( throwable instanceof SocketTimeoutException )
                return throwable;
            else if ( throwable instanceof ConnectException )
                return throwable;
        }

        return cause;
    }

}

When I try debugging, throwable is instance of SocketTimeoutException or ConnectException in the CustomErrorHandler.handleError().

But, below fucntion throws UndeclaredThrowableException.

private SignOutSuccessResponse trySignOut( String email, String userToken ) throws Exception {

    RestAdapter.Builder builder = new RestAdapter.Builder();
    builder.setEndpoint( ServerInfo.END_POINT_API );
    builder.setErrorHandler( new CustomErrorHandler() );

    if ( BuildConfig.DEBUG )
        builder.setLogLevel( RestAdapter.LogLevel.FULL );

    API sessionAPI = builder.build().create( API.class );
    return sessionAPI.signOut( email, userToken );
}

How can I resolve this problem not this way?

I'm using

  • Android studio
  • Retrofit 1.7.1
  • OkHttp 2.0.0

Please help me ㅠㅠ

Community
  • 1
  • 1

1 Answers1

0

Both SocketTimeException and ConnectException are checked exception as you probably already know. If you don't want to declare your interface with a checked exception like prescribed in the link you posted then you'll need to wrap both exception into an unchecked exception before returning them.

Miguel
  • 19,793
  • 8
  • 56
  • 46