ExceptionHelper.cs 5.9 KB

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