diff options
Diffstat (limited to 'desktop/node_modules/serialize-error')
-rw-r--r-- | desktop/node_modules/serialize-error/index.d.ts | 58 | ||||
-rw-r--r-- | desktop/node_modules/serialize-error/index.js | 101 | ||||
-rw-r--r-- | desktop/node_modules/serialize-error/license | 9 | ||||
-rw-r--r-- | desktop/node_modules/serialize-error/package.json | 41 | ||||
-rw-r--r-- | desktop/node_modules/serialize-error/readme.md | 55 |
5 files changed, 264 insertions, 0 deletions
diff --git a/desktop/node_modules/serialize-error/index.d.ts b/desktop/node_modules/serialize-error/index.d.ts new file mode 100644 index 0000000..714dc9f --- /dev/null +++ b/desktop/node_modules/serialize-error/index.d.ts @@ -0,0 +1,58 @@ +import {Primitive, JsonObject} from 'type-fest'; + +export type ErrorObject = { + name?: string; + stack?: string; + message?: string; + code?: string; +} & JsonObject; + +/** +Serialize an `Error` object into a plain object. + +Non-error values are passed through. +Custom properties are preserved. +Circular references are handled. + +@example +``` +import {serializeError} from 'serialize-error'; + +const error = new Error('🦄'); + +console.log(error); +//=> [Error: 🦄] + +console.log(serializeError(error)); +//=> {name: 'Error', message: '🦄', stack: 'Error: 🦄\n at Object.<anonymous> …'} +``` +*/ +export function serializeError<ErrorType>(error: ErrorType): ErrorType extends Primitive + ? ErrorType + : ErrorObject; + +/** +Deserialize a plain object or any value into an `Error` object. + +`Error` objects are passed through. +Non-error values are wrapped in a `NonError` error. +Custom properties are preserved. +Non-enumerable properties are kept non-enumerable (name, message, stack). +Enumerable properties are kept enumerable (all properties besides the non-enumerable ones). +Circular references are handled. + +@example +``` +import {deserializeError} from 'serialize-error'; + +const error = deserializeError({ + message: 'aaa', + stack: 'at <anonymous>:1:13' +}); + +console.log(error); +// Error: aaa +// at <anonymous>:1:13 +``` +*/ +export function deserializeError(errorObject: ErrorObject | unknown): Error; diff --git a/desktop/node_modules/serialize-error/index.js b/desktop/node_modules/serialize-error/index.js new file mode 100644 index 0000000..508c6a2 --- /dev/null +++ b/desktop/node_modules/serialize-error/index.js @@ -0,0 +1,101 @@ +'use strict'; + +class NonError extends Error { + constructor(message) { + super(NonError._prepareSuperMessage(message)); + Object.defineProperty(this, 'name', { + value: 'NonError', + configurable: true, + writable: true + }); + + if (Error.captureStackTrace) { + Error.captureStackTrace(this, NonError); + } + } + + static _prepareSuperMessage(message) { + try { + return JSON.stringify(message); + } catch (_) { + return String(message); + } + } +} + +const commonProperties = [ + {property: 'name', enumerable: false}, + {property: 'message', enumerable: false}, + {property: 'stack', enumerable: false}, + {property: 'code', enumerable: true} +]; + +const destroyCircular = ({from, seen, to_, forceEnumerable}) => { + const to = to_ || (Array.isArray(from) ? [] : {}); + + seen.push(from); + + for (const [key, value] of Object.entries(from)) { + if (typeof value === 'function') { + continue; + } + + if (!value || typeof value !== 'object') { + to[key] = value; + continue; + } + + if (!seen.includes(from[key])) { + to[key] = destroyCircular({from: from[key], seen: seen.slice(), forceEnumerable}); + continue; + } + + to[key] = '[Circular]'; + } + + for (const {property, enumerable} of commonProperties) { + if (typeof from[property] === 'string') { + Object.defineProperty(to, property, { + value: from[property], + enumerable: forceEnumerable ? true : enumerable, + configurable: true, + writable: true + }); + } + } + + return to; +}; + +const serializeError = value => { + if (typeof value === 'object' && value !== null) { + return destroyCircular({from: value, seen: [], forceEnumerable: true}); + } + + // People sometimes throw things besides Error objects… + if (typeof value === 'function') { + // `JSON.stringify()` discards functions. We do too, unless a function is thrown directly. + return `[Function: ${(value.name || 'anonymous')}]`; + } + + return value; +}; + +const deserializeError = value => { + if (value instanceof Error) { + return value; + } + + if (typeof value === 'object' && value !== null && !Array.isArray(value)) { + const newError = new Error(); + destroyCircular({from: value, seen: [], to_: newError}); + return newError; + } + + return new NonError(value); +}; + +module.exports = { + serializeError, + deserializeError +}; diff --git a/desktop/node_modules/serialize-error/license b/desktop/node_modules/serialize-error/license new file mode 100644 index 0000000..fa7ceba --- /dev/null +++ b/desktop/node_modules/serialize-error/license @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/desktop/node_modules/serialize-error/package.json b/desktop/node_modules/serialize-error/package.json new file mode 100644 index 0000000..8caa8eb --- /dev/null +++ b/desktop/node_modules/serialize-error/package.json @@ -0,0 +1,41 @@ +{ + "name": "serialize-error", + "version": "7.0.1", + "description": "Serialize/deserialize an error into a plain object", + "license": "MIT", + "repository": "sindresorhus/serialize-error", + "funding": "https://github.com/sponsors/sindresorhus", + "author": { + "name": "Sindre Sorhus", + "email": "sindresorhus@gmail.com", + "url": "https://sindresorhus.com" + }, + "engines": { + "node": ">=10" + }, + "scripts": { + "test": "xo && ava && tsd" + }, + "files": [ + "index.js", + "index.d.ts" + ], + "keywords": [ + "error", + "serialize", + "stringify", + "object", + "convert", + "process", + "send", + "deserialize" + ], + "dependencies": { + "type-fest": "^0.13.1" + }, + "devDependencies": { + "ava": "^2.4.0", + "tsd": "^0.11.0", + "xo": "^0.30.0" + } +} diff --git a/desktop/node_modules/serialize-error/readme.md b/desktop/node_modules/serialize-error/readme.md new file mode 100644 index 0000000..d27f004 --- /dev/null +++ b/desktop/node_modules/serialize-error/readme.md @@ -0,0 +1,55 @@ +# serialize-error [![Build Status](https://travis-ci.org/sindresorhus/serialize-error.svg?branch=master)](https://travis-ci.org/sindresorhus/serialize-error) + +> Serialize/deserialize an error into a plain object + +Useful if you for example need to `JSON.stringify()` or `process.send()` the error. + +## Install + +``` +$ npm install serialize-error +``` + +## Usage + +```js +const {serializeError, deserializeError} = require('serialize-error'); + +const error = new Error('🦄'); + +console.log(error); +//=> [Error: 🦄] + +const serialized = serializeError(error) + +console.log(serialized); +//=> {name: 'Error', message: '🦄', stack: 'Error: 🦄\n at Object.<anonymous> …'} + +const deserialized = deserializeError(serialized); +//=> [Error: 🦄] +``` + +## API + +### serializeError(value) + +Type: `Error | unknown` + +Serialize an `Error` object into a plain object. + +Non-error values are passed through. +Custom properties are preserved. +Non-enumerable properties are kept non-enumerable (name, message, stack). +Enumerable properties are kept enumerable (all properties besides the non-enumerable ones). +Circular references are handled. + +### deserializeError(value) + +Type: `{[key: string]: unknown} | unknown` + +Deserialize a plain object or any value into an `Error` object. + +`Error` objects are passed through. +Non-error values are wrapped in a `NonError` error. +Custom properties are preserved. +Circular references are handled. |