Skip to content

Commit a4e7da2

Browse files
fredericDelaportehazzik
authored andcommitted
Preserves the stack trace when unwrapping TargetInvocationException (#1443)
* Also follow up to NH-3500 which was only partially fixed. * Fixes #1441
1 parent 9a6b6bc commit a4e7da2

File tree

5 files changed

+18
-17
lines changed

5 files changed

+18
-17
lines changed

src/NHibernate/Async/Linq/DefaultQueryProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ protected virtual async Task<object> ExecuteQueryAsync(NhLinqExpression nhLinqEx
4444
}
4545
catch (TargetInvocationException e)
4646
{
47-
throw e.InnerException;
47+
throw ReflectHelper.UnwrapTargetInvocationException(e);
4848
}
4949
}
5050

src/NHibernate/Intercept/DefaultDynamicLazyFieldInterceptor.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,7 @@ public object Intercept(InvocationInfo info)
5151
}
5252
}
5353

54-
object returnValue;
55-
try
56-
{
57-
returnValue = info.InvokeMethodOnTarget();
58-
}
59-
catch (TargetInvocationException ex)
60-
{
61-
throw ReflectHelper.UnwrapTargetInvocationException(ex);
62-
}
63-
return returnValue;
54+
return info.InvokeMethodOnTarget();
6455
}
6556
}
6657
}

src/NHibernate/Linq/DefaultQueryProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ protected virtual object ExecuteQuery(NhLinqExpression nhLinqExpression, IQuery
211211
}
212212
catch (TargetInvocationException e)
213213
{
214-
throw e.InnerException;
214+
throw ReflectHelper.UnwrapTargetInvocationException(e);
215215
}
216216
}
217217

src/NHibernate/Persister/PersisterFactory.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using NHibernate.Mapping;
77
using NHibernate.Persister.Collection;
88
using NHibernate.Persister.Entity;
9+
using NHibernate.Util;
910

1011
namespace NHibernate.Persister
1112
{
@@ -101,7 +102,7 @@ public static IEntityPersister Create(System.Type persisterClass, PersistentClas
101102
Exception e = tie.InnerException;
102103
if (e is HibernateException)
103104
{
104-
throw e;
105+
throw ReflectHelper.UnwrapTargetInvocationException(tie);
105106
}
106107
else
107108
{
@@ -143,7 +144,7 @@ public static ICollectionPersister Create(System.Type persisterClass, Mapping.Co
143144
Exception e = tie.InnerException;
144145
if (e is HibernateException)
145146
{
146-
throw e;
147+
throw ReflectHelper.UnwrapTargetInvocationException(tie);
147148
}
148149
else
149150
{

src/NHibernate/Proxy/DynamicProxy/InvocationInfo.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
#endregion
88

9-
using System;
109
using System.Diagnostics;
1110
using System.Reflection;
1211
using System.Text;
12+
using NHibernate.Util;
1313

1414
namespace NHibernate.Proxy.DynamicProxy
1515
{
@@ -105,7 +105,16 @@ private string GetMethodName(MethodInfo method)
105105

106106
public virtual object InvokeMethodOnTarget()
107107
{
108-
return _callbackMethod.Invoke(Target, Arguments);
108+
object returnValue;
109+
try
110+
{
111+
returnValue = _callbackMethod.Invoke(Target, Arguments);
112+
}
113+
catch (TargetInvocationException ex)
114+
{
115+
throw ReflectHelper.UnwrapTargetInvocationException(ex);
116+
}
117+
return returnValue;
109118
}
110119
}
111-
}
120+
}

0 commit comments

Comments
 (0)