Guides / Capture console errors

How to capture console errors from your users' browsers

To capture console errors from your users' browsers, a script on your page must record them as they happen. Otherwise the browser shows them only in the user's own developer tools. Tellfix does this for you: its widget keeps the last 20 console errors and uncaught exceptions and attaches them to every bug report.

This guide shows how to do it yourself first, what that approach misses, and then what Tellfix captures.

Why you can't see your users' console errors

Console errors live in the browser of the user who hit them. Nothing sends them to you, and most users never open the developer tools. Asking a user to open the console, find the red lines and paste them into an email rarely works. By then they may have reloaded the page and lost them.

So the page has to collect them itself, before the user reports anything.

Capture them yourself with window.onerror and console.error

Capturing console errors without a tool takes three hooks, set up as early as your page allows. Browsers report errors in three ways: calls to console.error, uncaught exceptions (the error event on window, also handled by window.onerror), and promises rejected with no handler (the unhandledrejection event). Keep the latest few in a list and send them with the report:

errors.js
const errors = []

function keep(message) {
  errors.push(message)
  if (errors.length > 20) errors.shift()
}

// 1. console.error calls: log as before, then keep a copy.
const originalError = console.error
console.error = (...args) => {
  originalError.apply(console, args)
  keep(args.map((a) => (a instanceof Error ? a.stack ?? a.message : String(a))).join(" "))
}

// 2. Uncaught exceptions.
window.addEventListener("error", (event) => keep(event.message))

// 3. Promises rejected with no handler.
window.addEventListener("unhandledrejection", (event) => {
  keep(`Unhandled rejection: ${event.reason?.stack ?? event.reason}`)
})

// Send errors.slice() with the user's report.

Call the original console.error first so the developer tools still show every error. Keep the list short so a page that logs in a loop doesn't fill memory.

What capturing console errors this way misses

Capturing console errors in the page has limits, whoever writes the code:

  • Errors thrown before the script runs are gone. Load it early if those matter.
  • An error from a script on another origin reaches window.onerror as just Script error., unless the server sends CORS headers for it and you load it with the crossorigin attribute.
  • Stack traces point into your minified bundle. You need your source maps to read them.
  • The errors alone don't say what the user saw. You still need a screenshot, the page, the browser and a place to send it all.

How Tellfix captures console errors

Tellfix starts capturing console errors as soon as its widget loads. It wraps console.error, calling the original first so your logs are unchanged, and listens for the error and unhandledrejection events. It keeps the last 20 in the page's memory and sends nothing until a user sends a bug report.

<script src="https://app.tellfix.app/widget.js" data-project="pk_your_project_key" data-endpoint="https://app.tellfix.app/ingest"></script>

An Error passed to console.error, or a promise rejected with one, comes with its stack trace. An uncaught exception comes with its message. Other values logged with an error are kept as text. Feature requests carry no console errors.

The same limits apply to Tellfix: it can't catch errors thrown before the widget loads, or from cross-origin scripts without CORS.

Where the console errors show up

Console errors appear on each Tellfix report in the dashboard, beside the screenshot, the page and the browser. They also go wherever the report is filed:

Turn off or scrub console error capture

When you install Tellfix as a library, you can turn off console error capture or scrub the errors before each report leaves the browser. Errors can hold personal data your app logged, such as an email address in a failed request. consoleErrors: false stops capture, and beforeSend can change or empty the list:

import { tellfixTransport } from "@ganeshdole/tellfix-core"
import { mount } from "@ganeshdole/tellfix-widget"

mount({
  transport: tellfixTransport({
    endpoint: "https://app.tellfix.app/ingest",
    projectKey: "pk_your_project_key",
  }),
  beforeSend: (report) => ({
    ...report,
    metadata: {
      ...report.metadata,
      consoleErrors: report.metadata.consoleErrors.map((e) => e.replace(/\S+@\S+/g, "[email]")),
    },
  }),
})

The script tag has no option for this: it always captures them.

Console errors in reports, or every error

Tellfix attaches console errors to the bug a user reports. It does not collect errors from sessions where nobody sends a report. To see every error from every user, use an error monitoring tool as well. The comparison with Sentry User Feedback covers how the two differ.

Try it before you install it

This page runs the real widget. Report something on it and it lands in our inbox, the way your users’ reports will land in yours.

Get started