npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rollbar-http-error

v0.7.1

Published

HttpError class and Express middleware for gracefully handling planned errors (eg. 403, 403). Wraps rollbar.js

Downloads

15

Readme

class HttpError {...}

Wrapper around Rollbar.js, which:

  • Comes with its own Express Middleware,
  • Allows you to create custom errors with 4xx or 5xx HTTP error codes,
  • Files HTTP errors in Rollbar as info. Regular old unplanned errors are filed as error.
  • Fixes the Express request body so Rollbar sees the correct Host, URL, and IP address if behind a proxy.

Usage with Express:

const express = require('express');
const HttpError = require('rollbar-http-error');
const config = require('./config');

const app = express();
app.set('trust proxy', true);

// .. app init code ..

// Final middleware
app.use(HttpError.middleware(config.rollbarToken));

If rollbarToken is null or undefined, then errors will be printed to the console.

View from Rollbar.js

The target use-case is one where HttpErrors are necessary, and planned, and thrown out to the Express middleware to be delivered to the user. It's important to track them in Rollbar but they're not critical issues and they should not trigger alerts.

Here's the view of a real error vs an HttpError:

Rollbar view

Using in the app

Inside your app, throw errors:

const HttpError = require('rollbar-http-error');

// ...

if (!logged_in) {
  throw HttpError.forbidden('You must be logged in.');
}
if (!found) {
  throw HttpError.notFound('That page does not exist.');
}
if (!validate(req)) {
  throw HttpError.badRequest('Bad request.');
}

Throw custom errors:

if (isDeprecated()) {
  throw new HttpError(410, 'That page is GONE.');
}

Send custom HTTP headers:

if (!valid_jwt()) {
  const headers = {
    'WWW-Authenticate': 'Bearer',
  }
  throw new HttpError(401, 'JWT timed out', headers);
}

Send custom JSON body in the client response:

if (error_details.length) {
  const body = {
    error_details,
  };
  throw new HttpError(403, 'Encountered several errors', {}, body);
}

Send custom properties to Rollbar

if (error_details.length) {
  const custom = {
    error_details,
  };
  throw new HttpError(403, 'Encountered several errors', {}, {}, custom);
}

Send a good old fashioned 500 error and report the error to rollbar:

if (!db) {
  throw new Error('Application error: No db is defined');
}

Reporting to Rollbar

It is possible to access Rollbar directly, either as debug, warning, info, or error. This will print to the console if no Rollbar token was passed to the middleware:

HttpError.report.warning('This should not happen', req);

It is possible to access the raw rollbar instance. This will return undefined if no Rollbar token was passed to the middleware:

const rollbar = HttpError.rollbar();