Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Retrofit ErrorHandler gives UndeclaredThrowableException

Based on this post How should I handle "No internet connection" with Retrofit on Android

I made a custom ErrorHandler

private static class CustomErrorHandler implements ErrorHandler {

    @Override
    public Throwable handleError(RetrofitError error) {
        if (error.isNetworkError()) {
            return new MyNetworkError();
        }

        return error.getCause();
    }
}

And now my application crashes and I get this:

java.lang.reflect.UndeclaredThrowableException

Is there some configuration needed for the adapter? Using Retrofit 1.6.0 with OkHttp 2.0.0

Thanks.

like image 246
Niko Avatar asked Jul 07 '14 16:07

Niko


1 Answers

If you are returning a checked exception you need to declare it on the interface.

interface MyService {
  Foo doFoo() throws MyNetworkError;
}
like image 127
Jake Wharton Avatar answered Oct 13 '22 18:10

Jake Wharton