toError.js 611 B

12345678910111213141516171819202122232425
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true,
  4. });
  5. exports.toError = toError;
  6. var _inspect = require('./inspect.js');
  7. /**
  8. * Sometimes a non-error is thrown, wrap it as an Error instance to ensure a consistent Error interface.
  9. */
  10. function toError(thrownValue) {
  11. return thrownValue instanceof Error
  12. ? thrownValue
  13. : new NonErrorThrown(thrownValue);
  14. }
  15. class NonErrorThrown extends Error {
  16. constructor(thrownValue) {
  17. super('Unexpected error value: ' + (0, _inspect.inspect)(thrownValue));
  18. this.name = 'NonErrorThrown';
  19. this.thrownValue = thrownValue;
  20. }
  21. }