Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display a prominent error message whenever a JavaScript error occurs (for development use)

Whenever my JavaScript dies while logging an error message to the Console, I’d like to be told about this immediately. The console is too invisible and often hidden or obscured by other windows.

Can I have something as prominent as a .NET exception dialog? To me these JavaScript errors are completely fatal; they aren’t something that can be ignored, and I’d like to know about them before I spend a while wondering why something doesn’t happen.

Are there addons that do this for Firefox or Chrome?

like image 319
Roman Starkov Avatar asked Mar 06 '12 15:03

Roman Starkov


1 Answers

I think window.onerror handler will provide you such a functionality where you can alert the erorr, url and the line number,

DEMO

Note: Make sure window.onerror function is inside a separate script tag like below. Any error logged in error console will be alerted.

<script>
    window.onerror = function(msg, url, lineNo)  {
       alert(msg + '\n' + url + '\n Line No: ' + lineNo);
    }
</script>

<script>
    document.getElementById('test').asd = 123; //will throw an error
</script>

<script>   
    var s = [{]};
</script>

<script>
    throw "Custom Error";
</script>
like image 155
Selvakumar Arumugam Avatar answered Nov 15 '22 01:11

Selvakumar Arumugam