errorProps.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. function showProperty(x, y)
  6. {
  7. var value = y[x];
  8. if (x === "stack") {
  9. value = value && value.replace(/\(.+\\test.Error./ig, "(");
  10. }
  11. WScript.Echo(" " + x + "\t isOwn = " + y.hasOwnProperty(x) + "\t value = " + value);
  12. }
  13. function test(y)
  14. {
  15. WScript.Echo(" ToString = "+y);
  16. WScript.Echo(" Properties = ");
  17. showProperty("name", y);
  18. showProperty("message", y);
  19. showProperty("stack", y); // Explicitly adding the known non-enumerable members
  20. showProperty("number", y);
  21. showProperty("description", y);
  22. for (x in y)
  23. {
  24. showProperty(x, y);
  25. }
  26. }
  27. function safeCall(f) {
  28. var args = [];
  29. for (var a = 1; a < arguments.length; ++a)
  30. args.push(arguments[a]);
  31. try {
  32. return f.apply(this, args);
  33. } catch (ex) {
  34. WScript.Echo(ex.name + ": " + ex.message);
  35. }
  36. }
  37. WScript.Echo("Error.prototype");
  38. test(Error.prototype);
  39. WScript.Echo("RangeError.prototype");
  40. test(RangeError.prototype);
  41. safeCall(function () {
  42. WScript.Echo("ConversionError.prototype");
  43. test(ConversionError.prototype);
  44. });
  45. WScript.Echo("\nError");
  46. test(Error) ;
  47. err = new Error();
  48. WScript.Echo("\nnew Error()");
  49. test(err);
  50. err = new Error(undefined);
  51. WScript.Echo("\nnew Error(undefined)");
  52. test(err);
  53. err = new Error(null);
  54. WScript.Echo("\nnew Error(null)");
  55. test(err);
  56. err = new Error("Hello");
  57. WScript.Echo("\nnew Error(\"Hello\")");
  58. test(err);
  59. err = new Error(100, "With a number");
  60. WScript.Echo("\nnew Error(100, \"With a number\")");
  61. test(err) ;
  62. err = new Error("Hello");
  63. err.name = undefined;
  64. WScript.Echo("\nnew Error(\"Hello\"); name=undefined");
  65. test(err);
  66. err = new ReferenceError("I'm a reference error");
  67. WScript.Echo("\nnew ReferenceError(\"I'm a reference error\")");
  68. test(err);
  69. safeCall(function () {
  70. err = new RegExpError(22, "This is a RegExp error");
  71. WScript.Echo("\nnew RegExpError(22, \"This is a RegExp error\")");
  72. test(err);
  73. });
  74. err = new TypeError();
  75. WScript.Echo("\nnew TypeError()");
  76. test(err);
  77. err = new TypeError(undefined);
  78. WScript.Echo("\nnew TypeError(undefined)");
  79. test(err);
  80. err = new TypeError(null);
  81. WScript.Echo("\nnew TypeError(null)");
  82. test(err);
  83. var undef;
  84. err = new TypeError("With a undef name");
  85. err.name = undef;
  86. WScript.Echo("\nnew TypeError(\"With a undef name\")");
  87. test(err);
  88. WScript.Echo("\nRuntime TypeError()");
  89. try
  90. {
  91. blah = boo;
  92. } catch(err)
  93. {
  94. test(err);
  95. }