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

@dotcom-reliability-kit/serialize-error

v3.2.0

Published

A utility function to serialize an error object in a way that's friendly to loggers, view engines, and converting to JSON

Downloads

33,846

Readme

@dotcom-reliability-kit/serialize-error

A utility function to serialize an error object in a way that's friendly to loggers, view engines, and converting to JSON. This module is part of FT.com Reliability Kit.

Usage

Install @dotcom-reliability-kit/serialize-error as a dependency:

npm install --save @dotcom-reliability-kit/serialize-error

Include in your code:

import serializeError from '@dotcom-reliability-kit/serialize-error';
// or
const serializeError = require('@dotcom-reliability-kit/serialize-error');

serializeError

The serializeError function accepts an error-like object (e.g. an instance of Error or an object with a message property) and returns a plain JavaScript object (conforming to the SerializedError type) which contains the relevant properties:

serializeError(new Error('example message'));
// {
//     fingerprint: '...',
//     name: 'Error',
//     code: 'UNKNOWN',
//     message: 'An error occurred',
//     isOperational: false,
//     relatesToSystems: [],
//     cause: null,
//     stack: '...',
//     statusCode: null,
//     data: {}
// }

You can also pass in a plain object if you already have one that looks like an error (e.g. from a JSON API response):

serializeError({
    message: 'example message'
});

SerializedError type

The SerializedError type documents the return value of the serializeError function. It will always have the following properties, extracting them from a given error object.

SerializedError.fingerprint

This is a hash of the first part of the error stack, used to help group errors that occurred in the same part of the codebase. The fingerprint is null if the error does not include a stack trace.

[!WARNING] Do not rely on the format or length of the error fingerprint as the underlying hash may change without warning. You can rely on the fingerprint being unique to the type of error being thrown.

SerializedError.name

This is extracted from the error.name property and is always cast to a String. It defaults to "Error".

SerializedError.code

This is extracted from the error.code property and is always cast to a String. It defaults to "UNKNOWN".

SerializedError.message

This is extracted from the error.message property and is always cast to a String. It defaults to "An error occurred", which is not very helpful but we want this library to be resilient to unexpected input data.

SerializedError.isOperational

This indicates whether the error is operational (known about). See the documentation for OperationalError for more information. It is extracted from the error.isOperational property. It is always cast to a Boolean and defaults to false.

SerializedError.relatesToSystems

This array contains a list of system codes which are related to the error. It defaults to an empty array.

SerializedError.cause

This is an error instance extracted from the error.cause property, which is serialized before being assigned. It defaults to null.

SerializedError.errors

This is an array of error instances extracted from the error.errors property (e.g. from an AggregateError). The errors are serialized before being assigned.

SerializedError.stack

This is extracted from the error.stack property. If this property is not a string, then it will default to null.

SerializedError.statusCode

This is extracted from the error.statusCode property first, then the error.status property otherwise (we use look at both properties to maintain compatibility with third-party request and error libraries). These are always cast to a Number and this property defaults to null.

SerializedError.data

This is extracted from the error.data property. If this property is not a plain object, then it will default to an empty object: {}.

Migrating

Consult the Migration Guide if you're trying to migrate to a later major version of this package.

Contributing

See the central contributing guide for Reliability Kit.

License

Licensed under the MIT license. Copyright © 2022, The Financial Times Ltd.