← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Global Error Handling in JavaScript

javascript ES2017 Intermediate
debt(d7/e3/b3/t7)
d7 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'only careful code review or runtime testing' (d7). The detection_hints list ESLint and Sentry, but missing unhandledrejection listeners or silent promise rejections are not caught by default ESLint rules — they require specialist configuration or a runtime monitoring service like Sentry to observe in production. The absence of a global handler is invisible until real users hit uncaught errors, placing this closer to d7 than d5.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3). The quick_fix is clear: add both window.onerror and window.addEventListener('unhandledrejection') handlers that POST to a PHP endpoint or Sentry. This is a small, localised change — a new error-handling module wired up in one place — but it may touch configuration, endpoint setup, and rate-limiting logic, putting it slightly above a one-liner (e1) at e3.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3). The applies_to scope is web only, and the handler itself is a centralised setup (one file/module). However, once added it affects how all async code and promise chains should be written going forward (correlating with session IDs, rate-limiting reports), imposing a modest ongoing tax on maintainers without being truly cross-cutting.

t7 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'serious trap' (t7). The misconception field explicitly states that developers believe console.error() is sufficient for production error tracking, when in reality it only logs to DevTools and is invisible to server-side monitoring. This contradicts intuitive expectations — console.error looks like error reporting but is not. Combined with the common mistake of missing unhandledrejection for Promises, this is a well-documented but frequently encountered serious trap.

About DEBT scoring →

Also Known As

window.onerror unhandledrejection global error handler error boundary

TL;DR

window.onerror and unhandledrejection catch uncaught errors globally — essential for logging client-side errors back to PHP for monitoring.

Explanation

window.onerror catches synchronous errors. window.addEventListener('unhandledrejection') catches unhandled Promise rejections. Both should log to your PHP error tracking endpoint. Error.cause (ES2022) chains errors. window.reportError() programmatically triggers error handlers. In React: ErrorBoundary components catch render errors. Always include: message, stack, URL, line, user context, and PHP session ID for correlation. Never expose internal paths in production error messages.

Common Misconception

console.error() is sufficient for production error tracking — console.error logs to the browser DevTools only; real monitoring requires sending errors to your PHP backend or a service like Sentry.

Why It Matters

Client-side errors are invisible to PHP logs — without global error handlers you are blind to JavaScript failures affecting real users.

Common Mistakes

  • Only using try/catch locally — missing unhandled Promise rejections
  • Not correlating JS errors with PHP session ID or request ID
  • Sending error reports on every error — should rate-limit and batch

Code Examples

✗ Vulnerable
// Only catches sync errors, misses promise rejections:
window.onerror = (msg) => console.error(msg);
✓ Fixed
// Comprehensive global handler reporting to PHP:
function reportError(error, context = {}) {
    navigator.sendBeacon('/api/errors', JSON.stringify({
        message: error.message,
        stack:   error.stack,
        url:     location.href,
        context: { ...context, sessionId: window.__phpSessionId },
    }));
}

window.onerror = (msg, src, line, col, err) => {
    reportError(err ?? new Error(msg), { src, line, col });
};

window.addEventListener('unhandledrejection', (e) => {
    reportError(e.reason instanceof Error ? e.reason : new Error(e.reason));
});

Added 17 Mar 2026
Edited 22 Mar 2026
Views 23
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings F 0 pings S 3 pings S 1 ping M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 1 ping S 1 ping M 0 pings T 0 pings W 2 pings T 0 pings F 0 pings S 2 pings S 0 pings M 0 pings T 1 ping W 0 pings T 0 pings F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S
No pings yet today
No pings yesterday
Amazonbot 8 Perplexity 3 Unknown AI 3 Ahrefs 2 Google 2 ChatGPT 2 Majestic 1
crawler 18 crawler_json 2 pre-tracking 1
DEV INTEL Tools & Severity
🟠 High ⚙ Fix effort: Medium
⚡ Quick Fix
Add both window.onerror and window.addEventListener('unhandledrejection') handlers that POST errors to a PHP endpoint or Sentry
📦 Applies To
javascript ES2017 web
🔗 Prerequisites
🔍 Detection Hints
No unhandledrejection listener; console.error only without reporting; uncaught promise rejections swallowed silently
Auto-detectable: ✓ Yes eslint sentry
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Medium ✗ Manual fix Fix: Medium Context: Class Tests: Update

✓ schema.org compliant