Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

serialize-error

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

serialize-error - npm Package Compare versions

Comparing version 8.0.1 to 8.1.0

27

index.d.ts

@@ -10,2 +10,25 @@ import {Primitive, JsonObject} from 'type-fest';

export interface Options {
/**
The maximum depth of properties to preserve when serializing/deserializing.
@default Number.POSITIVE_INFINITY
@example
```
import {serializeError} from 'serialize-error';
const error = new Error('🦄');
error.one = {two: {three: {}}};
console.log(serializeError(error, {maxDepth: 1}));
//=> {name: 'Error', message: '…', one: {}}
console.log(serializeError(error, {maxDepth: 2}));
//=> {name: 'Error', message: '…', one: { two: {}}}
```
*/
readonly maxDepth?: number;
}
/**

@@ -59,3 +82,3 @@ Serialize an `Error` object into a plain object.

*/
export function serializeError<ErrorType>(error: ErrorType): ErrorType extends Primitive
export function serializeError<ErrorType>(error: ErrorType, options?: Options): ErrorType extends Primitive
? ErrorType

@@ -88,2 +111,2 @@ : ErrorObject;

*/
export function deserializeError(errorObject: ErrorObject | unknown): Error;
export function deserializeError(errorObject: ErrorObject | unknown, options?: Options): Error;

@@ -46,3 +46,5 @@ 'use strict';

to_,
forceEnumerable
forceEnumerable,
maxDepth,
depth
}) => {

@@ -53,2 +55,6 @@ const to = to_ || (Array.isArray(from) ? [] : {});

if (depth >= maxDepth) {
return to;
}
if (typeof from.toJSON === 'function' && from[isCalled] !== true) {

@@ -74,6 +80,10 @@ return toJSON(from);

if (!seen.includes(from[key])) {
depth++;
to[key] = destroyCircular({
from: from[key],
seen: seen.slice(),
forceEnumerable
forceEnumerable,
maxDepth,
depth
});

@@ -100,3 +110,5 @@ continue;

const serializeError = value => {
const serializeError = (value, options = {}) => {
const {maxDepth = Number.POSITIVE_INFINITY} = options;
if (typeof value === 'object' && value !== null) {

@@ -106,3 +118,5 @@ return destroyCircular({

seen: [],
forceEnumerable: true
forceEnumerable: true,
maxDepth,
depth: 0
});

@@ -120,3 +134,5 @@ }

const deserializeError = value => {
const deserializeError = (value, options = {}) => {
const {maxDepth = Number.POSITIVE_INFINITY} = options;
if (value instanceof Error) {

@@ -128,3 +144,9 @@ return value;

const newError = new Error(); // eslint-disable-line unicorn/error-message
destroyCircular({from: value, seen: [], to_: newError});
destroyCircular({
from: value,
seen: [],
to_: newError,
maxDepth,
depth: 0
});
return newError;

@@ -131,0 +153,0 @@ }

2

package.json
{
"name": "serialize-error",
"version": "8.0.1",
"version": "8.1.0",
"description": "Serialize/deserialize an error into a plain object",

@@ -5,0 +5,0 @@ "license": "MIT",

@@ -36,3 +36,3 @@ # serialize-error

### serializeError(value)
### serializeError(value, options?)

@@ -80,3 +80,3 @@ Type: `Error | unknown`

### deserializeError(value)
### deserializeError(value, options?)

@@ -91,1 +91,25 @@ Type: `{[key: string]: unknown} | unknown`

Circular references are handled.
### options
Type: `object`
#### maxDepth
Type: `number`\
Default: `Number.POSITIVE_INFINITY`
The maximum depth of properties to preserve when serializing/deserializing.
```js
const {serializeError} = require('serialize-error');
const error = new Error('🦄');
error.one = {two: {three: {}}};
console.log(serializeError(error, {maxDepth: 1}));
//=> {name: 'Error', message: '…', one: {}}
console.log(serializeError(error, {maxDepth: 2}));
//=> {name: 'Error', message: '…', one: { two: {}}}
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc