Web-Tracing ignoreErrors Option: How to Filter Specific Errors in JavaScript

The ignoreErrors option is a configuration array that silently drops unwanted error messages—using exact string matches or regular expressions—before they are transmitted to your monitoring backend.

The web-tracing library provides granular control over error collection through its ignoreErrors configuration option. This feature allows developers to suppress specific, non-actionable errors—such as third-party script failures or browser extension exceptions—while maintaining full visibility into critical runtime issues. Understanding how to leverage this filtering mechanism ensures cleaner error logs and reduces noise in your monitoring dashboard.

What Is the ignoreErrors Option?

The ignoreErrors option is defined in packages/core/src/lib/options.ts as an optional array that accepts strings or RegExp objects. When initialized, this array is empty by default (ignoreErrors = [] // 错误类型事件过滤), meaning no errors are filtered unless explicitly configured.

During error capture, the SDK invokes the isIgnoreErrors() function located in packages/core/src/lib/err.ts to evaluate each error against your configured patterns. If the error message matches any entry in the array, the SDK aborts processing, logs a debug message (ignoreErrors拦截成功), and prevents the error event from emitting entirely.

Configuring the ignoreErrors Option

You can enable error filtering during SDK initialization by passing an array of patterns to the init function. This configuration works across all supported frameworks including vanilla JavaScript, Vue, and React implementations.

Basic Configuration Example

import { init } from 'web-tracing'

init({
  dsn: 'https://example.com/collect',
  appName: 'my-app',
  error: true,               // Enable error collection
  ignoreErrors: [/^ignore/, 'Specific known error']
})

Source: [packages/core/__test__/err.spec.ts](https://github.com/m-cheng-web/web-tracing/blob/main/packages/core/__test__/err.spec.ts)

Real-World Usage in Vanilla JavaScript

import WebTracing from 'web-tracing'

WebTracing.init({
  dsn: 'https://example.com/collect',
  appName: 'demo',
  error: true,
  // Filter out specific known patterns
  ignoreErrors: ['111', /^promise/, /.*split is not .* function/],
})

Source: [examples/vanilla/main.ts](https://github.com/m-cheng-web/web-tracing/blob/main/examples/vanilla/main.ts)

Internal Implementation Details

The filtering logic resides in the isIgnoreErrors() function within packages/core/src/lib/err.ts. This function performs a sequential check against all configured patterns with the following behavior:

  1. Early Return: If ignoreErrors is empty, the function immediately returns false and allows error processing to continue.
  2. Message Extraction: The function extracts the error message from either error.errMessage or error.message, converting the result to a string.
  3. Pattern Matching: Each entry in the ignoreErrors array is evaluated:
    • RegExp entries: Tests the pattern using .test(errMessage)
    • String entries: Performs an exact equality comparison (errMessage === item)
  4. Interception: Upon match, the function logs the debug message ignoreErrors拦截成功 - 截条件:${item} 错误信息:${errMessage} and returns true, causing the error to be discarded.

Source Code Implementation

function isIgnoreErrors(error: any): boolean {
  if (!options.value.ignoreErrors.length) return false
  let errMessage = error.errMessage || error.message
  if (!errMessage) return false
  errMessage = String(errMessage)

  return options.value.ignoreErrors.some(item => {
    // RegExp case
    if (isRegExp(item)) {
      if ((item as RegExp).test(errMessage)) {
        debug(`ignoreErrors拦截成功 - 截条件:${item} 错误信息:${errMessage}`)
        return true
      }
      return false
    }
    // Exact string case
    if (errMessage === item) {
      debug(`ignoreErrors拦截成功 - 截条件:${item} 错误信息:${errMessage}`)
      return true
    }
    return false
  })
}

Source: [packages/core/src/lib/err.ts](https://github.com/m-cheng-web/web-tracing/blob/main/packages/core/src/lib/err.ts)

Testing and Validation

The functionality is validated in packages/core/__test__/err.spec.ts through a dedicated test case option ignoreErrors should work. This test confirms that errors with messages matching the configured patterns—such as those starting with "ignore" when using /^ignore/—are successfully intercepted and never reach the reporting endpoint.

Summary

  • The ignoreErrors option accepts an array of strings and RegExp objects in packages/core/src/lib/options.ts
  • Filtering occurs in packages/core/src/lib/err.ts via the isIgnoreErrors() function which checks against error.message or error.errMessage
  • Matching errors trigger a debug log (ignoreErrors拦截成功) and are silently dropped before backend transmission
  • Both exact string matches and regular expression patterns are supported simultaneously
  • Unit tests in packages/core/__test__/err.spec.ts verify the filtering behavior works as expected

Frequently Asked Questions

What pattern types does the ignoreErrors option support?

The ignoreErrors option supports two pattern types: exact string matches and JavaScript Regular Expressions. You can mix both types in the same array. For example, ['Exact Error Message', /^Network error/, /timeout.*/] filters exact strings, strings starting with "Network error", and any string containing "timeout".

Does ignoreErrors prevent errors from appearing in the console?

No, the ignoreErrors option only prevents errors from being reported to your monitoring backend. The errors still appear in the browser's developer console and follow normal JavaScript error propagation. This option specifically filters the telemetry pipeline in packages/core/src/lib/err.ts, not the runtime environment itself.

How do I debug my ignoreErrors configuration?

Enable debug logging in web-tracing to verify your filters. When an error matches a pattern in ignoreErrors, the SDK logs ignoreErrors拦截成功 - 截条件:${item} 错误信息:${errMessage} to the console. If you see this message, your filter is working. If the error still appears in your monitoring dashboard, verify that your RegExp syntax is correct and that you are matching against the actual error.message string.

Can I update ignoreErrors dynamically after initialization?

Based on the source code in packages/core/src/lib/options.ts, the ignoreErrors array is accessed via options.value.ignoreErrors at the time of error handling. While the documentation does not explicitly expose a public method for runtime updates, you can modify the options object directly if you have access to the internal options reference, though this is not recommended for production use without consulting the library's public API documentation.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →