How to detect if a ScriptObject
is a v8 Error
?
#564
-
I'm trying to implement a simple console and like to get more details when a Error object is passed. When i run that in something like public class JsConsole
{
public void log(params object[] args)
{
foreach (var arg in args)
{
if (arg is ScriptObject o)
{
// This only gives error message without stack when underlying exception comes from dotnet method call.
Console.WriteLine(o.InvokeMethod("toString"));
} else
{
Console.WriteLine(arg?.ToString());
}
}
}
} So what's the best way to detect if the |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
A hacky way is this: if ((o.GetProperty("constructor") as ScriptObject)?.PropertyNames.Contains("stackTraceLimit") == true)
{
writer.Append(o.GetProperty("stack"));
} else
{
writer.Append(o.InvokeMethod("toString"));
} |
Beta Was this translation helpful? Give feedback.
-
Hi @viceice, Why not let the script engine help? Consider: dynamic isInstanceOf = engine.Evaluate("(x, y) => x instanceof y"); And then: if (isInstanceOf(obj, engine.Script.Error)) {
Console.WriteLine("The object is a JavaScript Error!");
} Good luck! |
Beta Was this translation helpful? Give feedback.
Hi @viceice,
Why not let the script engine help? Consider:
And then:
Good luck!