logError.js 757 B

123456789101112131415161718192021222324252627
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @format
  8. * @flow strict
  9. */
  10. 'use strict';
  11. /**
  12. * Small utility that can be used as an error handler. You cannot just pass
  13. * `console.error` as a failure callback - it's not properly bound. If passes an
  14. * `Error` object, it will print the message and stack.
  15. */
  16. const logError = function(...args: $ReadOnlyArray<mixed>) {
  17. if (args.length === 1 && args[0] instanceof Error) {
  18. const err = args[0];
  19. console.error('Error: "' + err.message + '". Stack:\n' + err.stack);
  20. } else {
  21. console.error.apply(console, args);
  22. }
  23. };
  24. module.exports = logError;