|
22 | 22 | import com.palantir.gradle.failurereports.util.FailureReporterResources;
|
23 | 23 | import com.palantir.gradle.failurereports.util.ThrowableResources;
|
24 | 24 | import java.util.Optional;
|
| 25 | +import org.gradle.api.Project; |
25 | 26 | import org.gradle.api.Task;
|
26 | 27 |
|
27 | 28 | public final class ThrowableFailureReporter {
|
28 | 29 |
|
29 |
| - public static <T extends Task> FailureReport getFailureReport(T task) { |
| 30 | + public static <T extends Task> Optional<FailureReport> maybeGetFailureReport(T task) { |
30 | 31 | Throwable throwable = task.getState().getFailure();
|
31 |
| - // retrieving the last ExceptionWithSuggestion in the causal chain |
| 32 | + // try to get the last ExtraInfoException in the causal chain |
32 | 33 | Optional<ExceptionWithSuggestion> maybeExtraInfoException = Throwables.getCausalChain(throwable).stream()
|
33 | 34 | .filter(ExceptionWithSuggestion.class::isInstance)
|
34 | 35 | .map(ExceptionWithSuggestion.class::cast)
|
35 | 36 | .findFirst();
|
36 |
| - return maybeExtraInfoException |
37 |
| - .map(exception -> getExceptionWithSuggestionReport(task, throwable, exception)) |
38 |
| - .orElseGet(() -> getGenericExceptionReport(task, throwable)); |
| 37 | + return maybeExtraInfoException.map( |
| 38 | + exception -> getEnhancedExceptionReport(task.getProject(), task.getPath(), throwable, exception)); |
39 | 39 | }
|
40 | 40 |
|
41 | 41 | @SuppressWarnings("NullAway")
|
42 |
| - private static <T extends Task> FailureReport getExceptionWithSuggestionReport( |
43 |
| - T task, Throwable initialThrowable, ExceptionWithSuggestion extraInfoException) { |
44 |
| - FailureReport report = task.getProject().getObjects().newInstance(FailureReport.class); |
| 42 | + public static FailureReport getEnhancedExceptionReport( |
| 43 | + Project project, String taskPath, Throwable initialThrowable, ExceptionWithSuggestion extraInfoException) { |
| 44 | + FailureReport report = project.getObjects().newInstance(FailureReport.class); |
45 | 45 | report.getClickableSource().set(extraInfoException.getSuggestion());
|
46 | 46 | report.getErrorMessage()
|
47 | 47 | .set(ThrowableResources.formatThrowableWithMessage(initialThrowable, extraInfoException.getMessage()));
|
48 |
| - report.getHeader() |
49 |
| - .set(FailureReporterResources.getTaskErrorHeader(task.getPath(), extraInfoException.getMessage())); |
50 |
| - return report; |
51 |
| - } |
52 |
| - |
53 |
| - private static <T extends Task> FailureReport getGenericExceptionReport(T task, Throwable throwable) { |
54 |
| - FailureReport report = task.getProject().getObjects().newInstance(FailureReport.class); |
55 |
| - report.getClickableSource().set(task.getPath()); |
56 |
| - report.getErrorMessage().set(ThrowableResources.formatThrowable(throwable)); |
57 |
| - report.getHeader().set(FailureReporterResources.getTaskErrorHeader(task.getPath(), throwable)); |
| 48 | + report.getHeader().set(FailureReporterResources.getTaskErrorHeader(taskPath, extraInfoException.getMessage())); |
58 | 49 | return report;
|
59 | 50 | }
|
60 | 51 |
|
|
0 commit comments