Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OnError<TData> #2445

Closed
NooNameR opened this issue Jan 11, 2019 · 5 comments
Closed

Add OnError<TData> #2445

NooNameR opened this issue Jan 11, 2019 · 5 comments
Milestone

Comments

@NooNameR
Copy link

NooNameR commented Jan 11, 2019

Would be nice to see OnError<TData> as well
And consider to have OnError(Action<ICakeContext>) and OnError<TData>(Action<TData,ICakeContext>)

@devlead
Copy link
Member

devlead commented Jan 11, 2019

Relates to Gitter discussion https://gitter.im/cake-build/cake?at=5c385c932e25e453d76409f9

Is there any plans to do OnError?

Suggested current workaround https://gitter.im/cake-build/cake?at=5c3883d883c7e37765510bc9

Example:

public class BuildData
{
    public bool HasError { get; set; }
}

Setup(context=>new BuildData());

Task("Boom")
    .Does(()=>throw new Exception("Boom"))
    .OnError(exception => {
            Error(exception);
            var data = Context.Data.Get<BuildData>();
            data.HasError = true;
        });

Teardown<BuildData>((teardownContext, data) =>
{
    if (data.HasError)
    {
        Error("There was errors.");
    }
    else
    {
        Information("There was no errors");
    }
});

RunTarget("Boom");

Will output
image

@yansklyarenko
Copy link

@devlead Could you please elaborate on this one?

As far as I understand, when the requested method overload is implemented, the sample above will transform to this:

public class BuildData
{
    public bool HasError { get; set; }
}

Setup(context=>new BuildData());

Task("Boom")
    .Does(()=>throw new Exception("Boom"))
    .OnError<BuildData>((exception, data) => {
            Error(exception);
            data.HasError = true;
        });

Teardown<BuildData>((teardownContext, data) =>
{
    if (data.HasError)
    {
        Error("There was errors.");
    }
    else
    {
        Information("There was no errors");
    }
});

RunTarget("Boom");

It means there must be a method OnError<TData>(this CakeTaskBuilder builder, Action<Exception, TData> errorHandler), which should end up setting ErrorHandler property of the CakeTask to some value of type Action<Exception>. But where do I get the data from? There's no global Context like in the sample above, nor there's a parameter of type ICakeContext.

I investigated the Does functionality which looks similar and has the necessary overload. However, it also has ICakeContext parameter and gets TData from there when it's needed.

I might be missing something obvious, but it doesn't match in my head right now... I would appreciate some hint. Thanks!

@devlead
Copy link
Member

devlead commented Feb 14, 2019

There's a global ICakeContext Context property available in non static methods and delegates in scripts.

So Context.Data.Get<BuildData>(); will work as long as your not in a class or static method/delegate.

But to sort a OnError<BuildData> it would require a little bit more work internally.

@yansklyarenko
Copy link

But to sort a OnError<BuildData> it would require a little bit more work internally.

Yep, that's clear. I'd like to take over this challenge and would appreciate some hint regarding what that work internally could be. Is there a similar logic in the project I can leverage to understand what should be changed?

@yansklyarenko
Copy link

@devlead I think I got your point. I would appreciate your review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants