@dotcom-reliability-kit/serialize-error
Advanced tools
Comparing version 2.1.0 to 2.2.0
@@ -7,2 +7,3 @@ export = serializeError; | ||
type SerializedError = { | ||
fingerprint: string | null; | ||
name: string; | ||
@@ -12,3 +13,3 @@ code: string; | ||
isOperational: boolean; | ||
relatesToSystems: Array<string>; | ||
relatesToSystems: string[]; | ||
cause: (Error | null); | ||
@@ -18,3 +19,3 @@ stack: (string | null); | ||
data: { | ||
[x: string]: any; | ||
[key: string]: any; | ||
}; | ||
@@ -21,0 +22,0 @@ }; |
@@ -0,3 +1,9 @@ | ||
const crypto = require('node:crypto'); | ||
/** | ||
* @typedef {object} SerializedError | ||
* @property {string | null} fingerprint | ||
* 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. | ||
* @property {string} name | ||
@@ -11,3 +17,3 @@ * The name of the class that the error is an instance of. | ||
* Whether the error is operational, as in it's an error we expect sometimes as part of running the application. | ||
* @property {Array<string>} relatesToSystems | ||
* @property {string[]} relatesToSystems | ||
* An array of FT system codes which are related to this error. | ||
@@ -20,3 +26,3 @@ * @property {(Error | null)} cause | ||
* An HTTP status code to represent the error. | ||
* @property {Object<string, any>} data | ||
* @property {{[key: string]: any}} data | ||
* Any additional error information. | ||
@@ -74,2 +80,10 @@ */ | ||
errorProperties.stack = error.stack; | ||
// Calculate the error fingerprint | ||
const errorStackLines = error.stack.split(/[\r\n]+/); | ||
const errorStackHeader = errorStackLines.slice(0, 2).join('\n'); | ||
errorProperties.fingerprint = crypto | ||
.createHash('md5') | ||
.update(errorStackHeader) | ||
.digest('hex'); | ||
} | ||
@@ -107,2 +121,3 @@ | ||
{ | ||
fingerprint: null, | ||
name: 'Error', | ||
@@ -109,0 +124,0 @@ code: 'UNKNOWN', |
{ | ||
"name": "@dotcom-reliability-kit/serialize-error", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "A utility function to serialize an error object in a way that's friendly to loggers, view engines, and converting to JSON", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -9,2 +9,3 @@ | ||
* [`SerializedError` type](#serializederror-type) | ||
* [`SerializedError.fingerprint`](#serializederrorfingerprint) | ||
* [`SerializedError.name`](#serializederrorname) | ||
@@ -46,2 +47,3 @@ * [`SerializedError.code`](#serializederrorcode) | ||
// { | ||
// fingerprint: '...', | ||
// name: 'Error', | ||
@@ -71,2 +73,9 @@ // code: 'UNKNOWN', | ||
#### `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` | ||
@@ -73,0 +82,0 @@ |
Sorry, the diff of this file is not supported yet
10859
141
124