ExceptionHelper.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. [DoesNotReturn]
  12. public static void ThrowSyntaxError(Realm realm, string message = null)
  13. {
  14. throw new JavaScriptException(realm.Intrinsics.SyntaxError, message);
  15. }
  16. [DoesNotReturn]
  17. public static void ThrowArgumentException(string message = null)
  18. {
  19. ThrowArgumentException(message, null);
  20. }
  21. [DoesNotReturn]
  22. public static void ThrowArgumentException(string message, string paramName)
  23. {
  24. throw new ArgumentException(message, paramName);
  25. }
  26. [DoesNotReturn]
  27. public static void ThrowReferenceError(Realm realm, Reference reference)
  28. {
  29. ThrowReferenceError(realm, reference?.GetReferencedName()?.ToString());
  30. }
  31. [DoesNotReturn]
  32. public static void ThrowReferenceError(Realm realm, string name)
  33. {
  34. var message = name != null ? name + " is not defined" : null;
  35. throw new JavaScriptException(realm.Intrinsics.ReferenceError, message);
  36. }
  37. [DoesNotReturn]
  38. public static void ThrowTypeErrorNoEngine(string message = null, Exception exception = null)
  39. {
  40. throw new TypeErrorException(message);
  41. }
  42. [DoesNotReturn]
  43. public static void ThrowTypeError(Realm realm, string message = null, Exception exception = null)
  44. {
  45. throw new JavaScriptException(realm.Intrinsics.TypeError, message, exception);
  46. }
  47. [DoesNotReturn]
  48. public static void ThrowRangeError(Realm realm, string message = null)
  49. {
  50. throw new JavaScriptException(realm.Intrinsics.RangeError, message);
  51. }
  52. [DoesNotReturn]
  53. public static void ThrowUriError(Realm realm)
  54. {
  55. throw new JavaScriptException(realm.Intrinsics.UriError);
  56. }
  57. [DoesNotReturn]
  58. public static void ThrowNotImplementedException(string message = null)
  59. {
  60. throw new NotImplementedException(message);
  61. }
  62. [DoesNotReturn]
  63. public static void ThrowArgumentOutOfRangeException(string paramName, string message)
  64. {
  65. throw new ArgumentOutOfRangeException(paramName, message);
  66. }
  67. [DoesNotReturn]
  68. public static void ThrowTimeoutException()
  69. {
  70. throw new TimeoutException();
  71. }
  72. [DoesNotReturn]
  73. public static void ThrowStatementsCountOverflowException()
  74. {
  75. throw new StatementsCountOverflowException();
  76. }
  77. [DoesNotReturn]
  78. public static void ThrowArgumentOutOfRangeException()
  79. {
  80. throw new ArgumentOutOfRangeException();
  81. }
  82. [DoesNotReturn]
  83. public static void ThrowNotSupportedException(string message = null)
  84. {
  85. throw new NotSupportedException(message);
  86. }
  87. [DoesNotReturn]
  88. public static void ThrowInvalidOperationException(string message = null)
  89. {
  90. throw new InvalidOperationException(message);
  91. }
  92. [DoesNotReturn]
  93. public static void ThrowPromiseRejectedException(JsValue error)
  94. {
  95. throw new PromiseRejectedException(error);
  96. }
  97. [DoesNotReturn]
  98. public static void ThrowJavaScriptException(Engine engine, JsValue value, in Completion result)
  99. {
  100. throw new JavaScriptException(value).SetCallstack(engine, result.Location);
  101. }
  102. [DoesNotReturn]
  103. public static void ThrowRecursionDepthOverflowException(JintCallStack currentStack,
  104. string currentExpressionReference)
  105. {
  106. throw new RecursionDepthOverflowException(currentStack, currentExpressionReference);
  107. }
  108. [DoesNotReturn]
  109. public static void ThrowArgumentNullException(string paramName)
  110. {
  111. throw new ArgumentNullException(paramName);
  112. }
  113. [DoesNotReturn]
  114. public static void ThrowMeaningfulException(Engine engine, TargetInvocationException exception)
  115. {
  116. var meaningfulException = exception.InnerException ?? exception;
  117. var handler = engine.Options._ClrExceptionsHandler;
  118. if (handler != null && handler(meaningfulException))
  119. {
  120. ThrowError(engine, meaningfulException.Message);
  121. }
  122. throw meaningfulException;
  123. }
  124. [DoesNotReturn]
  125. private 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. }