How can get error text in Edgejs in c# when run a JS function?

98 Views Asked by At

I want run some JavaScript file in c# platform in a console application, for example:

using System;
using System.Threading.Tasks;
using EdgeJs;

class Program
{
    public static async Task Start()
    {
        var func = Edge.Func(@"
            return function (data, callback) {
                var hello = function() { 
                      // some javascript syntax error ... 
                      return ' Hello ' + data;
                    };
                callback(null, hello());
             }
         ");

        Console.WriteLine(await func("Word!"));
    }

    static void Main(string[] args)
    {
        Start().Wait();
    }
}

But when my js function has a error it does not give me any error text or hint to detect my errors .

So how can i get error text in Edgejs in c# when run a JS function ?

1

There are 1 best solutions below

0
On

One approach can be this:

 using System;
 using System.Threading.Tasks;
 using EdgeJs;

  class Program
  {
    public static async Task Start()
   {
     var func = Edge.Func(@"
     return function (data, callback) {
     var error = '';
     var hello = function() { 
     try{
             some javascript syntax error .... 
             return 'Hello ' + data;
     }catch(e){ error = e;}
             };
     var result = hello();
     if(error == '')
     {
          callback(null, result );
     } else { 
               callback(null, error);
            }
     }
    ");

     Console.WriteLine(await func("Word!"));
   }

  static void Main(string[] args)
  {
      Start().Wait();
  }
}

I mean using try catch in my js code but if could be other way without changing js code, it was excellent.