Retrofit/RxJava - get http response code
Asked Answered
G

2

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

Gallup answered 19/9, 2017 at 6:41 Comment(2)
Please look at my screenshot from debug. There is code field, but not accesibleGallup
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>>Bisectrix
C
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();
                });
Criseyde answered 19/9, 2017 at 7:6 Comment(2)
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 crashGallup
use instanceOf of your throwableCriseyde
M
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 .

Metencephalon answered 19/9, 2017 at 6:58 Comment(1)
This really sucks. I wonder why just error code is not accesible from throwable, the same way as error message. Thanks for tip anywayGallup

© 2022 - 2024 — McMap. All rights reserved.