RxWorkManagerObservers icon indicating copy to clipboard operation
RxWorkManagerObservers copied to clipboard

Error handling inside the worker

Open VillePakarinen opened this issue 5 years ago • 1 comments

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<WorkInfo> where WorkInfo.State == Failed.

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

VillePakarinen avatar Aug 13 '19 08:08 VillePakarinen

I agree, it should also apply to getWorkDataByIdSingle

dominik-korsa avatar Feb 01 '20 14:02 dominik-korsa