ExceptionHelper.cs 6.6 KB

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