Table of Contents

Error handling

How RulesEngine reports problems depends on two settings, EnableExceptionAsErrorMessage and IgnoreException, and on where the problem happens.

Rule problems (parse, compile and runtime errors)

flowchart TD
    problem["A rule fails to parse, compile or run"] --> data["Exception.Data gets RuleName and Expression"]
    data --> asmsg{"EnableExceptionAsErrorMessage<br/>(default true)"}
    asmsg -- false --> thrown(["Exception is thrown to the caller<br/>(RuleException for most errors)"])
    asmsg -- true --> failed["Rule result: IsSuccess = false"]
    failed --> ignore{"IgnoreException<br/>(default false)"}
    ignore -- true --> empty(["ExceptionMessage is empty"])
    ignore -- false --> msg(["ExceptionMessage describes the error"])

Figure 1. What happens to a problem inside a rule.

Setting Default Effect
EnableExceptionAsErrorMessage true Problems become a failed result with an ExceptionMessage. When false, the exception is thrown instead.
IgnoreException false When true, ExceptionMessage is left empty for rule errors, and an exception raised by an action is not rethrown.
EnableExceptionAsErrorMessageForRuleExpressionParsing true When false, an expression that cannot be parsed is silently replaced by a default value (false for rules) instead of producing an error. Leave it at true unless you have a specific reason.

The failure of one rule never prevents the others in the workflow from being evaluated when errors are reported as messages.

Message formats

Situation ExceptionMessage starts with
Expression cannot be parsed Exception while parsing expression followed by the expression and the parser's detail
Rule throws while running (for example a null reference) Error while executing rule : followed by the rule name and the exception message
Rule or global parameter cannot be compiled Error while compiling rule followed by the rule name and the reason
A local or global parameter throws while running Error while executing scoped params for rule followed by the rule name and the reason
An input has the same name as a global parameter GlobalParam name '<name>' collides with an input RuleParameter of the same name

When a method called from an expression is declared to return object, the parse error includes a hint to give the method a concrete return type, because member access and operators cannot be resolved on object.

Always thrown

These problems are programming or configuration errors and are not affected by the settings above.

Situation Exception
Workflow or rule is invalid when added RuleValidationException (derives from FluentValidation's ValidationException)
AddWorkflow with a name that already exists RuleValidationException; use AddOrUpdateWorkflow to replace
Executing a workflow that is not registered ArgumentException
ExecuteActionWorkflowAsync with an unknown rule name ArgumentException
A rule refers to an action that is not registered KeyNotFoundException
WorkflowsToInject names a workflow that is not registered Exception (Could not find injected Workflow)
Circular WorkflowsToInject InvalidOperationException
The cancellation token is cancelled OperationCanceledException

Action errors

An exception thrown by an action is wrapped (Exception while executing <ActionType>: ...) and stored in RuleResultTree.ActionResult.Exception. It is rethrown to the caller, with the rule name in Exception.Data["RuleName"], only when both EnableExceptionAsErrorMessage and IgnoreException are false.

Formatted error messages

When a rule fails and has no error from the engine, Rule.ErrorMessage is copied to ExceptionMessage after placeholders of the form $(name) are replaced (EnableFormattedErrorMessage, default true).

Placeholder Replaced with
$(input1) The input serialized as JSON (strings keep their quotes).
$(input1.Customer.Name) The value at that path, to any depth. Strings are inserted without quotes; objects and arrays as JSON.

Placeholders that cannot be resolved are left unchanged. Cyclic object graphs are handled. Child rules are formatted too.