DEV Community

JS Bits Bill
JS Bits Bill

Posted on • Edited on

8 1

Capture global JS errors with window.onerror()

Did you know you can capture and report on global JavaScript errors with window.error? Check it!

// The "error" event fires when an uncaught error is thrown
window.addEventListener('error', function(event) {
  console.log('Captured error:', event);
});
Enter fullscreen mode Exit fullscreen mode

Inside the error object, you'll get all sorts of information including the error message, target element, and even the stack trace.

This is how a lot of error logging platforms capture client-side errors: the event listener can be added globally and the callback can send the error date to a server where you can do all sorts of things with it. Neat! 😎


Check out more #JSBits at my blog, jsbits-yo.com. Or follow me on Twitter!

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay