Fixed
Status Update
Comments
sa...@saket.me <sa...@saket.me> #2
Some applications might want to have a different behavior. One example would be to use a custom logger, or upload an analytics event when such a case happens.
Adding another example: I'd like to fail fast and crash the application instead of only learning about issues from my users.
sa...@saket.me <sa...@saket.me> #3
I was able to write a workaround in the meantime. Sharing it here if it's useful to anyone else:
class FooWorkerFactory : WorkerFactory() {
override fun createWorker(
appContext: Context,
workerClassName: String,
workerParameters: WorkerParameters
): ListenableWorker {
val worker = …
return ThrowExceptionWorker(worker, workerParameters)
}
// Workaround for https://issuetracker.google.com/issues/261190695.
private class LogExceptionsWorker(
private val delegate: ListenableWorker,
workerParams: WorkerParameters,
) : ListenableWorker(delegate.applicationContext, workerParams) {
override fun startWork(): ListenableFuture<Result> {
return delegate.startWork().also { future ->
future.addListener(
{
try {
future.get()
} catch (e: Throwable) {
Timber.e(e, "Failed to run ${delegate::class.qualifiedName}")
}
},
{ it.run() },
)
}
}
}
}
se...@google.com <se...@google.com>
ap...@google.com <ap...@google.com> #4
Project: platform/frameworks/support
Branch: androidx-main
commit 4bdf321dc846ffdf3eacce9689fe735f423bf0fa
Author: Kuan-Ying Chou <kuanyingchou@google.com>
Date: Tue Jan 09 16:43:41 2024
Add custom exception handlers for Workers
Add workerInitializationExceptionHandler and workerExecutionExceptionHandler to Configuration so that users can set global exception handlers to handle exceptions occurred when constructing or executing a ListenableWorker. The handler will be run using Configuration.taskExecutor.
This CL also makes WorkerFactory.createWorkerWithDefaultFallback non-nullable and throws exceptions in more cases so we can pass the underlying exception to the handler. Throwing exceptions in WorkerFactory.createWorker will no longer crash the application in WorkerWrapper, RemoteWorkerWrapper, and ConstraintTrackingWorker and the exception will be intercepted by the handler.
Relnote: Add custom exception handlers for Workers
Bug: 261190695
Test: WorkerWrapperTest.java
Change-Id: Ib1b740f0503757bcc2baa90482a4e7b8d6002ffb
M work/work-multiprocess/src/androidTest/java/androidx/work/multiprocess/TrackingRemoteWorkerFactory.kt
M work/work-multiprocess/src/main/java/androidx/work/multiprocess/RemoteWorkerWrapper.kt
M work/work-runtime/api/current.txt
M work/work-runtime/api/restricted_current.txt
M work/work-runtime/src/androidTest/java/androidx/work/DefaultWorkerFactoryTest.java
M work/work-runtime/src/androidTest/java/androidx/work/impl/WorkerWrapperTest.java
M work/work-runtime/src/androidTest/java/androidx/work/impl/testutils/TrackingWorkerFactory.kt
A work/work-runtime/src/androidTest/java/androidx/work/worker/ExceptionInConstructionWorker.kt
M work/work-runtime/src/androidTest/java/androidx/work/worker/ExceptionWorker.java
M work/work-runtime/src/main/java/androidx/work/Configuration.kt
A work/work-runtime/src/main/java/androidx/work/WorkerExceptionInfo.kt
M work/work-runtime/src/main/java/androidx/work/WorkerFactory.kt
M work/work-runtime/src/main/java/androidx/work/impl/WorkerWrapper.kt
M work/work-runtime/src/main/java/androidx/work/impl/workers/ConstraintTrackingWorker.kt
M work/work-testing/src/main/java/androidx/work/testing/TestListenableWorkerBuilder.java
https://android-review.googlesource.com/2901049
Branch: androidx-main
commit 4bdf321dc846ffdf3eacce9689fe735f423bf0fa
Author: Kuan-Ying Chou <kuanyingchou@google.com>
Date: Tue Jan 09 16:43:41 2024
Add custom exception handlers for Workers
Add workerInitializationExceptionHandler and workerExecutionExceptionHandler to Configuration so that users can set global exception handlers to handle exceptions occurred when constructing or executing a ListenableWorker. The handler will be run using Configuration.taskExecutor.
This CL also makes WorkerFactory.createWorkerWithDefaultFallback non-nullable and throws exceptions in more cases so we can pass the underlying exception to the handler. Throwing exceptions in WorkerFactory.createWorker will no longer crash the application in WorkerWrapper, RemoteWorkerWrapper, and ConstraintTrackingWorker and the exception will be intercepted by the handler.
Relnote: Add custom exception handlers for Workers
Bug: 261190695
Test: WorkerWrapperTest.java
Change-Id: Ib1b740f0503757bcc2baa90482a4e7b8d6002ffb
M work/work-multiprocess/src/androidTest/java/androidx/work/multiprocess/TrackingRemoteWorkerFactory.kt
M work/work-multiprocess/src/main/java/androidx/work/multiprocess/RemoteWorkerWrapper.kt
M work/work-runtime/api/current.txt
M work/work-runtime/api/restricted_current.txt
M work/work-runtime/src/androidTest/java/androidx/work/DefaultWorkerFactoryTest.java
M work/work-runtime/src/androidTest/java/androidx/work/impl/WorkerWrapperTest.java
M work/work-runtime/src/androidTest/java/androidx/work/impl/testutils/TrackingWorkerFactory.kt
A work/work-runtime/src/androidTest/java/androidx/work/worker/ExceptionInConstructionWorker.kt
M work/work-runtime/src/androidTest/java/androidx/work/worker/ExceptionWorker.java
M work/work-runtime/src/main/java/androidx/work/Configuration.kt
A work/work-runtime/src/main/java/androidx/work/WorkerExceptionInfo.kt
M work/work-runtime/src/main/java/androidx/work/WorkerFactory.kt
M work/work-runtime/src/main/java/androidx/work/impl/WorkerWrapper.kt
M work/work-runtime/src/main/java/androidx/work/impl/workers/ConstraintTrackingWorker.kt
M work/work-testing/src/main/java/androidx/work/testing/TestListenableWorkerBuilder.java
se...@google.com <se...@google.com>
na...@google.com <na...@google.com> #5
The following release(s) address this bug.It is possible this bug has only been partially addressed:
androidx.work:work-multiprocess:2.10.0-alpha02
androidx.work:work-runtime:2.10.0-alpha02
androidx.work:work-testing:2.10.0-alpha02
Description
Component used: WorkManager Version used: 2.7.1 Devices/Android versions reproduced on: all
When Worker throws an exception, current default behavior is to log this exception to Logcat. There is no way to customize this behavior.
Some applications might want to have a different behavior. One example would be to use a custom logger, or upload an analytics event when such a case happens.
Today, Configuration class already provides a way to handle exception during initialization and during scheduling. This feature requests is to add a similar mechanism to handle exceptions from worker.