[d24f17c] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | class NonError extends Error {
|
---|
| 4 | constructor(message) {
|
---|
| 5 | super(NonError._prepareSuperMessage(message));
|
---|
| 6 | Object.defineProperty(this, 'name', {
|
---|
| 7 | value: 'NonError',
|
---|
| 8 | configurable: true,
|
---|
| 9 | writable: true
|
---|
| 10 | });
|
---|
| 11 |
|
---|
| 12 | if (Error.captureStackTrace) {
|
---|
| 13 | Error.captureStackTrace(this, NonError);
|
---|
| 14 | }
|
---|
| 15 | }
|
---|
| 16 |
|
---|
| 17 | static _prepareSuperMessage(message) {
|
---|
| 18 | try {
|
---|
| 19 | return JSON.stringify(message);
|
---|
| 20 | } catch {
|
---|
| 21 | return String(message);
|
---|
| 22 | }
|
---|
| 23 | }
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | const commonProperties = [
|
---|
| 27 | {property: 'name', enumerable: false},
|
---|
| 28 | {property: 'message', enumerable: false},
|
---|
| 29 | {property: 'stack', enumerable: false},
|
---|
| 30 | {property: 'code', enumerable: true}
|
---|
| 31 | ];
|
---|
| 32 |
|
---|
| 33 | const isCalled = Symbol('.toJSON called');
|
---|
| 34 |
|
---|
| 35 | const toJSON = from => {
|
---|
| 36 | from[isCalled] = true;
|
---|
| 37 | const json = from.toJSON();
|
---|
| 38 | delete from[isCalled];
|
---|
| 39 | return json;
|
---|
| 40 | };
|
---|
| 41 |
|
---|
| 42 | const destroyCircular = ({
|
---|
| 43 | from,
|
---|
| 44 | seen,
|
---|
| 45 | to_,
|
---|
| 46 | forceEnumerable,
|
---|
| 47 | maxDepth,
|
---|
| 48 | depth
|
---|
| 49 | }) => {
|
---|
| 50 | const to = to_ || (Array.isArray(from) ? [] : {});
|
---|
| 51 |
|
---|
| 52 | seen.push(from);
|
---|
| 53 |
|
---|
| 54 | if (depth >= maxDepth) {
|
---|
| 55 | return to;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | if (typeof from.toJSON === 'function' && from[isCalled] !== true) {
|
---|
| 59 | return toJSON(from);
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | for (const [key, value] of Object.entries(from)) {
|
---|
| 63 | if (typeof Buffer === 'function' && Buffer.isBuffer(value)) {
|
---|
| 64 | to[key] = '[object Buffer]';
|
---|
| 65 | continue;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | if (typeof value === 'function') {
|
---|
| 69 | continue;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | if (!value || typeof value !== 'object') {
|
---|
| 73 | to[key] = value;
|
---|
| 74 | continue;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | if (!seen.includes(from[key])) {
|
---|
| 78 | depth++;
|
---|
| 79 |
|
---|
| 80 | to[key] = destroyCircular({
|
---|
| 81 | from: from[key],
|
---|
| 82 | seen: seen.slice(),
|
---|
| 83 | forceEnumerable,
|
---|
| 84 | maxDepth,
|
---|
| 85 | depth
|
---|
| 86 | });
|
---|
| 87 | continue;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | to[key] = '[Circular]';
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | for (const {property, enumerable} of commonProperties) {
|
---|
| 94 | if (typeof from[property] === 'string') {
|
---|
| 95 | Object.defineProperty(to, property, {
|
---|
| 96 | value: from[property],
|
---|
| 97 | enumerable: forceEnumerable ? true : enumerable,
|
---|
| 98 | configurable: true,
|
---|
| 99 | writable: true
|
---|
| 100 | });
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | return to;
|
---|
| 105 | };
|
---|
| 106 |
|
---|
| 107 | const serializeError = (value, options = {}) => {
|
---|
| 108 | const {maxDepth = Number.POSITIVE_INFINITY} = options;
|
---|
| 109 |
|
---|
| 110 | if (typeof value === 'object' && value !== null) {
|
---|
| 111 | return destroyCircular({
|
---|
| 112 | from: value,
|
---|
| 113 | seen: [],
|
---|
| 114 | forceEnumerable: true,
|
---|
| 115 | maxDepth,
|
---|
| 116 | depth: 0
|
---|
| 117 | });
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | // People sometimes throw things besides Error objects…
|
---|
| 121 | if (typeof value === 'function') {
|
---|
| 122 | // `JSON.stringify()` discards functions. We do too, unless a function is thrown directly.
|
---|
| 123 | return `[Function: ${(value.name || 'anonymous')}]`;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | return value;
|
---|
| 127 | };
|
---|
| 128 |
|
---|
| 129 | const deserializeError = (value, options = {}) => {
|
---|
| 130 | const {maxDepth = Number.POSITIVE_INFINITY} = options;
|
---|
| 131 |
|
---|
| 132 | if (value instanceof Error) {
|
---|
| 133 | return value;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
|
---|
| 137 | const newError = new Error(); // eslint-disable-line unicorn/error-message
|
---|
| 138 | destroyCircular({
|
---|
| 139 | from: value,
|
---|
| 140 | seen: [],
|
---|
| 141 | to_: newError,
|
---|
| 142 | maxDepth,
|
---|
| 143 | depth: 0
|
---|
| 144 | });
|
---|
| 145 | return newError;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | return new NonError(value);
|
---|
| 149 | };
|
---|
| 150 |
|
---|
| 151 | module.exports = {
|
---|
| 152 | serializeError,
|
---|
| 153 | deserializeError
|
---|
| 154 | };
|
---|