2
0

ExceptionHelper.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using System.Diagnostics.CodeAnalysis;
  2. using System.Reflection;
  3. using System.Runtime.ExceptionServices;
  4. using Jint.Native;
  5. using Jint.Native.Error;
  6. using Jint.Runtime.CallStack;
  7. using Jint.Runtime.Modules;
  8. namespace Jint.Runtime
  9. {
  10. /// <summary>
  11. /// Wraps known runtime type error information.
  12. /// </summary>
  13. internal sealed record ErrorDispatchInfo(ErrorConstructor ErrorConstructor, string? Message = null);
  14. internal static class ExceptionHelper
  15. {
  16. [DoesNotReturn]
  17. public static void ThrowSyntaxError(Realm realm, string? message = null)
  18. {
  19. throw CreateSyntaxError(realm, message);
  20. }
  21. [DoesNotReturn]
  22. public static void ThrowSyntaxError(Realm realm, string message, in SourceLocation location)
  23. {
  24. throw CreateSyntaxError(realm, message).SetJavaScriptLocation(location);
  25. }
  26. public static JavaScriptException CreateSyntaxError(Realm realm, string? message)
  27. {
  28. return new JavaScriptException(realm.Intrinsics.SyntaxError, message);
  29. }
  30. [DoesNotReturn]
  31. public static void ThrowArgumentException(string? message = null)
  32. {
  33. ThrowArgumentException(message, paramName: null);
  34. }
  35. [DoesNotReturn]
  36. public static void ThrowArgumentException(string? message, string? paramName)
  37. {
  38. throw new ArgumentException(message, paramName);
  39. }
  40. [DoesNotReturn]
  41. public static void ThrowReferenceError(Realm realm, Reference reference)
  42. {
  43. ThrowReferenceNameError(realm, reference?.ReferencedName?.ToString());
  44. }
  45. [DoesNotReturn]
  46. public static void ThrowReferenceNameError(Realm realm, string? name)
  47. {
  48. var message = name != null ? name + " is not defined" : null;
  49. ThrowReferenceError(realm, message);
  50. }
  51. [DoesNotReturn]
  52. public static void ThrowReferenceError(Realm realm, string? message)
  53. {
  54. var location = realm.GlobalObject.Engine.GetLastSyntaxElement()?.Location ?? default;
  55. throw new JavaScriptException(realm.Intrinsics.ReferenceError, message).SetJavaScriptLocation(location);
  56. }
  57. [DoesNotReturn]
  58. public static void ThrowTypeErrorNoEngine(string? message = null, Node? source = null)
  59. {
  60. throw new TypeErrorException(message, source);
  61. }
  62. [DoesNotReturn]
  63. public static void ThrowTypeError(Realm realm, string? message = null)
  64. {
  65. var location = realm.GlobalObject.Engine.GetLastSyntaxElement()?.Location ?? default;
  66. throw new JavaScriptException(realm.Intrinsics.TypeError, message).SetJavaScriptLocation(location);
  67. }
  68. [DoesNotReturn]
  69. public static void ThrowRangeError(Realm realm, string? message = null)
  70. {
  71. var location = realm.GlobalObject.Engine.GetLastSyntaxElement()?.Location ?? default;
  72. throw new JavaScriptException(realm.Intrinsics.RangeError, message).SetJavaScriptLocation(location);
  73. }
  74. public static ErrorDispatchInfo CreateUriError(Realm realm, string message)
  75. {
  76. return new ErrorDispatchInfo(realm.Intrinsics.UriError, message);
  77. }
  78. public static ErrorDispatchInfo CreateRangeError(Realm realm, string message)
  79. {
  80. return new ErrorDispatchInfo(realm.Intrinsics.RangeError, message);
  81. }
  82. [DoesNotReturn]
  83. public static void ThrowNotImplementedException(string? message = null)
  84. {
  85. throw new NotImplementedException(message);
  86. }
  87. [DoesNotReturn]
  88. public static void ThrowArgumentOutOfRangeException(string paramName, string message)
  89. {
  90. throw new ArgumentOutOfRangeException(paramName, message);
  91. }
  92. [DoesNotReturn]
  93. public static void ThrowTimeoutException()
  94. {
  95. throw new TimeoutException();
  96. }
  97. [DoesNotReturn]
  98. public static void ThrowStatementsCountOverflowException()
  99. {
  100. throw new StatementsCountOverflowException();
  101. }
  102. [DoesNotReturn]
  103. public static void ThrowArgumentOutOfRangeException()
  104. {
  105. #pragma warning disable MA0015
  106. throw new ArgumentOutOfRangeException();
  107. #pragma warning restore MA0015
  108. }
  109. [DoesNotReturn]
  110. public static void ThrowNotSupportedException(string? message = null)
  111. {
  112. throw new NotSupportedException(message);
  113. }
  114. [DoesNotReturn]
  115. public static void ThrowInvalidOperationException(string? message = null, Exception? exception = null)
  116. {
  117. throw new InvalidOperationException(message, exception);
  118. }
  119. [DoesNotReturn]
  120. public static void ThrowPromiseRejectedException(JsValue error)
  121. {
  122. throw new PromiseRejectedException(error);
  123. }
  124. [DoesNotReturn]
  125. public static void ThrowJavaScriptException(Engine engine, JsValue value, in Completion result)
  126. {
  127. throw new JavaScriptException(value).SetJavaScriptCallstack(engine, result.Location);
  128. }
  129. [DoesNotReturn]
  130. public static void ThrowJavaScriptException(Engine engine, JsValue value, in SourceLocation location)
  131. {
  132. throw new JavaScriptException(value).SetJavaScriptCallstack(engine, location);
  133. }
  134. [DoesNotReturn]
  135. public static void ThrowJavaScriptException(ErrorConstructor errorConstructor, string message)
  136. {
  137. throw new JavaScriptException(errorConstructor, message);
  138. }
  139. [DoesNotReturn]
  140. public static void ThrowRecursionDepthOverflowException(JintCallStack currentStack)
  141. {
  142. var currentExpressionReference = currentStack.Pop().ToString();
  143. throw new RecursionDepthOverflowException(currentStack, currentExpressionReference);
  144. }
  145. [DoesNotReturn]
  146. public static void ThrowArgumentNullException(string paramName)
  147. {
  148. throw new ArgumentNullException(paramName);
  149. }
  150. [DoesNotReturn]
  151. public static void ThrowMeaningfulException(Engine engine, TargetInvocationException exception)
  152. {
  153. var meaningfulException = exception.InnerException ?? exception;
  154. if (engine.Options.Interop.ExceptionHandler(meaningfulException))
  155. {
  156. ThrowError(engine, meaningfulException.Message);
  157. }
  158. ExceptionDispatchInfo.Capture(meaningfulException).Throw();
  159. #pragma warning disable CS8763
  160. }
  161. #pragma warning restore CS8763
  162. [DoesNotReturn]
  163. internal static void ThrowError(Engine engine, string message)
  164. {
  165. throw new JavaScriptException(engine.Realm.Intrinsics.Error, message);
  166. }
  167. [DoesNotReturn]
  168. public static void ThrowPlatformNotSupportedException(string message)
  169. {
  170. throw new PlatformNotSupportedException(message);
  171. }
  172. [DoesNotReturn]
  173. public static void ThrowMemoryLimitExceededException(string message)
  174. {
  175. throw new MemoryLimitExceededException(message);
  176. }
  177. [DoesNotReturn]
  178. public static void ThrowExecutionCanceledException()
  179. {
  180. throw new ExecutionCanceledException();
  181. }
  182. [DoesNotReturn]
  183. public static void ThrowModuleResolutionException(string message, string specifier, string? parent, string? filePath = null)
  184. {
  185. throw new ModuleResolutionException(message, specifier, parent, filePath);
  186. }
  187. [DoesNotReturn]
  188. public static void ThrowInvalidPreparedScriptArgumentException(string paramName)
  189. {
  190. throw new ArgumentException($"Instances of {typeof(Prepared<Script>)} returned by {nameof(Engine.PrepareScript)} are allowed only.", paramName);
  191. }
  192. [DoesNotReturn]
  193. public static void ThrowInvalidPreparedModuleArgumentException(string paramName)
  194. {
  195. throw new ArgumentException($"Instances of {typeof(Prepared<AstModule>)} returned by {nameof(Engine.PrepareModule)} are allowed only.", paramName);
  196. }
  197. }
  198. }