ExceptionHelper.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 new JavaScriptException(realm.Intrinsics.SyntaxError, message);
  20. }
  21. [DoesNotReturn]
  22. public static void ThrowSyntaxError(Realm realm, string message, in SourceLocation location)
  23. {
  24. throw new JavaScriptException(realm.Intrinsics.SyntaxError, message).SetJavaScriptLocation(location);
  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(Realm realm, Reference reference)
  38. {
  39. ThrowReferenceNameError(realm, reference?.ReferencedName?.ToString());
  40. }
  41. [DoesNotReturn]
  42. public static void ThrowReferenceNameError(Realm realm, string? name)
  43. {
  44. var message = name != null ? name + " is not defined" : null;
  45. ThrowReferenceError(realm, message);
  46. }
  47. [DoesNotReturn]
  48. public static void ThrowReferenceError(Realm realm, string? message)
  49. {
  50. var location = realm.GlobalObject.Engine.GetLastSyntaxElement()?.Location ?? default;
  51. throw new JavaScriptException(realm.Intrinsics.ReferenceError, message).SetJavaScriptLocation(location);
  52. }
  53. [DoesNotReturn]
  54. public static void ThrowTypeErrorNoEngine(string? message = null, Node? source = null)
  55. {
  56. throw new TypeErrorException(message, source);
  57. }
  58. [DoesNotReturn]
  59. public static void ThrowTypeError(Realm realm, string? message = null)
  60. {
  61. var location = realm.GlobalObject.Engine.GetLastSyntaxElement()?.Location ?? default;
  62. throw new JavaScriptException(realm.Intrinsics.TypeError, message).SetJavaScriptLocation(location);
  63. }
  64. [DoesNotReturn]
  65. public static void ThrowRangeError(Realm realm, string? message = null)
  66. {
  67. var location = realm.GlobalObject.Engine.GetLastSyntaxElement()?.Location ?? default;
  68. throw new JavaScriptException(realm.Intrinsics.RangeError, message).SetJavaScriptLocation(location);
  69. }
  70. public static ErrorDispatchInfo CreateUriError(Realm realm, string message)
  71. {
  72. return new ErrorDispatchInfo(realm.Intrinsics.UriError, message);
  73. }
  74. public static ErrorDispatchInfo CreateRangeError(Realm realm, string message)
  75. {
  76. return new ErrorDispatchInfo(realm.Intrinsics.RangeError, message);
  77. }
  78. [DoesNotReturn]
  79. public static void ThrowNotImplementedException(string? message = null)
  80. {
  81. throw new NotImplementedException(message);
  82. }
  83. [DoesNotReturn]
  84. public static void ThrowArgumentOutOfRangeException(string paramName, string message)
  85. {
  86. throw new ArgumentOutOfRangeException(paramName, message);
  87. }
  88. [DoesNotReturn]
  89. public static void ThrowTimeoutException()
  90. {
  91. throw new TimeoutException();
  92. }
  93. [DoesNotReturn]
  94. public static void ThrowStatementsCountOverflowException()
  95. {
  96. throw new StatementsCountOverflowException();
  97. }
  98. [DoesNotReturn]
  99. public static void ThrowArgumentOutOfRangeException()
  100. {
  101. #pragma warning disable MA0015
  102. throw new ArgumentOutOfRangeException();
  103. #pragma warning restore MA0015
  104. }
  105. [DoesNotReturn]
  106. public static void ThrowNotSupportedException(string? message = null)
  107. {
  108. throw new NotSupportedException(message);
  109. }
  110. [DoesNotReturn]
  111. public static void ThrowInvalidOperationException(string? message = null, Exception? exception = null)
  112. {
  113. throw new InvalidOperationException(message, exception);
  114. }
  115. [DoesNotReturn]
  116. public static void ThrowPromiseRejectedException(JsValue error)
  117. {
  118. throw new PromiseRejectedException(error);
  119. }
  120. [DoesNotReturn]
  121. public static void ThrowJavaScriptException(Engine engine, JsValue value, in Completion result)
  122. {
  123. throw new JavaScriptException(value).SetJavaScriptCallstack(engine, result.Location);
  124. }
  125. [DoesNotReturn]
  126. public static void ThrowJavaScriptException(Engine engine, JsValue value, in SourceLocation location)
  127. {
  128. throw new JavaScriptException(value).SetJavaScriptCallstack(engine, location);
  129. }
  130. [DoesNotReturn]
  131. public static void ThrowJavaScriptException(ErrorConstructor errorConstructor, string message)
  132. {
  133. throw new JavaScriptException(errorConstructor, message);
  134. }
  135. [DoesNotReturn]
  136. public static void ThrowRecursionDepthOverflowException(JintCallStack currentStack)
  137. {
  138. var currentExpressionReference = currentStack.Pop().ToString();
  139. throw new RecursionDepthOverflowException(currentStack, currentExpressionReference);
  140. }
  141. [DoesNotReturn]
  142. public static void ThrowArgumentNullException(string paramName)
  143. {
  144. throw new ArgumentNullException(paramName);
  145. }
  146. [DoesNotReturn]
  147. public static void ThrowMeaningfulException(Engine engine, TargetInvocationException exception)
  148. {
  149. var meaningfulException = exception.InnerException ?? exception;
  150. if (engine.Options.Interop.ExceptionHandler(meaningfulException))
  151. {
  152. ThrowError(engine, meaningfulException.Message);
  153. }
  154. ExceptionDispatchInfo.Capture(meaningfulException).Throw();
  155. #pragma warning disable CS8763
  156. }
  157. #pragma warning restore CS8763
  158. [DoesNotReturn]
  159. internal static void ThrowError(Engine engine, string message)
  160. {
  161. throw new JavaScriptException(engine.Realm.Intrinsics.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. [DoesNotReturn]
  179. public static void ThrowModuleResolutionException(string message, string specifier, string? parent, string? filePath = null)
  180. {
  181. throw new ModuleResolutionException(message, specifier, parent, filePath);
  182. }
  183. }
  184. }