Catching and logging uncaught exceptions from classes invoked in tasks

As far as I can tell, if you don’t catch and log your exceptions/throwables inside the class invoked from a gradle task, gradle’s probably doesn’t to show it to you and you’re left with the very unhelpful: Execution failed for task ‘:parse’.

The ‘–stacktrace’ option appears to apply only to exceptions thrown from within the Gradle framework itself, as it doesn’t seem to help in the case where the task application code that is doing the throwing.

The only workaround I can see is to wrap all application classes that are called from gradle tasks with something like:

public void someMethodCalledFromGradleThatCouldThrow() throws ProcessingException {
try {
Do things.
    } catch (ProcessingException e) {
        LOG.error("Exception caught while processing", e);
        throw e;
    } catch (Throwable e) {
        LOG.error("Exception in gradle task", e);
        throw new ProcessingException(e);

Is there something I’m missing to show application stack traces from gradle tasks?

This seems like a really common use case and adding the above to everything is very non-DRY.

The exception type and message are always shown (for 'GradleException’s, only the message is shown). And ‘–stacktrace’ always shows the stack trace, no matter which exception and where it’s thrown. That’s the intended behavior, and it’s also what I see.

While it does work for test cases, we’re seeing some conditions that consistently do not provide any stack information and kill off the gradle daemon.

Here’s the tail of the output from a task that failed with no stacktrace.

Without using the daemon: gradle -b /opt/nim/parserV4/build.gradle run -d

20:52:01.570 [ERROR] [org.gradle.api.Project] org.gradle.api.internal.LocationAwareException: Script ‘/opt/nim/parserV4/workflows/replica.gradle’ line: 33 Execution failed for task ‘:transformPdfs’. 20:52:01.571 [DEBUG] [org.gradle.logging.internal.DefaultLoggingConfigurer] Finished configuring with level: DEBUG, configurers: [org.gradle.logging.internal.OutputEventRenderer@5c244f19, org.gradle.logging.internal.logback.LogbackLoggingConfigurer@53c52191, org.gradle.logging.internal.JavaUtilLoggingConfigurer@331ee18e]

Here’s the replica.gradle task that is failing:

task transformPdfs(dependsOn:parse, description: "Transform PDFs to raster images") {
 inputs.dir parseDir
 outputs.dir generatedDir
 doLast {
  PdfConverter pdf = new PdfConverter(project.ext.dataset, "${workflow}", cbCfg) // line 33

With --daemon arg, the daemon terminates with a gradle internal stack trace:

21:05:27.320 [ERROR] [org.gradle.BuildExceptionReporter] FAILURE: Build failed with an exception. 21:05:27.320 [ERROR] [org.gradle.BuildExceptionReporter] 21:05:27.321 [ERROR] [org.gradle.BuildExceptionReporter] * What went wrong: 21:05:27.321 [ERROR] [org.gradle.BuildExceptionReporter] Gradle build daemon disappeared unexpectedly (it may have been stopped, killed or may have crashed) 21:05:27.323 [ERROR] [org.gradle.BuildExceptionReporter] 21:05:27.324 [ERROR] [org.gradle.BuildExceptionReporter] * Exception is: 21:05:27.325 [ERROR] [org.gradle.BuildExceptionReporter] org.gradle.launcher.daemon.client.DaemonDisappearedException: Gradle build daemon disappeared unexpectedly (it may have been stopped, killed or may have crashed) 21:05:27.325 [ERROR] [org.gradle.BuildExceptionReporter]

at org.gradle.launcher.daemon.client.DaemonClient.handleDaemonDisappearance( 21:05:27.326 [ERROR] [org.gradle.BuildExceptionReporter]

at org.gradle.launcher.daemon.client.DaemonClient.monitorBuild( 21:05:27.326 [ERROR] [org.gradle.BuildExceptionReporter]

at org.gradle.launcher.daemon.client.DaemonClient.executeBuild( 21:05:27.326 [ERROR] [org.gradle.BuildExceptionReporter]

at org.gradle.launcher.daemon.client.DaemonClient.execute( 21:05:27.326 [ERROR] [org.gradle.BuildExceptionReporter]

at org.gradle.launcher.daemon.client.DaemonClient.execute( 21:05:27.327 [ERROR] [org.gradle.BuildExceptionReporter]

at 21:05:27.327 [ERROR] [org.gradle.BuildExceptionReporter]

at org.gradle.api.internal.Actions$RunnableActionAdapter.execute( 21:05:27.327 [ERROR] [org.gradle.BuildExceptionReporter]

at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute( 21:05:27.327 [ERROR] [org.gradle.BuildExceptionReporter]

at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute( 21:05:27.328 [ERROR] [org.gradle.BuildExceptionReporter]

at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute( 21:05:27.328 [ERROR] [org.gradle.BuildExceptionReporter]

at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute( 21:05:27.328 [ERROR] [org.gradle.BuildExceptionReporter]

at org.gradle.launcher.cli.ExceptionReportingAction.execute( 21:05:27.329 [ERROR] [org.gradle.BuildExceptionReporter]

at org.gradle.launcher.cli.ExceptionReportingAction.execute( 21:05:27.329 [ERROR] [org.gradle.BuildExceptionReporter]

at org.gradle.launcher.Main.doAction( 21:05:27.329 [ERROR] [org.gradle.BuildExceptionReporter]

at 21:05:27.329 [ERROR] [org.gradle.BuildExceptionReporter]

at org.gradle.launcher.Main.main( 21:05:27.330 [ERROR] [org.gradle.BuildExceptionReporter]

at org.gradle.launcher.bootstrap.ProcessBootstrap.runNoExit( 21:05:27.330 [ERROR] [org.gradle.BuildExceptionReporter]

at 21:05:27.330 [ERROR] [org.gradle.BuildExceptionReporter]

at org.gradle.launcher.GradleMain.main(

I can post more complete information if needed.

To make progress, we’d need a self-contained reproducible example.

I’ll work on putting together a test case.

One thought here is that gradle only print stacktraces for Exception but not Throwable. Things like NPEs are not shown.

Cannot reproduce. NPEs show just fine for me, both in the configuration and the execution phase.