I am in the process of adding a plugin in my proje...
# community-support
r
I am in the process of adding a plugin in my project which can listen to build failures by using DataFlow Action or Shared Build Services and reports/modify the failure using Problem's api by
.throwing
call. Is it possible to catch the exception thrown by gradle-core plugin or any third-party library or plugin and modify it a bit to throw ours based on the failure or exception type?
v
Do you really mean "by plugin", or do you mean during task execution of arbitrary task X?
r
Sorry for not being clear, i meant custom plugin will be listening to any failures streaming from any task.
v
It is not particularly clean, but you can configure all tasks, then store the
actions
in a local variable, clear the actions, add a
doFirst
action that has a
try/catch
and inside calls the persisted
actions
, this way you can catch the thrown exceptions and handle them as you please, unless after that some other actions are added of course.
👀 1
j
You can do that with the new Flow API I believe (one of the few things you can actually do with that) // Flow Action Example
Copy code
abstract public class MyFlowAction implements FlowAction<RecommendationsAction.Parameter> {
    public interface Parameter extends FlowParameters {
        @Input
        Property<Throwable> getError();
    }

    @Override
    public void execute(Parameter parameters) {
        if (parameters.getError().isPresent()) {
            String message = null;
            Throwable t = parameters.getError().get();

            if (somCondition) {
                throw new RuntimeException(myOwnMessage);
            }
        }
    }
}
// Register FlowAction
Copy code
flowScope.always(MyFlowAction.class,
  spec -> spec.getParameters().getError().set(flowProviders.getBuildWorkResult().map(r -> r.getFailure().orElse(null))
));
r
Yep, i am currently using the same approach @Jendrik Johannes I was hoping to catch the exception thrown by any task and then add some transformation to it and then re-throw it again. Not sure, if we can add some transformation to the exception thrown by different tasks or just catch them and re-throw ours to avoid duplication of same exception 👀 Something similar to - https://github.com/gradle/gradle/issues/26955
v
You probably need to use the hack-around I described. 🤷‍♂️
r
Agree.