Files
11ty-blog/content/blog/Capturing-Client-Side-JavaScript-Errors/Capturing-Client-Side-JavaScript-Errors.md
Tommy Parnell 53074f3174 stop
2023-11-04 15:17:48 -04:00

1.2 KiB

title, tags, permalink, id, updated, date
title tags permalink id updated date
Capturing Client Side JavaScript Errors
javascript
client side
error handling
/capturing-client-side-javascript-errors/ 24 2014-03-19 20:05:33 2014-03-19T23:57:16.000Z

Capturing client side errors in my opinion is really good. For starters you can troubleshoot your client side implementation, but you can also make sure a js change did not break certain pages.

Below is a really simple, yet effective way to capture errors. Eventually you may want to implement something more advanced, but this will get you out of the gate.

window.onerror = function (errorMsg, url, lineNumber, column, error) {
    $.ajax('/api/Error', {
        type: "POST",
        data: {
            Message: errorMsg,
            ScriptUrl: url,
            Line: lineNumber,
            PageUrl: window.location.protocol + "//" + window.location.host + "/" + window.location.pathname,
            StackTrace: function (){return error ? error.stack: '';}

        }
    });
};

You will need Jquery, and a server side API to accept the data. Not all browsers are currently including a Stack Trace, so you will only get stacks from certain browsers.