@dotcom-reliability-kit/log-error
Advanced tools
Comparing version 4.1.5 to 4.2.0
@@ -8,2 +8,3 @@ const appInfo = require('@dotcom-reliability-kit/app-info'); | ||
* @import { ErrorLoggingOptions } from '@dotcom-reliability-kit/log-error' | ||
* @import { SerializedError } from '@dotcom-reliability-kit/serialize-error' | ||
*/ | ||
@@ -15,3 +16,3 @@ | ||
* @private | ||
* @param {ErrorLoggingOptions & {event: string, level: 'error' | 'fatal' | 'warn'}} options | ||
* @param {Omit<ErrorLoggingOptions, 'error'> & {error: SerializedError, event: string, level: 'error' | 'fatal' | 'warn'}} options | ||
* The data to log. | ||
@@ -28,7 +29,6 @@ * @returns {void} | ||
}) { | ||
const serializedError = serializeError(error); | ||
const logData = { | ||
event, | ||
message: extractErrorMessage(serializedError), | ||
error: serializedError, | ||
message: extractErrorMessage(error), | ||
error, | ||
app: { | ||
@@ -88,8 +88,18 @@ commit: appInfo.commitHash, | ||
*/ | ||
function logHandledError({ error, includeHeaders, logger, request }) { | ||
function logHandledError({ | ||
error, | ||
includeHeaders, | ||
logger, | ||
logUserErrorsAsWarnings = false, | ||
request | ||
}) { | ||
const serializedError = serializeError(error); | ||
logError({ | ||
error, | ||
error: serializedError, | ||
event: 'HANDLED_ERROR', | ||
includeHeaders, | ||
level: 'error', | ||
level: | ||
logUserErrorsAsWarnings && isUserError(serializedError) | ||
? 'warn' | ||
: 'error', | ||
logger, | ||
@@ -106,4 +116,5 @@ request | ||
function logRecoverableError({ error, includeHeaders, logger, request }) { | ||
const serializedError = serializeError(error); | ||
logError({ | ||
error, | ||
error: serializedError, | ||
event: 'RECOVERABLE_ERROR', | ||
@@ -123,4 +134,5 @@ includeHeaders, | ||
function logUnhandledError({ error, includeHeaders, logger, request }) { | ||
const serializedError = serializeError(error); | ||
logError({ | ||
error, | ||
error: serializedError, | ||
event: 'UNHANDLED_ERROR', | ||
@@ -134,2 +146,14 @@ includeHeaders, | ||
/** | ||
* @param {SerializedError} error | ||
* @returns {boolean} | ||
*/ | ||
function isUserError(error) { | ||
return ( | ||
error.statusCode !== null && | ||
error.statusCode >= 400 && | ||
error.statusCode < 500 | ||
); | ||
} | ||
exports.logHandledError = logHandledError; | ||
@@ -136,0 +160,0 @@ exports.logRecoverableError = logRecoverableError; |
{ | ||
"name": "@dotcom-reliability-kit/log-error", | ||
"version": "4.1.5", | ||
"version": "4.2.0", | ||
"description": "A method to consistently log error object with optional request information", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -14,2 +14,3 @@ | ||
* [`options.logger`](#optionslogger) | ||
* [`options.logUserErrorsAsWarnings`](#optionslogusererrorsaswarnings) | ||
* [`options.request`](#optionsrequest) | ||
@@ -217,2 +218,11 @@ * [Migrating](#migrating) | ||
#### `options.logUserErrorsAsWarnings` | ||
> [!NOTE]<br /> | ||
> This option is only available in the [`logHandledError`](#loghandlederror) function. | ||
A `boolean` indicating whether to log user errors (those with a `400`–`499` `status` property) with a level of `warn` rather than `error`. This helps to reduce the amount of error-level logs that you need to focus on. | ||
This option defaults to `false`. | ||
#### `options.request` | ||
@@ -219,0 +229,0 @@ |
@@ -19,4 +19,8 @@ import { Request } from '@dotcom-reliability-kit/serialize-request'; | ||
export function logHandledError(options: ErrorLoggingOptions): void; | ||
export type HandledErrorLoggingOptions = ErrorLoggingOptions & { | ||
logUserErrorsAsWarnings?: boolean; | ||
}; | ||
export function logHandledError(options: HandledErrorLoggingOptions): void; | ||
export function logRecoverableError(options: ErrorLoggingOptions): void; | ||
@@ -23,0 +27,0 @@ |
14525
167
287