Skip to content

appsignal/appsignal-javascript

Repository files navigation

AppSignal for JavaScript

Build Status code style: prettier

Description

Usage

First, make sure you've installed AppSignal in your application by following the steps in Installation.

Track any error

Catch an error and report it to AppSignal:

try {
  // do something that might throw an error
} catch (error) {
  appsignal.sendError(error)
  // handle the error
}

// You can catch errors asynchronously by listening to Promises...
asyncActionThatReturnsAPromise().catch(error => appsignal.sendError(error))

// ...or by using async/await
async function() {
  try {
    const result = await asyncActionThatReturnsAPromise()
  } catch (error) {
    appsignal.sendError(error)
    // handle the error
  }
}

// ...or in an event handler or callback function
events.on("event", (err) => {
  appsignal.sendError(err)
})

NOTE: Uncaught exceptions are not captured by default. We made the decision to not include this functionality as part of the core library due to the high amount of noise from browser extensions, ad blockers etc. that generally makes libraries such as this less effective.

We recommend using a relevant integration as a better way to handle exceptions, or, if you would prefer capture uncaught exceptions, you can do so by using the @appsignal/plugin-window-events package alongside this one.

Installation

First, sign up for an AppSignal account and add the @appsignal/javascript package to your package.json. Then, run yarn install/npm install.

You can also add these packages to your package.json on the command line:

yarn add @appsignal/javascript
npm install --save @appsignal/javascript

You can then import and use the package in your bundle:

import Appsignal from "@appsignal/javascript" // For ES Module
const Appsignal = require("@appsignal/javascript").default // For CommonJS module

const appsignal = new Appsignal({
  key: "YOUR FRONTEND API KEY"
})

It's recommended (although not necessarily required) to use the instance of the Appsignal object like a singleton. One way that you can do this is by exporting an instance of the library from a .js/.ts file somewhere in your project.

import Appsignal from "@appsignal/javascript"

export default new Appsignal({
  key: "YOUR FRONTEND API KEY"
}) 

Currently, we have no plans to supply a CDN-hosted version of this library.

If you're stuck, feel free to contact us for help!

Development

Installation

This repository is a mono-managed monorepo, containing packages (located in the /packages directory) that map to separate npm modules.

First install mono on your local machine by following the mono installation steps.

Then install the dependencies and prepare the project for development use using mono:

mono bootstrap

You can then run the following to start the compiler in watch mode. This automatically compiles both the ES Module and CommonJS variants:

mono run yarn build:watch --parallel

You can also build the library without watching the directory:

yarn build        # build both CJS and ESM
yarn build:cjs    # just CJS
yarn build:esm    # just ESM

Testing

The tests for this library use Jest as the test runner. Once you've installed the dependencies, you can run the following command in the root of this repository to run the tests for all packages, or in the directory of a package to run only the tests pertaining to that package:

mono test

Versioning

This repo uses Semantic Versioning (often referred to as semver). Each package in the repository is versioned independently from one another.

Contributing

Thinking of contributing to this repo? Awesome! πŸš€

Please follow our Contributing guide in our documentation and follow our Code of Conduct.

Also, we would be very happy to send you Stroopwafels. Have look at everyone we send a package to so far on our Stroopwafels page.

Support

Contact us and speak directly with the engineers working on AppSignal. They will help you get set up, tweak your code and make sure you get the most out of using AppSignal.

Also see our SUPPORT.md file.