isError.js 991 B

123456789101112131415161718192021222324252627282930313233343536
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = isError;
  6. var _prettyFormat = _interopRequireDefault(require('pretty-format'));
  7. function _interopRequireDefault(obj) {
  8. return obj && obj.__esModule ? obj : {default: obj};
  9. }
  10. /**
  11. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  12. *
  13. * This source code is licensed under the MIT license found in the
  14. * LICENSE file in the root directory of this source tree.
  15. */
  16. function isError(potentialError) { // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  17. // duck-type Error, see #2549
  18. const isError =
  19. potentialError !== null &&
  20. typeof potentialError === 'object' &&
  21. typeof potentialError.message === 'string' &&
  22. typeof potentialError.name === 'string';
  23. const message = isError
  24. ? null
  25. : `Failed: ${(0, _prettyFormat.default)(potentialError, {
  26. maxDepth: 3
  27. })}`;
  28. return {
  29. isError,
  30. message
  31. };
  32. }