Posts

Showing posts from October, 2017

Custom Error Handling with RxJava & Retrofit 2

Recently I started redeveloping my Android app Monitor for EnergyHive in order to try and take advantage of some of the new techniques that "modern Android devs" are using. I must say, the many hours of reading up and research was totally worth it - MVP keeps everything much simpler, Retrofit stops me from having to build API calls myself and RxJava handles threading - letting me focus on the code instead of avoiding the dreaded memory leaks.

Before I added RxJava, in an effort to modularise things, I made use of Retrofit Callbacks. These had some extra logic that would take the erroneous server response and parse it into my own custom Exception. I would check for response error messages and error codes even when the server returned something other than code 2XX and then create a custom ApiError object. I was able to create my own Callback that extended the Retrofit callback, and performed some logic in the Retrofit methods that passed the relevant Exception to two new abstr…