Source: Core/formatError.js

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