🪨 A well tested suite of tools designed to help FT.com applications be more reliable and measurable
Installations
npm install @dotcom-reliability-kit/serialize-error
Developer Guide
Typescript
Yes
Module System
CommonJS
Min. Node Version
20.x || 22.x
Node Version
22.13.0
NPM Version
10.9.2
Score
84.4
Supply Chain
100
Quality
88.8
Maintenance
100
Vulnerability
100
License
Releases
fetch-error-handler: v1.0.0
Published on 30 Jan 2025
opentelemetry: v3.0.0
Published on 21 Jan 2025
serialize-request: v4.0.0
Published on 21 Jan 2025
serialize-error: v4.0.0
Published on 21 Jan 2025
middleware-render-error-info: v6.0.0
Published on 21 Jan 2025
middleware-log-errors: v5.0.0
Published on 21 Jan 2025
Contributors
Languages
JavaScript (98.29%)
TypeScript (1.21%)
CSS (0.35%)
Shell (0.15%)
Developer
Financial-Times
Download Statistics
Total Downloads
481,560
Last Day
1,544
Last Week
6,844
Last Month
31,242
Last Year
315,998
GitHub Statistics
8 Stars
1,084 Commits
1 Forks
51 Watching
10 Branches
33 Contributors
Bundle Size
1.18 kB
Minified
617.00 B
Minified + Gzipped
Package Meta Information
Latest Version
4.0.0
Package Id
@dotcom-reliability-kit/serialize-error@4.0.0
Unpacked Size
9.71 kB
Size
3.29 kB
File Count
4
NPM Version
10.9.2
Node Version
22.13.0
Publised On
21 Jan 2025
Total Downloads
Cumulative downloads
Total Downloads
481,560
Last day
13%
1,544
Compared to previous day
Last week
-10%
6,844
Compared to previous week
Last month
23.9%
31,242
Compared to previous month
Last year
127%
315,998
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
No dependencies detected.
@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:
1npm install --save @dotcom-reliability-kit/serialize-error
Include in your code:
1import serializeError from '@dotcom-reliability-kit/serialize-error'; 2// or 3const 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:
1serializeError(new Error('example message')); 2// { 3// fingerprint: '...', 4// name: 'Error', 5// code: 'UNKNOWN', 6// message: 'An error occurred', 7// isOperational: false, 8// relatesToSystems: [], 9// cause: null, 10// stack: '...', 11// statusCode: null, 12// data: {} 13// }
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):
1serializeError({
2 message: 'example message'
3});
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.
No vulnerabilities found.
No security vulnerabilities found.