babel-plugin-catch-logger

raw JSON →
0.1.13 verified Sat Apr 25 auth: no javascript

A Babel plugin that automatically adds catch clauses to uncaught Promises and attaches a logging call (e.g., Sentry.captureException) to all catch blocks and .catch() handlers. Version 0.1.13 is the latest stable release. It works by injecting a configurable import for your logging service and wrapping catch clauses with a logger invocation. Differentiators: zero manual instrumentation, adds logging to existing codebases without refactoring, supports both try-catch blocks and promise chains, and allows namespace or default imports for the logging library.

error Cannot find module '@sentry/node'
cause The logging library specified in 'source' is not installed.
fix
Run npm install @sentry/node (or the appropriate logging library).
error Sentry.captureException is not a function
cause The logging method specified in 'methodName' does not exist on the imported module, often due to using namespaced: true with a library that does not export that method as a namespace member.
fix
Check the library's exports or set namespaced: false if the library has a default export with that method.
error Unexpected token: keyword «function»
cause The plugin may produce invalid syntax if the input code contains non-standard JavaScript (e.g., TypeScript, JSX) not handled by Babel presets.
fix
Ensure that the appropriate Babel presets (e.g., @babel/preset-env, @babel/preset-typescript) are loaded before this plugin.
gotcha The plugin adds catch clauses to ALL Promises without a catch, which may introduce side effects or duplicate error reporting if the developer already logs errors elsewhere.
fix Review generated code and manually handle uncaught Promises in a centralized way, or disable catchPromise for specific files using a babel environment.
gotcha If namespaced is false, the plugin expects a default export from the logging library; many libraries (e.g., @sentry/node) export as namespace by default, causing a runtime error.
fix Set namespaced: true when using libraries like @sentry/node that export named exports only; otherwise use a library with a default export.
gotcha The plugin modifies the AST significantly; it may conflict with other Babel plugins that transform Promises or try-catch blocks (e.g., babel-plugin-parameter-try-catch).
fix Ensure the run order of plugins is correct and test for conflicts, or use a single plugin for error handling.
npm install babel-plugin-catch-logger
yarn add babel-plugin-catch-logger
pnpm add babel-plugin-catch-logger

Shows basic Babel configuration and how the plugin transforms uncaught Promises.

// .babelrc
{
  "plugins": [
    [
      "babel-plugin-catch-logger",
      {
        "name": "Sentry",
        "source": "@sentry/node",
        "methodName": "captureException",
        "catchPromise": true,
        "namespaced": true
      }
    ]
  ]
}

// Input code (src/foo.js)
export function bar() {
  return fetch('https://example.com').then(res => res.json());
}

// Compiled output:
import * as Sentry from '@sentry/node';
export function bar() {
  return fetch('https://example.com').then(res => res.json()).catch(function (_e) {
    Sentry.captureException(_e);
  });
}