Empty message property on JavaScript Error Object when catch exception raised from .Net method

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ToRemObjects
{
public class TestClass
{
double m;

    public double testProperty
    {
        get
        {
            return m;
        }
        set
        {
            m = value;
        }
    }
}

public class RemObjectsConsole
{
    Dictionary<object, object> bag = new Dictionary<object, object>();

    public RemObjectsConsole()
    {
    }

    public double propDouble
    {
        get { return 1.2D; }
        set { Console.WriteLine("propDouble:{0}", value); }
    }
    public double getDouble()
    {
        return 1.3D;
    }
    public void writeDouble(double value)
    {
        Console.WriteLine("writeDouble:{0}", value);
    }
    public double getDateAsDouble()
    {
        return 1357908756.0D;
    }
    public int getDateAsInt()
    {
        return 1357908756;
    }

    public int propInt
    {
        get { return 150; }
        set { Console.WriteLine("propInt:{0}", value); }
    }
    public int getInt()
    {
        return 150;
    }
    public void writeInt(int value)
    {
        Console.WriteLine("writeInt:{0}", value);
    }

    public string propString
    {
        get { return "string.value"; }
        set { Console.WriteLine("propString:{0}", (value == null ? "<<null>>" : value)); }
    }
    public void writeString(string value)
    {
        Console.WriteLine("writeString:{0}", (value == null ? "<<null>>" : value));
    }
    public string getString()
    {
        return "My get string...";
    }

    public bool propBoolean
    {
        get { return true; }
        set { Console.WriteLine("propBoolean:{0}", value); }
    }
    public void writeBoolean(bool value)
    {
        Console.WriteLine("writeBoolean:{0}", value);
    }
    public bool getBoolean()
    {
        return true;
    }

    public DateTime propDate
    {
        get { return new DateTime(1974, 1, 1); }
        set { Console.WriteLine("propDate:{0}", value); }
    }
    public void writeDate(DateTime value)
    {
        Console.WriteLine("writeDate:{0}", value);
    }
    public DateTime getDate()
    {
        return new DateTime(1974, 1, 1);
    }

    public object propObject
    {
        get { return this; }
        set { Console.WriteLine("propObject:{0}:{1}", (null == value ? "<<null>>" : value.GetType().Name), (null == value ? "<<null>>" : value)); }
    }

    public void writeObject(object value)
    {
        Console.WriteLine("writeObject:{0}:{1}", (null == value ? "<<null>>" : value.GetType().Name), (null == value ? "<<null>>" : value));
    }

    public override string ToString()
    {
        return "My Console.ToString to string ...";
    }

    public void load(string commandText, params object[] parameters)
    {
        // do somting...
        int i = 0;
    }

    public void load2(string commandText, object[] parameters)
    {
        // do somting...
        int i = 0;
    }

    public void loadStrings(params string[] parameters)
    {
        // do somting...
        int i = 0;
    }
    public void loadDoubles(params double[] parameters)
    {
        // do somting...
        int i = 0;
    }
    public void loadStrings2(string[] parameters)
    {
        // do somting...
        int i = 0;
    }

    public object this[string name]
    {
        get
        {
            object value;
            bag.TryGetValue(name, out value);
            return value;
        }
        set
        {
            bag[name] = value;
        }
    }

    public object this[int index]
    {
        get
        {
            object value;
            bag.TryGetValue(index.ToString(), out value);
            return value;
        }
        set
        {
            bag[index.ToString()] = value;
        }
    }

    public void ThrowException()
    {
        throw new ApplicationException("test exception ....");
    }
}

class Program
{
    static void Main(string[] args)
    {
        using (var esc = new RemObjects.Script.EcmaScriptComponent())
        {
            esc.Include("test1", @"

function testSomeOthers(cc) {
try
{
cc.ThrowException();
throw new Error(‘test error…’);
}
catch(e)
{
cc.writeObject(e.message);//is empty???, when .Net exception no message property exists, may be is need native .Net exception
// to be wraped into JavaScript Error object with additional property NativeError
cc.writeObject(e);
}
finally
{
cc.writeObject(‘catch finally…’);
}
}
");

            RemObjectsConsole cc = new RemObjectsConsole();
            var o = esc.RunFunction("testSomeOthers", cc);
            Console.ReadLine();
        }
    }

}

}

Hello

Thanks again for perfect testcases.
Fix has been uploaded to GitHub.
At Monday we’ll put the built installer in your personal downloads folder.

Thanks for follow-ups

Hello
I grab source from GitHub and make tests,
but this don’t work, message continue to be empty,
I will test it again at Monday,

I posted issue about evaluating script, which not create variables into
GlobalContext, what is going on with it (Corresponding issue is registered as #60328.),
I have some legacy scripts which share state like that,
It is important to me, this will allow me going on

Hello

Sorry, the exception issue fix was partial. Pushed the full fix and tests for it to the GitHub

Thanks

Now in this case all is right,

But now I am using GlobalObject.NotStrictGlobalEval,
how ck said here

http://connect.remobjects.com/discussion/comment/10699/#Comment_10699

I can’t access real Exception with EcmaScriptComponent.RunException
and also can’t determinate where is real SyntaxError

CS Code:
using RemObjects.Script;
using RemObjects.Script.EcmaScript;
using System;
using System.Reflection;
namespace ToRemObjects
{
public class MyException : ApplicationException
{
public MyException()
: base(“My exception message text …”)
{ }
public int Code
{
get
{
return -100;
}
}
}
public class TestClass
{
public void DoSomething()
{
throw new MyException();
}
}
class Program
{
static object Eval(EcmaScriptComponent esc, string code)
{
return esc.GlobalObject.NotStrictGlobalEval(esc.GlobalObject.ExecutionContext, esc.GlobalObject, code);
//return esc.GlobalObject.InnerEval(esc.GlobalObject.ExecutionContext, false, esc.GlobalObject, code);
}

    static void Main(string[] args)
    {

string script = @“
gVar.DoSomething();
”;

string script2 = @“
function aa() {
var i=0;
var z=100;
if (i,z) {
10*10;
}
else {
100;
}
}
”;

        using (var esc = new RemObjects.Script.EcmaScriptComponent())
        {
            esc.Globals.SetVariable("gVar", new TestClass());
            try
            {
                Eval(esc, script); // empty RunException ???
                //esc.RunFunction("eval", script); // null!=RunException
                esc.Include("aaa", script); // empty RunException ???
                Eval(esc, script2); // Exception: SyntaxError: Syntax error??? can't determine easily where problem is
                esc.RunFunction("eval", script2); // Exception: SyntaxError: Syntax error??? can't determine easily where problem is
                // esc.Include("aaa", script2); // Exception: aaa(5:11) E11 Syntax error
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: {0}", null == ex ? "<<null>>" : ex.ToString());
                Console.WriteLine("InnerException: {0}", null == ex.InnerException ? "<<null>>" : ex.InnerException.ToString());
                Console.WriteLine("RunException: {0}", null == esc.RunException ? "<<null>>" : esc.RunException.ToString());
            }
            Console.ReadLine();
        }
    }
}

}

Hello.

Corresponding issue is registered as #60480.

Thanks.

So exceptions generally don’t leave a place to hold position info. In train (our script based build engine) we use the debugging events to store the last tracepoint position and use that for exceptions.

The exceptions Script throws is:

  • ScriptRuntimeException < occurs when something uses “throw” (or a javascript function uses throw). The real exception object (As in javascript Error) is in the Original property.
  • ScriptComponentException < This is when running a non-existent function

Yes, I also found ScriptRuntimeException.Original,

For exceptions: I eval scripts out of the box(NotStrictEval,InnerEval), how to use debug events for something, which can’t be parsed

Eval currently doesn’t support line information