Is it possible to programatically catch JavaScript

2019-02-20 04:34发布

I don't think that this is doable, but wanted to throw it out to the community to confirm my suspicions.

Let's say you're writing a program in another language like Python, PHP, or ASP. This program is intended to build another program written in JavaScript. However, the first program is unfortunately not immune to errors. So, occasionally, the program which builds the JavaScript program does some funky stuff and outputs a syntax error in the JavaScript source. Now some user goes and loads the program and it essentially halts, because the web browser running it can't properly parse the JavaScript. This user is probably not going to be happy. I wouldn't be.

This brings me to my question. Is it possible to write an error handler that would catch these kind of syntax problems allowing the application to fail gracefully?

Here's an example:

    <html>
  <head>
   <script type="text/javascript" charset="utf-8">
    window.onerror = errorHandler;
    function errorHandler(a,b,c) {
     alert('horray!  No wait, Booo!');
    }
    vara jfs;
   </script>
  </head>
  <body>
   Can this be done?
  </body>
 </html>

or

<html>
  <head>
   <script type="text/javascript" charset="utf-8">
    try {
     vara jfs;
    } catch  (e) {
     alert('horray!  No wait, Booo!');
    }
   </script>
  </head>
  <body>
   Can this be done?
  </body>
 </html>

3条回答
再贱就再见
2楼-- · 2019-02-20 04:50

If you are comfortable with the security exposure, you could call eval() on the code in question to detect syntax errors.

function say(x) { ... emit message in some way.... }

var scriptlets = [
    "foof1 = function(a) {if (a == 7) { return \"A-OK\"; } } ",
    "foof2 = function (a) {if argle bargle seventy 7, then this isn't going to work}"
];

function verifyScriptlet(n) {
    var s = scriptlets[n];
    try {
        var x = eval(s);
        say("verified #"+ n +", result: " + (typeof x));
    }
    catch (exc1)
    {
        say("Exception while verifying #"+ n +": " + exc1.message);
    }
}

verifyScriptlet(0);
verifyScriptlet(1);
查看更多
唯我独甜
3楼-- · 2019-02-20 05:03

Generally, a programming language is parsed, and if that succeeds possibly compiled to another form, and if that succeeds then interpreted/executed on hardware or a vm. Just because code passes one phase, doesn't mean it's valid code.

You are asking if code running in the last phase, can be aware of problems encountered in the first part and deal with it.

Some languages offer various hooks into it's own parsing and compiling mechanism. As Cheeso mentioned, eval is Javascript's way of calling into this mechanism.

Rather than wrapping all of your scripts in strings, it might be nicer to leave them in javascript files, and load them into your main program with the following javascript (or similar).

 function load(file){
    var client = new XMLHttpRequest();
    client.open("GET", file, false);
    client.send(null);
    return client.responseText;
  }

  function include(file){
    try {
      eval(load(file));
    } catch (e) {
       alert("problem with file " + file);
    }
  }
查看更多
霸刀☆藐视天下
4楼-- · 2019-02-20 05:15

Google has released their Closure toolset which includes a JavaScript compiler and minimizer. I haven't used it personally but have heard a lot of great things. Also, it apparently helps you locate browser compatibility issues.

I don't think it will help you to perform real-time analysis, but still could be a valuable tool.

查看更多
登录 后发表回答