Quantcast
Channel: ClearScript
Viewing all articles
Browse latest Browse all 2297

New Post: Returning syntax error location

0
0
Hi,

well it seems the problem is not as easy to describe as I hoped so I have build a small example:
public class Program
{
    static void Main(string[] args)
    {
        try
        {
            var host = new ScriptHost();
            dynamic result = host.LoadModule();
            Console.WriteLine(result.content);
        }
        catch (Exception ex)
        {
            // (0)
            throw;
        }
    }
}

public class ScriptHost
{
    private V8ScriptEngine _engine = new V8ScriptEngine("MyEngine");
    private dynamic _moduleLoader;

    public ScriptHost()
    {
        var moduleLoaderSource = @"(function loadModule(host, name, source) { return host.Run(name, source); })";
        _moduleLoader = _engine.Evaluate("module_loader.js", moduleLoaderSource);
    }

    public object LoadModule()
    {
        var someModuleSource = @"({ content: 'There is a syntax error in this module.'§ })";
        return _moduleLoader(this, "some_module.js", someModuleSource);
    }

    public object Run(string name, string source)
    {
        try
        {
            return _engine.Evaluate(name, source);
        }
        catch (Exception ex)
        {
            // (1)
            throw;
        }
    }
}
Now let's look at the ErrorDetail property of the two exceptions (1) and (2):
(1)
"SyntaxError: Invalid or unexpected token
    at loadModule (module_loader.js [temp]:1:56) -> ({ content: 'There is a syntax error in this module.'§ })"
The stacktrace looks confusing here. While the given source code is indeed the syntax error, it claims that this line is in module_loader.js [temp]:1:56 which is wrong.
(2)
"Error: SyntaxError: Invalid or unexpected token
    at loadModule (module_loader.js [temp]:1:56) -> (function loadModule(host, name, source) { return host.Run(name, source); })"
Ok, now the given source code corresponds to the location. However, that only means that now both are wrong. Much less problematic but also strange: The new "Error:" prefix that was not part of the messsage at (1).

Ideally I would expect a stacktrace like this:
"SyntaxError: Invalid or unexpected token
    at (some_module.js [temp]:1:53) -> ({ content: 'There is a syntax error in this module.'§ })"
    at loadModule (module_loader.js [temp]:1:56)"

Viewing all articles
Browse latest Browse all 2297