Skip to content

Commit cc4a434

Browse files
authored
Rethrow exception (#36)
* Rethrow exception * Add test that you get the right kind of exception
1 parent fc0406a commit cc4a434

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ appsettings.config
3434
.vs/
3535
# project rider
3636
.idea
37-
37+
.ionide

src/Isop/Domain/Method.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using System.Linq;
55
using System.ComponentModel.DataAnnotations;
6+
using System.Runtime.ExceptionServices;
67

78
namespace Isop.Domain
89
{
@@ -23,7 +24,12 @@ public Parameter[] GetParameters(){
2324
}
2425
public object Invoke(object instance, object[] values){
2526
if (instance==null) throw new ArgumentNullException(nameof(instance));
26-
return _methodInfo.Invoke(instance, values);
27+
try{
28+
return _methodInfo.Invoke(instance, values);
29+
}catch(TargetInvocationException ex){
30+
ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
31+
return null;
32+
}
2733
}
2834

2935
public IEnumerable<Argument> GetArguments()

src/Tests/ExceptionsTests.cs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Globalization;
2+
using System.IO;
3+
using System.Linq;
4+
using Microsoft.Extensions.DependencyInjection;
5+
using NUnit.Framework;
6+
namespace Isop.Tests
7+
{
8+
[TestFixture]
9+
public class ExceptionsTests
10+
{
11+
public class SpecificException : System.Exception
12+
{
13+
}
14+
15+
[Test]
16+
public void It_can_parse_class_and_method_and_execute()
17+
{
18+
var sc = new ServiceCollection();
19+
sc.AddSingleton(ci => new ObjectController() { OnAction = () => throw new SpecificException() });
20+
21+
var arguments = new Build(sc) { typeof(ObjectController) }
22+
.SetCulture(CultureInfo.InvariantCulture)
23+
.Parse(new[] { "Object", "Action" });
24+
25+
Assert.That(arguments.UnRecognizedArguments.Count(), Is.EqualTo(0));
26+
Assert.Throws<SpecificException>(() =>arguments.Invoke(new StringWriter()));
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)