Skip to content

error-reporter/weer

Repository files navigation

Weer

Weer screenshot

Build Status

Web Extensions Error Reporter catches global errors, shows notifications and opens error reporter in one click

Report page demo

Table of Contents

Why

Catching Errors Without Weer

There is some mess in how you catch errors in a web-extension:

'use strict'; // Only if you don't use ES6 modules.
/*
  bg-window — background window, main window of a web-extension.
  non-bg-windows — popup, settings and other pages windows of a web-extension, that are not bg-window.
*/

window.addEventListener('error', (errorEvent) => {/* ... */});

// Case 1
throw new Error('Root (caught only in bg-window, not caught in non-bg windows');

// Case 2
setTimeout(
  () => { throw new Error('Timeouted root (caught by handlers'); },
  0,
);

// Case 3
chrome.tabs.getCurrent(() => {

  throw new Error('Chrome API callback (not caught by handlers)');

});

// Case 4
chrome.tabs.getCurrent(() => setTimeout(() => {

  throw new Error('Timeouted Chrome API callback (caught by handlers)');

}, 0));

// Case 5
chrome.tabs.getCurrent(async () => {

  throw new Error(
    'Async Chrome API callback (caught by handlers in Chrome, not caught in FireFox even if timeouted)',
  );

});

So if you want error catchers to work — your code must be wrapped in setTimeout.

This behavior may be a bug and is discussed in https://crbug.com/357568.

Now let's look how to catch errors with Weer.

Weer in a Background Script

'use strict'; // Only if you don't use ES6 modules.
// Import and setup Weer here, see corresponding paragraphs below.

throw new Error('This is caught by Weer, notification is shown, opens error reporter on click');

Weer in a Non-Background Script

// In popup, settings and other pages.
'use strict'; // Only if you don't use ES6 modules.

chrome.runtime.getBackgroundPage((bgWindow) =>
  bgWindow.Weer.ErrorCatchers.installListenersOn({ hostWindow: window, nameForDebug: 'PUP' }, () => {

    // Put all your code inside this arrow body (it is timeouted).

    // Case 1:
    throw new Error('PUPERR (caught by Weer)');

    // Case 2:
    document.getElementById('btn').onclick = () => {

      throw new Error('ONCLCK! (caught by Weer)');

    };

    // Case 3:
    chrome.tabs.getCurrent(Weer.Utils.timeouted(() => {

      throw new Error('Timeouted Chrome API callback (caught by Weer)');

    }));

  })
);

// Case 4:
chrome.tabs.getCurrent(Weer.Utils.timeouted(() => {

  throw new Error('Timeouted Chrome API callback (caught by Weer)');

}));

// Case 5
chrome.tabs.getCurrent(async () => {

  throw new Error(
    'Async Chrome API callback (caught by Weer in Chrome, never caught in FireFox even if timeouted)',
  );

});

Install

npm install --save weer

Usage

Formats

tree ./node_modules/weer
weer/
├── cjs // Common JS format: `require(...)`
│   ├── error-catchers.js
│   ├── get-notifiers-singleton.js
│   ├── index.js
│   └── utils.js
├── esm // EcmaScript Modules format: `import ...`
│   ├── error-catchers.js
│   ├── get-notifiers-singleton.js
│   ├── index.js
│   └── utils.js
├── package.json
└── umd // Universal Module Definition format: `<script src=...></script>`
    ├── error-catchers.js // Requires `utils` bundle
    ├── get-notifiers-singleton.js // Requires `utils` bundle
    ├── index.js // All in one bundle, no dependencies
    └── utils.js

Import

With Bundler

For webpack, rollup, etc.

import Weer from 'weer';

If you need only a part of the API:

import Utils from 'weer/esm/utils';
import ErrorCatchers from 'weer/esm/error-catchers';
import GetNotifiersSingleton from 'weer/esm/get-notifiers-singleton';

Without Bundler

$ cp ./node_modules/weer/umd/index.js ./foo-extension/vendor/weer.js
$ cat foo-extension/manifest.json
...
"scripts": [
  "./vendor/optional-debug.js",
  "./vendor/weer.js",
  ...
],
...

Setup

Permissions in manifest.json

"permissions": [
  "notifications",
  ...
],

BG Window

'use strict'; // Only if you don't use ES6 modules.
// For EcmaScript modules (node_modules/weer/esm) and CommonJS (node_modules/weer/cjs):
//   1. Import Weer somehow.
//   2. window.Weer = Weer; // Expose for non-bg windows (popup, settings, etc.).

Weer.install({
  // Required:
  sendReports: {
    toEmail: 'homerjsimpson@example.com',
    inLanguages: ['en'], // In what languages to show report template.
  },
  // Optional:
  extErrorIconUrl: 'https://example.com/img/ext-error-128.png',
  pacErrorIconUrl: 'https://example.com/img/pac-error-128.png',
  maskIconUrl: 'https://example.com/img/mask-128.png',
});

Debugging

  1. Bundle visionmedia/debug for your environment and export global debug.
  2. Enable it by debug.enable('weer:*') in extension background window and reload extension.

Examples of Setups

See examples of setups for webpack, rollup or without bundlers.

Demo

clone this repo
npm install
cd examples
npm start
ls dist <- Load as unpacked extension and play (tested on Chrome).

Supported Browsers

Chrome: yes.
Firefox: yes, but notifications are not sticky, unhandled proimise rejections are never caught, clicking notifications sometimes doesn't work.

API

See API.md.

Maintainer

Contribute

You are welcome to propose issues, pull requests or ask questions.
By commiting your code you agree to give all the rights on your contribution to Ilya Ig. Petrov.

Credits

For credits of used assets see https://github.com/error-reporter/error-reporter.github.io

License

The product is dual-licensed under GPL-3.0+ and commercial license. To obtain commercial license contact Ilya Ig. Petrov.