Skip to content

Recovering failure results

rosaliafx edited this page Dec 3, 2015 · 7 revisions

If one of the tasks fails during the workflow execution it causes the whole workflow to fail. If you want to prevent this behavior for a particular task, you can use RecoverWith helper. Consider we have a version task from the previous topics:

public string Version { get; set; } // passed from Rosalia CLI

protected override void RegisterTasks()
{
    ITaskFuture<string> getVersionTask = Task(
        "svnVersion",
        new SvnVersionTask
        {
            /* ... */
        }
        .TransformWith(svnVersion => 
            string.Format("1.0.{0}.0", svnVersion.Max.Number)));
}

and we want to cover the case when SvnVersionTask fails. It would look like this:

public string Version { get; set; } // passed from Rosalia CLI

protected override void RegisterTasks()
{
    ITaskFuture<string> getVersionTask = Task(
        "svnVersion",
        new SvnVersionTask
        {
            /* ... */
        }
        .TransformWith(svnVersion => 
            string.Format("1.0.{0}.0", svnVersion.Max.Number))
        .WithPrecondition(
            string.IsNullOrEmpty(Version),  
            Version)
        .RecoverWith("1.0.0.0"));          // <---
}

The value of "1.0.0.0" will be used in case of failure. A func can be used instead of concrete value, it could be useful if you have some calculation and want to prevent it from unnesecary evaluation:

public string Version { get; set; } // passed from Rosalia CLI

protected override void RegisterTasks()
{
    ITaskFuture<string> getVersionTask = Task(
        "svnVersion",
        new SvnVersionTask
        {
            /* ... */
        }
        .TransformWith(svnVersion => 
            string.Format("1.0.{0}.0", svnVersion.Max.Number))
        .WithPrecondition(
            string.IsNullOrEmpty(Version),  
            Version)
        .RecoverWith(() => {                        // <---
            // some logic here                         <---
            // we want this logic to run               <---
            // only in failure cases, not always       <---
            return "1.0.0.0";                       // <---
        }));          
}