ExceptionHelper.cs 5.3 KB

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