Giter Club home page Giter Club logo

window.onerror's Introduction

window.onerror

Log client side javascript exceptions on your server. Works in all major browsers.

When our client side javascript is deployed it has to work in a hostile browser environment with network errors, limited memory, malware and browser extensions of poor quality. With automated testing we have come a long way, but considering the unpredictable browser environment, there will be error cases that we cannot possibly predict.

Nevertheless, our users expect everything to work flawlessly and we cannot expect them to provide reliable error reports. As a consequence, we are looking for a method to detect new javascript errors as soon as possible.

So how do we keep track of client side errors?

A simple solution is to attach a javascript error handler to window.onerror.

Note that this does not catch all errors! But it might catch enough. Let us continue this happy-go-lucky approach and allow the error handler to be optimistic, because then we can keep it short and simple. That makes it less intrusive to inline the entire javascript error handler in a html script tag, which is preferable over using script src because that extra http get request might fail.

On the server side we keep a log file with all the error messages we have received from the client side error handler.

We can now use this server side log file to discover when our client side javascript is failing in new and unexpected ways. Keep in mind that this is a happy-go-lucky solution, and that by design we are going to miss some errors.

Classes of errors

There are two classes of client side errors:

  • Errors that we can fix, and
  • Errors we cannot fix

I have collected a list of the last class at https://github.com/tlk/window.onerror-ignore

Get Started

  1. Insert the following before any other script tags in your HTML:

    <script>
    window.onerror = function(m,u,l,c) {
        if (window.XMLHttpRequest) {
            var xhr = new XMLHttpRequest();
            var data = "msg="+encodeURIComponent(m)
                    +"&url="+encodeURIComponent(u)
                    +"&line="+l
                    +"&col="+c
                    +"&href="+encodeURIComponent(window.location.href);
            xhr.open("GET", "/logger.php?"+data, true);
            xhr.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
            xhr.send();
        }
    };
    </script>
  2. Make your server store the data that is being posted to it. This example is written in PHP to illustrate the concept:

    <?php
    
    function getVal($key) {
        return array_key_exists($key, $_GET)
            ? $_GET[$key]
            : '';
    }
    
    function isValidRequest() {
        return getVal('msg')
            && !(getVal('msg') == 'Script error.' && getVal('line') == '0')
            && array_key_exists('HTTP_USER_AGENT', $_SERVER);
    }
    
    if (!isValidRequest()) {
        exit;
    }
    
    $data = array();
    array_push($data,
        time(),
        getVal('msg'),
        getVal('url'),
        getVal('line'),
        getVal('col'),
        getVal('href'),
        $_SERVER['HTTP_USER_AGENT']
    );
    
    $line = json_encode($data) . "\n";
    
    # NOTE: Adjust this path and file permissions
    file_put_contents('/var/log/window.onerror/all.log', $line, FILE_APPEND | LOCK_EX);
    
    ?>
  3. Verify by adding a script error:

    <script>
    alrt("foo");
    </script>

See also

Commercial solutions

window.onerror's People

Contributors

tlk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

krisfremen

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.