Home

windowonerror

Window.onerror is a global error handler provided by web browsers. It allows a web page to respond to uncaught JavaScript errors by assigning a function to window.onerror. This mechanism is long-standing and widely supported, though modern monitoring often uses additional techniques.

The handler is invoked when an unhandled exception occurs and is passed five arguments: message, source, lineno,

Common usage is to log errors to a server, report telemetry, or present a graceful message to

window.onerror = function (message, source, lineno, colno, error) {

fetch('/log', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ message, source, lineno, colno, stack: error && error.stack

return true;

};

Window.onerror is complemented by other events such as the error event (window.addEventListener('error', ...)) and the unhandledrejection event

Limitations include that not all errors are delivered in all browsers, and certain errors may be swallowed

colno,
and
an
error
object
(where
supported).
Returning
true
from
the
handler
may
suppress
the
browser's
normal
error
output
in
some
environments,
but
behavior
is
not
guaranteed
across
all
browsers.
users.
Example:
})
});
for
promises.
These
can
be
used
in
concert
to
capture
resource-loading
failures
and
unhandled
promise
rejections.
due
to
security
or
cross-origin
constraints.
It
is
best
used
as
a
supplement
to
explicit
try-catch
blocks
and
dedicated
monitoring
solutions,
rather
than
as
the
sole
error-handling
mechanism.