DeveloperError.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import defined from "./defined.js";
  2. /**
  3. * Constructs an exception object that is thrown due to a developer error, e.g., invalid argument,
  4. * argument out of range, etc. This exception should only be thrown during development;
  5. * it usually indicates a bug in the calling code. This exception should never be
  6. * caught; instead the calling code should strive not to generate it.
  7. * <br /><br />
  8. * On the other hand, a {@link RuntimeError} indicates an exception that may
  9. * be thrown at runtime, e.g., out of memory, that the calling code should be prepared
  10. * to catch.
  11. *
  12. * @alias DeveloperError
  13. * @constructor
  14. * @extends Error
  15. *
  16. * @param {String} [message] The error message for this exception.
  17. *
  18. * @see RuntimeError
  19. */
  20. function DeveloperError(message) {
  21. /**
  22. * 'DeveloperError' indicating that this exception was thrown due to a developer error.
  23. * @type {String}
  24. * @readonly
  25. */
  26. this.name = "DeveloperError";
  27. /**
  28. * The explanation for why this exception was thrown.
  29. * @type {String}
  30. * @readonly
  31. */
  32. this.message = message;
  33. //Browsers such as IE don't have a stack property until you actually throw the error.
  34. let stack;
  35. try {
  36. throw new Error();
  37. } catch (e) {
  38. stack = e.stack;
  39. }
  40. /**
  41. * The stack trace of this exception, if available.
  42. * @type {String}
  43. * @readonly
  44. */
  45. this.stack = stack;
  46. }
  47. if (defined(Object.create)) {
  48. DeveloperError.prototype = Object.create(Error.prototype);
  49. DeveloperError.prototype.constructor = DeveloperError;
  50. }
  51. DeveloperError.prototype.toString = function () {
  52. let str = `${this.name}: ${this.message}`;
  53. if (defined(this.stack)) {
  54. str += `\n${this.stack.toString()}`;
  55. }
  56. return str;
  57. };
  58. /**
  59. * @private
  60. */
  61. DeveloperError.throwInstantiationError = function () {
  62. throw new DeveloperError(
  63. "This function defines an interface and should not be called directly."
  64. );
  65. };
  66. export default DeveloperError;