7

Android, Retrofit, RxJava. Please look at this example call:

 mcityService.signOut(signOutRequest)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(resp ->
                {
                    busyIndicator.dismiss();
                    finish();
                }, throwable ->
                {
                    Log.d(tag, throwable.toString());
                    busyIndicator.dismiss();
                    Toast.makeText(this,throwable.getMessage(),Toast.LENGTH_LONG).show();
                    finish();
                });

Does anybody know how to get error code (as error number) from throwable? I am able to get full stacktrace or message (as shown above). How to capture error code?

Error code from throwable: enter image description here

2
  • Please look at my screenshot from debug. There is code field, but not accesible Commented Sep 19, 2017 at 7:02
  • apart form the answers check the comment by jake @ github.com/square/retrofit/issues/1218 which means any non 200 errors can be handled in onNext with ` Observable<Response<Type>>` or Observable<Result<Type>> Commented Sep 19, 2017 at 8:26

2 Answers 2

6

Just use casting??

mcityService.signOut(signOutRequest)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(resp ->
                {
                    busyIndicator.dismiss();
                    finish();
                }, throwable ->
                {
                    Log.d(tag, throwable.toString());
                    Log.d(code, ((HttpException)throwable).code());

                    busyIndicator.dismiss();
                    Toast.makeText(this,throwable.getMessage(),Toast.LENGTH_LONG).show();
                    finish();
                });
Sign up to request clarification or add additional context in comments.

2 Comments

Also need to check if throwable is actually instance of HttpException. For example, if there is no network available, it's not and app will crash
use instanceOf of your throwable
3

Retrofit 2 + Rxjava handling error here is your answer

                @Override
            public void onError(Throwable e) {
                if (e instanceof HttpException) {
                    ResponseBody body = ((HttpException) e).response().errorBody();

                    Converter<ResponseBody, Error> errorConverter =
                        application.getRetrofit().responseBodyConverter(Error.class, new Annotation[0]);
                // Convert the error body into our Error type.
                    try {
                        Error error = errorConverter.convert(body);
                        Log.i("","ERROR: " + error.message);
                        mLoginView.errorText(error.message);
                    } catch (IOException e1) {
                    e1.printStackTrace();
                    }
                 }



            static class Error{
            String message;
            }

see here for more .

1 Comment

This really sucks. I wonder why just error code is not accesible from throwable, the same way as error message. Thanks for tip anyway

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.