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

blip-http-errors

v1.0.2

Published

Error handling library with special considerations for HTTP clients and logging

Downloads

5

Readme

Blip

Error handling library with special considerations for HTTP clients and logging.

Get started

npm install blip-http-errors
yarn add blip-http-errors

Usage

throw new BlipError(message, options); // in your catch statement

logger.error(err.inspect); // in your logger (⚠️ SERVER ONLY - not for client response)
return res.status(blip.statusCode).json(blip); // using your favourite framework - in your error handler (✅ Client safe)

Still confused? Check the example below.

Example value (json)

{ 
  "err": {
    "message": "Your error message",
    "statusCode": 500
  }
}

Why Blip?

The idea came from the need to have a HTTP consistent error handling library for both the server and the client. Blip is designed client-safe from get-go, so you don't need to worry about your sensitive data leaking to the client.

Blip has taken inspiration from React's concept of "lifting state up". In Blip, we encourage "lifting errors up". This allows you to keep a consistent error handling experience, no matter what the error.

Example

Let's say your application has specific error handling logic - it might use a third-party logger, or a solution like Sentry. It's easy to end up with repetitive code handling your error, like this:

// First error
try {
    throw new Error('beep!');
} catch (err) {
    logger.error(err);
    return res.status(500).json({ statusCode: 500, message: 'beep!' });
}

// Second error
try {
    throw new Error('boop!');
} catch (err) {
    logger.error(err);
    return res.status(400).json({ statusCode: 400, message: 'boop!' });
}

These errors are handled in a similar fashion, and it means if we want to change our error handling logic, we need to change it in multiple places. Not good! With Blip, we can lift the error handling logic up, and keep our code clean:

// First error
try {
    throw new Error('beep!');
} catch (err) {
    throw new BlipError('beep!', { statusCode: 500, rootErr: err }); // pass rootErr to keep the original error
}

// Second error
try {
    throw new Error('boop!');
} catch (err) {
    throw new BlipError('boop!', { statusCode: 500, rootErr: err }); // pass rootErr to keep the original error
}

// Lifted error handling logic
function handler() {
    logger.err(err.inspect); // contains info about BlipError, and the original error (rootErr).
    return res.status(err.statusCode).json(err);
}

API

You can view the full API here.

throw new BlipError(message, options)

Throws a BlipError. The returned BlipError is client-safe and can be sent in responses JSON. Parameters:

  • message - The error message to display to the client.
  • options - An optional object containing any of the following:
    • statusCode - The HTTP status code of the error. Defaults to 500.
    • rootErr - The original error.
    • data - Any additional metadata for inspection.

blipError.inspection

⚠️ This property is designed for server use only - do not send the response to the client as it may contain sensitive data (such as data and rootErr's name and type properties).

Returns a special representation of BlipError that can be inspected by the console or custom logging solutions. Aliases: blipError.inspect, blipError.log

blipError.message

Returns the error message to display to the client.

blipError.statusCode

Returns the HTTP Status Code associated with this error. Aliases: blipError.status, blipError.code

blipError.rootErr

⚠️ This property is designed for server use only - it's usually not advisable to send the original Error to the client (most frameworks will send an empty object to be safe by default).

Returns the original Error.

blipError.stack

⚠️ Stack traces are not client-safe, and should not be sent to the client.

Returns the stack trace of the rootErr, or the stack trace of the BlipError if rootErr is not defined.

blipError.originalStack

⚠️ Stack traces are not client-safe, and should not be sent to the client.

Blip will overwrite the stack trace of the BlipError with the stack trace of the rootErr if it's defined. It's unlikely you'll ever use this, but this getter will obtain the original stack trace of the BlipError.