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);
        e.printStackTrace();
        throw e;
    } catch (Throwable e) {
        LOG.error("Exception in gradle task", e);
        e.printStackTrace();
        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
 outputs.files("${parseDir}/${ParserConstants.CFG_FILENAME}")
 doLast {
  generatedDir.deleteDir()
  generatedDir.mkdirs()
  PdfConverter pdf = new PdfConverter(project.ext.dataset, "${workflow}", cbCfg) // line 33
  pdf.processPdfs(generatedDir)
 }
}

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(DaemonClient.java:232) 21:05:27.326 [ERROR] [org.gradle.BuildExceptionReporter]

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

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

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

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

at org.gradle.launcher.cli.RunBuildAction.run(RunBuildAction.java:50) 21:05:27.327 [ERROR] [org.gradle.BuildExceptionReporter]

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

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

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

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

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

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

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

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

at org.gradle.launcher.bootstrap.EntryPoint.run(EntryPoint.java:45) 21:05:27.329 [ERROR] [org.gradle.BuildExceptionReporter]

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

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

at org.gradle.launcher.bootstrap.ProcessBootstrap.run(ProcessBootstrap.java:32) 21:05:27.330 [ERROR] [org.gradle.BuildExceptionReporter]

at org.gradle.launcher.GradleMain.main(GradleMain.java:23)

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.