Fake Mirrors
    Preparing search index...

    Function serializeError

    Re-export of serialize-error library for serializing Error objects to JSON.

    • Serialize an Error object into a plain object.

      • 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).
      • Primitive values (including null, undefined, strings, numbers, etc.) and functions are wrapped in a NonError error and serialized.
      • Buffer properties are replaced with [object Buffer].
      • Circular references are handled.
      • If the input object has a .toJSON() method, then it's called instead of serializing the object's properties.
      • It's up to .toJSON() implementation to handle circular references and enumerability of the properties.

      Parameters

      • error: unknown
      • Optionaloptions: Options

      Returns ErrorObject

      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> …'}
      import {serializeError} from 'serialize-error';

      class ErrorWithDate extends Error {
      constructor() {
      super();
      this.date = new Date();
      }
      }

      const error = new ErrorWithDate();

      console.log(serializeError(error));
      //=> {date: '1970-01-01T00:00:00.000Z', name, message, stack}
      import {serializeError} from 'serialize-error';

      const error = new Error('Unicorn');

      error.horn = {
      toJSON() {
      return 'x';
      }
      };

      serializeError(error);
      // => {horn: 'x', name, message, stack}