contactlab / contactsnag

Bugsnag wrapper and utilities for Contactlab's applications
https://contactlab.github.io/contactsnag
Apache License 2.0
3 stars 0 forks source link
bug bugsnag contactlab tracker

ContactSnag

Node CI npm (scoped) node-current (scoped) GitHub package.json dependency version (dev dep on branch) GitHub package.json dependency version (dev dep on branch) GitHub package.json dependency version (dev dep on branch) GitHub

Bugsnag utilities for Contactlab's applications.

This package embeds 2 main functionalities:

It is fully written in TypeScript and extensively uses fp-ts library.

Requirements

Installation

Get the latest version from NPM registry:

$ npm install contactsnag fp-ts io-ts --save

# -- or --

$ yarn add contactsnag fp-ts io-ts

ContactSnag Client

The package exposes a single ContactSnag function.

This receives a configuration object and returns a ContactSnag client:

declare function ContactSnag(conf: Config): Client;

Config is a kind of subset of Bugsnag's configuration options with some differences:

Client is a custom data structure which embeds some Bugsnag.Client features, guarantees a lazy initialization and that an error is raised if a wrong configuration is passed.

interface Client {
  readonly client: () => ActualClient;

  readonly start: () => void;

  readonly notify: (
    error: Bugsnag.NotifiableError,
    onError?: Bugsnag.OnErrorCallback
  ) => IOEither<Error, void>;

  readonly setUser: (user: Bugsnag.User) => IOEither<Error, void>;
}

The ActualClient type encodes the three different states of the ContactSnag client:

type ActualClient = ConfigError | Still | Started;

ConfigError represents a not valid configuration provided to the client's creation function:

interface ConfigError {
  readonly type: 'ConfigError';
  readonly error: Error;
}

Still represents a not started client:

interface Still {
  readonly type: 'Still';
  readonly config: Config;
}

Started represents a started client:

interface Started {
  readonly type: 'Started';
  readonly bugsnag: Bugsnag.Client;
}

Each time a Client's method is called, it will internally check (a.k.a. fold) the current state and execute an operation.

client()

Returns the current ActualClient.

start()

Starts the client if it's in the Still state, otherwise it does nothing.

notify()

Returns a void effectful operation that can fail (IOEither).

When the IO is ran:

Usage example

Notify Bugsnag with a custom error message:

import {ContactSnag} from 'contactsnag';

const client = ContactSnag({
  apiKey: 'TEST-API-KEY',
  appVersion: '1.2.3',
  enabledReleaseStages: ['production'],
  releaseStage: 'production'
});

// Set notification on button click
document.getElementById('btn').addEventListener('click', () =>
  client.notify(new Error('Custom error message'), event => {
    event.addMetadata('custom', 'errname', 'My error name');
  })()
);

// Start Bugsnag
client.start();

setUser()

Returns a void effectful operation that can fail (IOEither).

When the IO is ran:

Usage example

Set a user for the entire session:

import {ContactSnag} from 'contactsnag';

const client = ContactSnag({
  apiKey: 'TEST-API-KEY',
  appVersion: '1.2.3',
  enabledReleaseStages: ['production'],
  releaseStage: 'production'
});

// Start Bugsnag
client.start();

// Set user after 1 second
setTimeout(() => {
  client.setUser({id: '1'})();
}, 1000);

Contributing

Opening issues is always welcome.

Then, fork the repository or create a new branch, write your code and send a pull request.

This project uses Prettier (automatically applied as pre-commit hook), ESLint (with TypeScript integration) and Jest.

Tests are run with:

$ npm test

License

Apache 2.0.