Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome truncates error messages and adds "<omitted>" in window.onerror

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>
like image 952
AndyS Avatar asked Mar 05 '14 13:03

AndyS


2 Answers

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:

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

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>

like image 138
AndyS Avatar answered Nov 20 '22 15:11

AndyS


This is a known problem in Chrome and has already been noticed by other people: https://github.com/angular/angular.js/issues/5568.

I don't think there is a workaround, but the bug is already reported: http://crbug.com/331971

like image 3
Tom Leese Avatar answered Nov 20 '22 13:11

Tom Leese