NativeErrors.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. // TrimStackTracePath is needed because same file is run in version 1/2 where LoadScriptFile is not defined
  6. function TrimStackTracePath(obj)
  7. {
  8. return obj;
  9. }
  10. if (this.WScript && typeof this.WScript.LoadScriptFile === "function")
  11. {
  12. this.WScript.LoadScriptFile("TrimStackTracePath.js");
  13. }
  14. function PadString(s, l)
  15. {
  16. while (s.length < l)
  17. {
  18. s += ' ';
  19. }
  20. return s;
  21. }
  22. function DumpObject(o)
  23. {
  24. var a = new Array();
  25. for (var i in o)
  26. {
  27. a[a.length] = i;
  28. }
  29. a[a.length] = "description"; // Explicitly adding the known non-enumerable members
  30. a[a.length] = "number";
  31. a[a.length] = "stack";
  32. a.sort();
  33. for (var i = 0; i < a.length; i++)
  34. {
  35. if (a[i] === "stack")
  36. {
  37. o[a[i]] = TrimStackTracePath(o[a[i]]);
  38. }
  39. WScript.Echo(PadString(a[i], 15) + "= " + PadString("(" + typeof(o[a[i]]) + ")", 10) + o[a[i]]);
  40. }
  41. WScript.Echo(PadString("toString()", 15) + "= " + o.toString());
  42. }
  43. function Test(s)
  44. {
  45. WScript.Echo(s);
  46. DumpObject(eval("new " + s));
  47. WScript.Echo();
  48. }
  49. function safeCall(f)
  50. {
  51. var args = [];
  52. for (var a = 1; a < arguments.length; ++a)
  53. args.push(arguments[a]);
  54. try
  55. {
  56. return f.apply(this, args);
  57. }
  58. catch (ex)
  59. {
  60. WScript.Echo(ex.name + ": " + ex.message);
  61. }
  62. }
  63. Test("EvalError");
  64. Test("RangeError('This is a range error')");
  65. Test("ReferenceError");
  66. Test("SyntaxError");
  67. Test("TypeError('This is a type error')");
  68. Test("URIError");
  69. safeCall(Test, "RegExpError");
  70. safeCall(Test, "ConversionError");