How can we return error value from ReflectionFunction? #925
-
Is there a way to return an error from a custom function that returns such as a StringValue? Example: using Microsoft.PowerFx;
using Microsoft.PowerFx.Types;
var config = new PowerFxConfig();
config.AddFunction(new MyFuncFunction());
var engine = new RecalcEngine(config);
var ret = engine.Eval("If(IsError(MyFunc(\"\")), \"Error\", \"Success\")");
public class MyFuncFunction : ReflectionFunction
{
public StringValue Execute(StringValue param)
{
if (param.Value == "")
{
// Failed
// How to return error from here?
}
else
{
// Success
return FormulaValue.New("");
}
}
} Best regards. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Check the CommomErrors class. You can call some of the static methods to get a pre-built error message or you can call As a reminder, it is also possible to create a ErrorValue from an expression: |
Beta Was this translation helpful? Give feedback.
-
FYI - here's an improvement here: #1018 |
Beta Was this translation helpful? Give feedback.
Check the CommomErrors class. You can call some of the static methods to get a pre-built error message or you can call
CommomErrors.CustomError
to build your own custom error message.If none of them work for you, it is still a good source to learn how to build ErrorValue objects.
As a reminder, it is also possible to create a ErrorValue from an expression:
Error({Kind:ErrorKind.InvalidArgument, Message:"This argument is invalid."})