0

I create retrofit2 service:

@POST("getTransactions/{page}")
    Single<List<TransactionItem>> getTransactions(@Body TransactionRequest transactionRequest, @Path("page") int page);

And try implement it in my Presenter:

private class TransactionsHandler implements SingleObserver<List<TransactionItem>> {

        @Override
        public void onSubscribe(Disposable d) {
            callView(TransactionsFragment::showProgress);
        }

        @Override
        public void onSuccess(List<TransactionItem> transactionItems) {
            //update UI
        }

        @Override
        public void onError(Throwable e) {
            //show error on UI
        }
    }

But in onError method I can get only Throwable.

In backend side I implement custom error and return it to android. For examle:

@Data
  @AllArgsConstructor
  private static class EmailException {
    private String message;
  }

return new ResponseEntity<>(new EmailException("Email not sended"), HttpStatus.INTERNAL_SERVER_ERROR);

But I can not get this errorBody from Throwable. How can I implement my custom error.

ip696
  • 6,574
  • 12
  • 65
  • 128

1 Answers1

2

Instead of this

Single<List<TransactionItem>> getTransactions(@Body TransactionRequest transactionRequest, @Path("page") int page);

Please declare it like this:

Single<Response<List<TransactionItem>>> getTransactions(@Body TransactionRequest transactionRequest, @Path("page") int page);

Also you need to update your observer to implement SingleObserver<Response<List<TransactionItem>>> instead.

Please note that in this way only the onSuccess method will be called even if the response contains an error. Then in onSuccess you can check if the response was successful or not.

Please check Get response status code using Retrofit 2.0 and RxJava for more details.

Blehi
  • 1,990
  • 1
  • 18
  • 20