Skip to content

Commit

Permalink
When a exception occured in execution, the error message is transfere…
Browse files Browse the repository at this point in the history
…d to the next executions. (#592)

* Update Helpers.cs

* Whenever an exception is occured in an execution. Error message will be transfered to the next runs.

---------

Co-authored-by: YogeshPraj <yoprajap@microsoft.com>
  • Loading branch information
taskinozdemir and YogeshPraj authored Sep 4, 2024
1 parent 95c6340 commit 4ca3cc2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
7 changes: 3 additions & 4 deletions src/RulesEngine/HelperFunctions/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ internal static class Helpers
internal static RuleFunc<RuleResultTree> ToResultTree(ReSettings reSettings, Rule rule, IEnumerable<RuleResultTree> childRuleResults, Func<object[], bool> isSuccessFunc, string exceptionMessage = "")
{
return (inputs) => {

var isSuccess = false;
var inputsDict = new Dictionary<string, object>();
string finalMessage = exceptionMessage;
try
{
inputsDict = inputs.ToDictionary(c => c.Name, c => c.Value);
isSuccess = isSuccessFunc(inputs.Select(c => c.Value).ToArray());
}
catch (Exception ex)
{
exceptionMessage = GetExceptionMessage($"Error while executing rule : {rule?.RuleName} - {ex.Message}", reSettings);
finalMessage = GetExceptionMessage($"Error while executing rule : {rule?.RuleName} - {ex.Message}", reSettings);
HandleRuleException(new RuleException(exceptionMessage,ex), rule, reSettings);
isSuccess = false;
}
Expand All @@ -38,11 +38,10 @@ internal static RuleFunc<RuleResultTree> ToResultTree(ReSettings reSettings, Rul
Inputs = inputsDict,
IsSuccess = isSuccess,
ChildResults = childRuleResults,
ExceptionMessage = exceptionMessage
ExceptionMessage = finalMessage
};

};

}

internal static RuleFunc<RuleResultTree> ToRuleExceptionResult(ReSettings reSettings, Rule rule,Exception ex)
Expand Down
33 changes: 32 additions & 1 deletion test/RulesEngine.UnitTest/BusinessRuleEngineTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,38 @@ public async Task ExecuteRule_RuntimeError_ShouldReturnAsErrorMessage()
Assert.All(result, rule => Assert.StartsWith("Error while executing rule :", rule.ExceptionMessage));
}

[Fact]
public async Task ExecuteRule_RuntimeErrorInPreviousRun_ShouldReturnEmptyErrorMessage()
{
var workflow = new Workflow {
WorkflowName = "TestWorkflow",
Rules = new[] {
new Rule {
RuleName = "ruleWithRuntimeError",
Expression = "input1.Country.ToLower() == \"india\""
}
}
};

var re = new RulesEngine(new[] { workflow }, null);
var input = new RuleTestClass {
Country = null
};

var result = await re.ExecuteAllRulesAsync("TestWorkflow", input);

Assert.NotNull(result);
Assert.All(result, rule => Assert.False(rule.IsSuccess));
Assert.All(result, rule => Assert.StartsWith("Error while executing rule :", rule.ExceptionMessage));

input.Country="india";
result = await re.ExecuteAllRulesAsync("TestWorkflow", input);

Assert.NotNull(result);
Assert.All(result, rule => Assert.True(rule.IsSuccess));
Assert.All(result, rule => Assert.Empty(rule.ExceptionMessage));
}

[Fact]
public async Task ExecuteRule_RuntimeError_ThrowsException()
{
Expand Down Expand Up @@ -1166,4 +1197,4 @@ public bool CheckExists(string str)
}

}
}
}

0 comments on commit 4ca3cc2

Please sign in to comment.