PaulinaSadowska / RxWorkManagerObservers

GNU General Public License v3.0
109 stars 9 forks source link

Error handling inside the worker #1

Open VillePakarinen opened 5 years ago

VillePakarinen commented 5 years ago

Hello,

I tested out the library I noticed one issue which might need some explaining

By the spec of WorkInfo.State: WorkInfo.State - FAILED Used to indicate that the WorkRequest has completed in a failure state.

I'm not getting this State Failed out of the worker by listening to it with

WorkManager.getInstance().getWorkInfoByIdObservable(workRequest.id)

Dummy worker class below

class UpdateWorker(appContext: Context, params: WorkerParameters) : RxWorker(appContext, params) {
. . . 
override fun createWork(): Single<Result> {
        val inputValue = inputData.getString(KEY_ID) ?: ""
        return locaRepository.getById(inputValue)
            .flatMap { object ->
                remoteRepository.update(object)
            }
            .doOnError { error ->
                Timber.e(error)
            }
            .toSingleDefault(Result.success())
            .onErrorReturnItem(Result.failure())
    }
}

When the API request fails onErrorReturnItem(Result.failure()) is getting called. However

WorkManager.getInstance().getWorkInfoByIdObservable(workRequest.id)

Function throws an error in the stream. Now what I would expect to happen is to receive Observable where WorkInfo.State == Failed.

Is this expected behaviour or could you add ignore error flag here as well ?

dominik-korsa commented 4 years ago

I agree, it should also apply to getWorkDataByIdSingle