ExceptionHelper.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. public static T ThrowInvalidOperationException<T>(string message = null)
  131. {
  132. throw new InvalidOperationException(message);
  133. }
  134. public static T ThrowPromiseRejectedException<T>(JsValue error)
  135. {
  136. throw new PromiseRejectedException(error);
  137. }
  138. [DoesNotReturn]
  139. public static void ThrowJavaScriptException(Engine engine, JsValue value, in Completion result)
  140. {
  141. throw new JavaScriptException(value).SetCallstack(engine, result.Location);
  142. }
  143. [DoesNotReturn]
  144. public static void ThrowRecursionDepthOverflowException(JintCallStack currentStack, string currentExpressionReference)
  145. {
  146. throw new RecursionDepthOverflowException(currentStack, currentExpressionReference);
  147. }
  148. [DoesNotReturn]
  149. public static void ThrowArgumentNullException(string paramName)
  150. {
  151. throw new ArgumentNullException(paramName);
  152. }
  153. public static T ThrowArgumentNullException<T>(string paramName)
  154. {
  155. throw new ArgumentNullException(paramName);
  156. }
  157. [DoesNotReturn]
  158. public static void ThrowMeaningfulException(Engine engine, TargetInvocationException exception)
  159. {
  160. var meaningfulException = exception.InnerException ?? exception;
  161. var handler = engine.Options._ClrExceptionsHandler;
  162. if (handler != null && handler(meaningfulException))
  163. ThrowError(engine, meaningfulException.Message);
  164. throw meaningfulException;
  165. }
  166. [DoesNotReturn]
  167. public static void ThrowError(Engine engine, string message)
  168. {
  169. throw new JavaScriptException(engine.Error, message);
  170. }
  171. [DoesNotReturn]
  172. public static void ThrowPlatformNotSupportedException(string message)
  173. {
  174. throw new PlatformNotSupportedException(message);
  175. }
  176. [DoesNotReturn]
  177. public static void ThrowMemoryLimitExceededException(string message)
  178. {
  179. throw new MemoryLimitExceededException(message);
  180. }
  181. [DoesNotReturn]
  182. public static void ThrowExecutionCanceledException()
  183. {
  184. throw new ExecutionCanceledException();
  185. }
  186. }
  187. }