最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - error params are null in window.onerror in google chrome - Stack Overflow

programmeradmin4浏览0评论

I have html page which causes js error and an global handler for it.

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8">
    <script type="text/javascript" src="js/jquery-3.1.0.min.js"></script>
    <script type="text/javascript">
        $(function () {
            window.onerror = function (errorMsg, url, lineNumber) {
                alert('Error: ' + errorMsg + ' Script: ' + url + ' Line: ' + lineNumber);
            }

            throw new Error("this is error");
        });
    </script>
</head>
<body>
    <div>Test page</div>
</body>
</html>

I receive alert: "Error: Script error. Script: Line: 0". As you can see error info is missing. How can I get this info here like I can see errors in developer tools. Any extensions for chrome are not suitable. I must catch errors with their info in javascript.

I have html page which causes js error and an global handler for it.

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8">
    <script type="text/javascript" src="js/jquery-3.1.0.min.js"></script>
    <script type="text/javascript">
        $(function () {
            window.onerror = function (errorMsg, url, lineNumber) {
                alert('Error: ' + errorMsg + ' Script: ' + url + ' Line: ' + lineNumber);
            }

            throw new Error("this is error");
        });
    </script>
</head>
<body>
    <div>Test page</div>
</body>
</html>

I receive alert: "Error: Script error. Script: Line: 0". As you can see error info is missing. How can I get this info here like I can see errors in developer tools. Any extensions for chrome are not suitable. I must catch errors with their info in javascript.

Share Improve this question asked Jul 21, 2017 at 13:37 Anton23Anton23 2,1295 gold badges17 silver badges30 bronze badges 1
  • I created a stack snippet with your handler and it displayed the source (url) correctly. I wonder if it's got to do with cross-origin scripts as explained in the notes here – James Commented Jul 21, 2017 at 13:52
Add a ment  | 

5 Answers 5

Reset to default 6

When a syntax(?) error occurs in a script, loaded from a different origin, the details of the syntax error are not reported to prevent leaking information (see bug 363897). Instead the error reported is simply "Script error." This behavior can be overriden in some browsers using the crossorigin attribute on and having the server send the appropriate CORS HTTP response headers. A workaround is to isolate "Script error." and handle it knowing that the error detail is only viewable in the browser console and not accessible via JavaScript.

I also ran into this. See notes section here: https://developer.mozilla/en-US/docs/Web/API/GlobalEventHandlers/onerror

This implies the development setup might need some adjustment to avoid this policy issue. I'm merely guessing here, so don't take it for granted.

I am just adding answer for the sake of those who came here looking.

Source: https://developer.mozilla/en-US/docs/Web/API/GlobalEventHandlers/onerror

Please go this URL, as you can see there are five parameters to

window.onerror = function(message, source, lineno, colno, error) { ... }

the last one is the one you need, it has plete record of what you see in console in case of error.

message: error message (string). Available as event (sic!) in HTML onerror="" handler.
source: URL of the script where the error was raised (string)
lineno: Line number where error was raised (number)
colno: Column number for the line where the error occurred (number)
error: Error Object (object)

Then Error Object has two properties

message
stack

stack is the one which will contain all the information you required.

use console.log(errorMsg, url, lineNumber)instead of alert to get more readable log. You can see the log in browser developer console. Also you can check the use of try..catch for better error handling.

use jquery script with anonymous attirbute, and the script server response header with [access-control-allow-origin:*]. Your error throw code is runned with jquery callback.

errorMsg is NOT the message "this is error"; it is Script error., which is precisely what you're seeing. You simply don't get the error message that way. onerror is unsuited to replace a try-catch block.

发布评论

评论列表(0)

  1. 暂无评论