formatError.js 819 B

123456789101112131415161718192021222324252627282930
  1. import defined from './defined.js';
  2. /**
  3. * Formats an error object into a String. If available, uses name, message, and stack
  4. * properties, otherwise, falls back on toString().
  5. *
  6. * @exports formatError
  7. *
  8. * @param {*} object The item to find in the array.
  9. * @returns {String} A string containing the formatted error.
  10. */
  11. function formatError(object) {
  12. var result;
  13. var name = object.name;
  14. var message = object.message;
  15. if (defined(name) && defined(message)) {
  16. result = name + ': ' + message;
  17. } else {
  18. result = object.toString();
  19. }
  20. var stack = object.stack;
  21. if (defined(stack)) {
  22. result += '\n' + stack;
  23. }
  24. return result;
  25. }
  26. export default formatError;