https://kotlinlang.org logo
Title
k

Kashif

01/03/2020, 7:20 AM
is it the responsibility of ViewModel to decide either it is network error and notify ui or it is RemoteDataSource responsibility to decide it is network error, server error etc ?
h

Hitender Pannu

01/03/2020, 9:15 AM
RemoteDataSource will be the first one to receive the error so I think if it will be better if it can decided what kind of error it is and view Model should be the one which will decide what error message or icon should be displayed for that particular error. There can be multiple viewModel observing the RemoteDataSource and in this way each viewModel can take independent decisions for what to do in case when they get that error. You will get more suggestions if you ask this in right channel may be android-architecture or join the android slack community
z

zhuinden

01/03/2020, 9:48 AM
sounds like your data source should decide the error type of the error it gets
k

Kashif

01/03/2020, 9:53 AM
after reading some tutorials and documentation of
RxJava2CallAdapterFactory
, retrofits says ”
Direct body (e.g., {@code Observable<User>}) calls {@code onNext} with the deserialized body
* for 2XX responses and calls {@code onError} with {@link HttpException} for non-2XX responses and
* {@link IOException} for network errors
which means i need to declare base error class BaseError.kt which will check if it is IOException then show network failure error and if it is HttpException then get the error message from it. This BaseError class will be extends by ViewModels which update ui via livedata
h

Hitender Pannu

01/03/2020, 10:00 AM
Yes, you can create a sealed class with custom exceptions that you need to and a mapper which will map the retrofit errors to those custom exceptions.
but i am not sure why view Model will extend BaseError?
k

Kashif

01/03/2020, 10:02 AM
Exceptions will be directed directly to viewModel either via Repo/RemoteDataSource so it will be good to determine which exception it is in viewModel base class
h

Hitender Pannu

01/03/2020, 10:04 AM
this option might look tempting however viewModel should not be concerned about the retrofit errors. In future you might want to replace retrofit with some other library which will throw some different exceptions. In that case you will have to refactor everything.
👍 1
k

Kashif

01/03/2020, 10:05 AM
good catch
so yes then RemoteDataSource or Repo will be good point to take decision and mapping them