ExceptionHelper.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using Jint.Runtime.CallStack;
  3. using Jint.Runtime.References;
  4. namespace Jint.Runtime
  5. {
  6. internal static class ExceptionHelper
  7. {
  8. public static T ThrowSyntaxError<T>(Engine engine, string message = null)
  9. {
  10. ThrowSyntaxError(engine, message);
  11. return default;
  12. }
  13. public static void ThrowSyntaxError(Engine engine, string message = null)
  14. {
  15. throw new JavaScriptException(engine.SyntaxError, message);
  16. }
  17. public static T ThrowArgumentException<T>(string message = null)
  18. {
  19. ThrowArgumentException(message);
  20. return default;
  21. }
  22. public static void ThrowArgumentException(string message = null)
  23. {
  24. ThrowArgumentException(message, null);
  25. }
  26. public static void ThrowArgumentException(string message, string paramName)
  27. {
  28. throw new ArgumentException(message, paramName);
  29. }
  30. public static void ThrowReferenceError(Engine engine, Reference reference)
  31. {
  32. ThrowReferenceError(engine, reference?.GetReferencedName());
  33. }
  34. public static void ThrowReferenceError(Engine engine, string name)
  35. {
  36. var message = name != null ? name + " is not defined" : null;
  37. throw new JavaScriptException(engine.ReferenceError, message);
  38. }
  39. public static T ThrowTypeErrorNoEngine<T>(string message = null, Exception exception = null)
  40. {
  41. throw new TypeErrorException(message);
  42. }
  43. public static T ThrowReferenceError<T>(Engine engine, string message = null)
  44. {
  45. throw new JavaScriptException(engine.ReferenceError, message);
  46. }
  47. public static T ThrowTypeError<T>(Engine engine, string message = null, Exception exception = null)
  48. {
  49. ThrowTypeError(engine, message, exception);
  50. return default;
  51. }
  52. public static void ThrowTypeError(Engine engine, string message = null, Exception exception = null)
  53. {
  54. throw new JavaScriptException(engine.TypeError, message, exception);
  55. }
  56. public static T ThrowRangeError<T>(Engine engine, string message = null)
  57. {
  58. throw new JavaScriptException(engine.RangeError, message);
  59. }
  60. public static T ThrowRangeErrorNoEngine<T>(string message)
  61. {
  62. throw new RangeErrorException(message);
  63. }
  64. public static void ThrowRangeError(Engine engine, string message = null)
  65. {
  66. throw new JavaScriptException(engine.RangeError, message);
  67. }
  68. public static void ThrowUriError(Engine engine)
  69. {
  70. throw new JavaScriptException(engine.UriError);
  71. }
  72. public static void ThrowNotImplementedException()
  73. {
  74. throw new NotImplementedException();
  75. }
  76. public static T ThrowNotImplementedException<T>()
  77. {
  78. throw new NotImplementedException();
  79. }
  80. public static T ThrowArgumentOutOfRangeException<T>()
  81. {
  82. throw new ArgumentOutOfRangeException();
  83. }
  84. public static void ThrowArgumentOutOfRangeException(string paramName, string message)
  85. {
  86. throw new ArgumentOutOfRangeException(paramName, message);
  87. }
  88. public static void ThrowTimeoutException()
  89. {
  90. throw new TimeoutException();
  91. }
  92. public static void ThrowStatementsCountOverflowException()
  93. {
  94. throw new StatementsCountOverflowException();
  95. }
  96. public static void ThrowArgumentOutOfRangeException()
  97. {
  98. throw new ArgumentOutOfRangeException();
  99. }
  100. public static T ThrowNotSupportedException<T>(string message = null)
  101. {
  102. throw new NotSupportedException(message);
  103. }
  104. public static void ThrowNotSupportedException(string message = null)
  105. {
  106. throw new NotSupportedException(message);
  107. }
  108. public static void ThrowInvalidOperationException(string message = null)
  109. {
  110. throw new InvalidOperationException(message);
  111. }
  112. public static void ThrowJavaScriptException(string message)
  113. {
  114. throw new JavaScriptException("TypeError");
  115. }
  116. public static void ThrowRecursionDepthOverflowException(JintCallStack currentStack, string currentExpressionReference)
  117. {
  118. throw new RecursionDepthOverflowException(currentStack, currentExpressionReference);
  119. }
  120. public static void ThrowArgumentNullException(string paramName)
  121. {
  122. throw new ArgumentNullException(paramName);
  123. }
  124. public static T ThrowArgumentNullException<T>(string paramName)
  125. {
  126. throw new ArgumentNullException(paramName);
  127. }
  128. public static void ThrowError(Engine engine, string message)
  129. {
  130. throw new JavaScriptException(engine.Error, message);
  131. }
  132. public static void ThrowPlatformNotSupportedException(string message)
  133. {
  134. throw new PlatformNotSupportedException(message);
  135. }
  136. public static void ThrowMemoryLimitExceededException(string message)
  137. {
  138. throw new MemoryLimitExceededException(message);
  139. }
  140. }
  141. }