Chrome会截断错误消息并添加“<省略>”"在window.onerror [英] Chrome truncates error messages and adds "<omitted>" in window.onerror

查看:119
本文介绍了Chrome会截断错误消息并添加“<省略>”"在window.onerror的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用window.onerror来捕获未处理的异常(为开发团队记录它们,并显示友好的用户警报)。最近我们注意到,在谷歌浏览器中,如果错误消息超过了一定的长度,则截断该错误消息,并且文本 ...<省略> ... 为神秘地添加到错误消息中。



下面的代码将演示这一点(在Chrome版本33.0.1750中)。我想知道是否有其他人有这个问题吗?

 < html> 
< head>
< script type =text / javascript>

window.onerror = function(errorMsg,url,lineNumber){
alert('Error:'+ errorMsg);
}
$ b $ var throwError = function(){
抛出新错误(
'出错了,出错了,出错了,出错了。 +
'出错了,出错了,出了点问题,出了点问题'+
'出了问题,出错了,出了点问题,出了点问题'+
'一些文本在这个点之前被截断!');
}

< / script>
< / head>
< body>
< h1> Hello World< / h1>
< input type =buttononclick =throwError()value =抛出未被捕获的错误! />< br />< br />
< / body>
< / html>


解决方案

我发现了一个很好的解决方案。



Chrome实现了新的HTML标准,其中2个新参数已添加到 onerror 处理程序中,即: colNumber 错误对象(包括堆栈跟踪)。



请参阅:


  • https://mikewest.org/2013/08/debugging -runtime-errors-with-window-onerror

  • HTML Living Standard
    完整的错误消息可以通过 error.message 按照我下面的代码示例。这是第一个参数 message 的重复,看起来Chrome似乎决定截断 message 。不知道为什么他们不得不打破那些正在工作的东西...: - (



    IE和FireFox(截至我目前的版本:11.0.9600和26.0)都是还没有实现新的5参数标准,所以代码考虑到了这一点。



    希望这可以帮助别人!



    pre $ < html>< html>
    代码示例返回完整的错误信息: < head>
    < script type =text / javascript>

    // Chrome传递我们必须使用的错误对象(第5个参数),因为它现在会截断Msg第一个参数)
    window.onerror = function(errorMsg,url,lineNumber,columnNumber,errorObject){
    var errMsg;
    //检查errorObject为IE和FF不通过通过(还)
    if(errorObject&&&& errorObject!== undefined){
    errMsg = errorObject.message;
    }
    else {
    errMsg = errorMsg ;
    }
    alert('Error:'+ errMsg);
    }
    $ b $ var throwError = function(){
    抛出新错误(
    '出错了,出错了,出错了,出错了。 +
    '出错了,出错了,出了点问题,出了点问题'+
    '出了问题,出错了,出了点问题,出了点问题'+
    '文本不会被截断!:-)');
    }

    < / script>
    < / head>
    < body>
    < h1> Hello World< / h1>
    < input type =buttononclick =throwError()value =抛出未被捕获的错误! />< br />< br />
    < / body>


    We use window.onerror to catch unhandled exceptions (to log them for the dev team, and display a friendly user alert). Recently we noticed that in Google Chrome, the error message got truncated if it was above a certain length, and the text "...<omitted>..." was mysteriously added to the error message.

    The code below will demonstrate this (in Chrome ver 33.0.1750). I was wondering if anyone else has had this problem?

    <html>
        <head>
            <script type="text/javascript">
    
            window.onerror = function (errorMsg, url, lineNumber) {
                alert('Error: ' + errorMsg);
            }
    
            var throwError = function () {
                throw new Error(
                'Something went wrong. Something went wrong. Something went wrong. Something went wrong. ' +
                'Something went wrong. Something went wrong. Something went wrong. Something went wrong. ' + 
                'Something went wrong. Something went wrong. Something went wrong. Something went wrong. ' + 
                'Some text gets truncated before this point!');
            }
    
            </script>
        </head>
        <body>
            <h1>Hello World</h1>
            <input type="button" onclick="throwError()" value="Throw uncaught error!" /><br /><br />
        </body>
    </html>
    

    解决方案

    I found a solution that works well.

    Chrome implements the new HTML standard, in which 2 new parameters have been added to the onerror handler, namely: the colNumber, and the error object (including stack trace).

    See:

    The full error message can be accessed through error.message as per my code sample below. This is a duplicate of the 1st parameter message, it seems like Chrome has decided to truncate message. Not sure why they had to break something that was working... :-(

    IE and FireFox (as of my current versions: 11.0.9600 & 26.0) are not yet implementing the new 5-parameter standard, so the code makes allowance for that.

    Hope this helps someone!

    Code sample to return full error message:

    <html>
    <head>
        <script type="text/javascript">
    
        //Chrome passes the error object (5th param) which we must use since it now truncates the Msg (1st param).
        window.onerror = function (errorMsg, url, lineNumber, columnNumber, errorObject) {
            var errMsg;
            //check the errorObject as IE and FF don't pass it through (yet)
            if (errorObject && errorObject !== undefined) {
                    errMsg = errorObject.message;
                }
                else {
                    errMsg = errorMsg;
                }
            alert('Error: ' + errMsg);
        }
    
        var throwError = function () {
            throw new Error(
            'Something went wrong. Something went wrong. Something went wrong. Something went wrong. ' +
            'Something went wrong. Something went wrong. Something went wrong. Something went wrong. ' + 
            'Something went wrong. Something went wrong. Something went wrong. Something went wrong. ' + 
            'Text does not get truncated! :-)');
        }
    
        </script>
    </head>
    <body>
        <h1>Hello World</h1>
        <input type="button" onclick="throwError()" value="Throw uncaught error!" /><br /><br />
    </body>
    

    这篇关于Chrome会截断错误消息并添加“&lt;省略&gt;”&quot;在window.onerror的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆