toError.mjs 496 B

123456789101112131415161718
  1. import { inspect } from './inspect.mjs';
  2. /**
  3. * Sometimes a non-error is thrown, wrap it as an Error instance to ensure a consistent Error interface.
  4. */
  5. export function toError(thrownValue) {
  6. return thrownValue instanceof Error
  7. ? thrownValue
  8. : new NonErrorThrown(thrownValue);
  9. }
  10. class NonErrorThrown extends Error {
  11. constructor(thrownValue) {
  12. super('Unexpected error value: ' + inspect(thrownValue));
  13. this.name = 'NonErrorThrown';
  14. this.thrownValue = thrownValue;
  15. }
  16. }