babel-plugin-try-catch-reporter

0.1.0 • Public • Published

babel-plugin-try-catch-reporter

Start

npm i -D babel-plugin-try-catch-reporter

Config

  • loggerName string required -- Log object name

  • loggerMethod string required -- Log object methods

  • exclude array optional (default ['__mocks__', '__tests__', '__snapshots__', 'node_modules']) -- Folders not processed, added values are merged

  • include array optional -- Target folder for processing

  • customErrorHandler string optional -- If there is a custom error handling function, it must be mounted on the window global object, loggerName does not take effect

config by loggerName

{
  "plugins": [
    [
      "babel-plugin-try-catch-reporter",
      {
        "loggerMethod": "captureException",
        "loggerName": "window.Sentry"
      }
    ]
  ]
}

Demo

try {
} catch {}

try {
} catch (_) {}

try {
} catch (E) {}

try {
  throw new Error('test');
} catch (_) {
  _Sentry.captureException(_);
}

try {
} catch (e) {
  let a = 0;
  window.Sentry.captureException(e);
}

gets compiled to:

try {
} catch (error) {
  if (typeof window.Sentry === 'object' && typeof window.Sentry.captureException === 'function') {
    window.Sentry.captureException(error);
  }
}

try {
} catch (_) {
  if (typeof window.Sentry === 'object' && typeof window.Sentry.captureException === 'function') {
    window.Sentry.captureException(_);
  }
}

try {
} catch (E) {
  if (typeof window.Sentry === 'object' && typeof window.Sentry.captureException === 'function') {
    window.Sentry.captureException(E);
  }
}

try {
  throw new Error('test');
} catch (_) {
  _Sentry.captureException(_);
}

try {
} catch (e) {
  let a = 0;
  window.Sentry.captureException(e);
}

config by customErrorHandler

{
  "plugins": [
    [
      "babel-plugin-try-catch-reporter",
      {
        "loggerMethod": "captureException",
        "loggerName": "window.Sentry",
        "customErrorHandler": "window.handleErrorReporter"
      }
    ]
  ]
}

Demo

try {
  throw new Error('catch-by-has-logger-customErrorHandler');
} catch (e) {
  window.handleErrorReporter(e);
}

try {
  throw new Error('catch-with-window-logger-by-customErrorHandler');
} catch (e) {
  window.Sentry.captureException(e);
}

gets compiled to:

try {
  throw new Error('catch-by-has-logger-customErrorHandler');
} catch (e) {
  window.handleErrorReporter(e);
}

try {
  throw new Error('catch-with-window-logger-by-customErrorHandler');
} catch (e) {
  window.Sentry.captureException(e);
}

Package Sidebar

Install

npm i babel-plugin-try-catch-reporter

Weekly Downloads

3

Version

0.1.0

License

MIT

Unpacked Size

8.6 kB

Total Files

6

Last publish

Collaborators

  • pipihua