codingseb / ExpressionEvaluator

A Simple Math and Pseudo C# Expression Evaluator in One C# File. Can also execute small C# like scripts
MIT License
595 stars 99 forks source link

Error message - Exception has been thrown by the target of an invocation. #138

Closed iisfaq closed 2 years ago

iisfaq commented 2 years ago

I was just testing your cool parser and came across the following

"Chris"[35]

This returns Exception has been thrown by the target of an invocation. - not a very friendly message.

I tracked it down to the following line


 2926                        else if (itemProperties?.Length > 0)
*2927                            return itemProperties[0].GetValue(left, oIndexingArgs.Cast<object>().ToArray()); 
 2928                        else

I changed the code to this

                            try
                            {
                                return itemProperties[0].GetValue(left, oIndexingArgs.Cast<object>().ToArray());
                            }
                            catch(Exception ex)
                            {
                                throw new Exception($"Invalid index speciifed '{string.Join(",",oIndexingArgs.Cast<object>().ToArray())}' for '{left}'");
                            }

This results in something like this

Invalid index specified '35' for 'Chris'

iisfaq commented 2 years ago

I was going to create a pull request but I don't think I have the rights to do that. I have not done this before.

Chris

codingseb commented 2 years ago

Hello @iisfaq thanks for the issue. Yes of course you can submit pull request. I just need to accept it to integrate your code in the main repo. (you need to fork the repo first to do you modifications and create a pull request from your repo.)

But for this issue I think I will implement a more generic solution to throw better exceptions in general with the use of ExceptionDispatchInfo as suggested by @jl0pd in #124.

codingseb commented 2 years ago

I just published version 1.4.8.0 (on nuget) that should throw better exceptions in this kind of case.