Browse Source

Fix exception propagation from .NET constructors (#860)

Marko Lahma 4 years ago
parent
commit
54023a32bf
2 changed files with 10 additions and 6 deletions
  1. 8 4
      Jint.Tests/Runtime/InteropTests.cs
  2. 2 2
      Jint/Runtime/Interop/TypeReference.cs

+ 8 - 4
Jint.Tests/Runtime/InteropTests.cs

@@ -1899,7 +1899,7 @@ namespace Jint.Tests.Runtime
             public MemberExceptionTest(bool throwOnCreate)
             {
                 if (throwOnCreate)
-                    throw new InvalidOperationException();
+                    throw new InvalidOperationException("thrown as requested");
             }
 
             public JsValue ThrowingProperty1
@@ -2459,8 +2459,6 @@ namespace Jint.Tests.Runtime
             Assert.Equal(1, engine.Execute("E.b;").GetCompletionValue().AsNumber());
         }
 
-        #region DelegateCanReturnValue
-
         public class TestItem
         {
             public double Cost { get; set; }
@@ -2521,6 +2519,12 @@ namespace Jint.Tests.Runtime
             Assert.Equal(30, engine.Execute("lst.Where(x => x.Name == 'b').Sum(x => x.Age);").GetCompletionValue().AsNumber());
         }
 
-        #endregion
+        [Fact]
+        public void ExceptionFromConstructorShouldPropagate()
+        {
+            _engine.SetValue("Class", TypeReference.CreateTypeReference(_engine, typeof(MemberExceptionTest)));
+            var ex = Assert.Throws<InvalidOperationException>(() => _engine.Execute("new Class(true);"));
+            Assert.Equal("thrown as requested", ex.Message);
+        }
     }
 }

+ 2 - 2
Jint/Runtime/Interop/TypeReference.cs

@@ -94,9 +94,9 @@ namespace Jint.Runtime.Interop
 
                     return result;
                 }
-                catch
+                catch (TargetInvocationException exception)
                 {
-                    // ignore method
+                    ExceptionHelper.ThrowMeaningfulException(_engine, exception);
                 }
             }