zzzprojects / Eval-Expression.NET

C# Eval Expression | Evaluate, Compile, and Execute C# code and expression at runtime.
https://eval-expression.net/
Other
449 stars 86 forks source link

NullReferenceException not very descriptive when parameter is not provided #78

Closed michaelsudnik closed 3 years ago

michaelsudnik commented 4 years ago

Hi,

When running the following code, a NullReferenceException exception is thrown which is not very descriptive.

Dictionary<string, object> parameters = new Dictionary<string, object>();       
var result = Eval.Execute("Param", parameters);

https://dotnetfiddle.net/UcGMqF

Could it throw a more useful error message?

Or is there a way to validate which parameters an expression is expecting?

Thanks,

Mike

JonathanMagnan commented 4 years ago

Thank you for reporting,

We will look if something can be done about the error message.

Best Regards,

Jon

JonathanMagnan commented 4 years ago

Hello @michaelsudnik ,

The v4.0.3 has been released which improves the error message.

There is some way to know when a value is missed such as the VariableFactory:

Dictionary<string, object> parameters = new Dictionary<string, object>();

var context = new EvalContext();
context.VariableFactory = argument =>
{
    if (argument.Name == "Param")
    {
        argument.Value = 3;
        argument.IsHandled = true;
    }
};
var result = context.Execute("Param", parameters);

Another way is by using ExpandoObject and letting you know which variable is missing:

var context = new EvalContext();
var result = context.Compile<Func<ExpandoObject, int>>("Param");

var missingVariables = context.DynamicMemberNames;

Let me know if that answer your question.

michaelsudnik commented 4 years ago

Hi,

I appreciate the update - although the new message text is a little informal. There is also an additional "." after "expected".

Oops! A null expression has been found. A not null statement was expected.. The error occurred for expression "Param" at position 0 near "Param".

Would it be possible to throw a custom exception, with relevant parameters ("position", "near") set, so that I can better handle the error and inform the user better (e.g. with a translated message)? Otherwise, my only option would be to parse the message to get the relevant information.

Best regards,

Mike

JonathanMagnan commented 4 years ago

Sure, we will look at it.

JonathanMagnan commented 4 years ago

Hello @michaelsudnik ,

Just to confirm that we are currently creating an EvalException that will provide all necessary information.

It should be released this week.

JonathanMagnan commented 4 years ago

Hello @michaelsudnik ,

The v4.0.8 has been released.

Most exception now throw an EvalException

public class EvalException : Exception
{   
    /// <summary>Gets or sets the original code.</summary>
    /// <value>The original code.</value>
    public string OriginalCode { get; set; }

    /// <summary>Gets or sets the parsed code.</summary>
    /// <value>The parsed code.</value>
    public string ParsedCode { get; set; }

    /// <summary>Gets or sets the token.</summary>
    /// <value>The token.</value>
    public string Token { get; set; }

    /// <summary>Gets or sets the start position of the exception (from the parsed code).</summary>
    /// <value>The start position (from the parsed code).</value>
    public int StartPosition { get; set; }

    /// <summary>Gets or sets the near text (from the parsed code).</summary>
    /// <value>The near text (from the parsed code).</value>
    public string NearText { get; set; }
}

Let me know if that's what you were looking for.

Best Regards,

Jon

michaelsudnik commented 4 years ago

Hi Jon,

Sorry for not replying earlier about this sooner and we appreciate the work done so far!

We could really do with a "Reason" property displaying the reason for the error so that we can then format it appropriately. For example, it could simply be "A null expression has been found."

Even better would be if you could report an Error Code (or enum) which corresponds to the Reason or type of exception so that we could translate these Error Codes into user-friendly messages in specific languages and know which properties of the exception would be relevant for our messages.

We are still also receiving a NullReference exception in this case: https://dotnetfiddle.net/DYO6kR

Best regards,

Mike

JonathanMagnan commented 4 years ago

Hello @michaelsudnik ,

We will some code a little bit everywhere to make sure the expression is not null (or throw a specific error in this case).

We will also check how we can improve those error messages.

Best Regards,

Jon

JonathanMagnan commented 3 years ago

Hello @michaelsudnik ,

The v4.0.20 has been released.

We have improved the error handling at several places. My developer will continue to look at it to improve some other part of the code to better handle this exception.

Best Regards,

Jon

JonathanMagnan commented 3 years ago

Hello @michaelsudnik ,

We added more null exception handling in the v4.0.24,

For now, we will close it as I believe we added enough check until some people report us this error again.

Best Regards,

Jon