9
9
using System . Linq ;
10
10
using System . Linq . Expressions ;
11
11
using System . Reflection ;
12
+ using System . Runtime . ExceptionServices ;
12
13
13
14
namespace CouchDB . Driver
14
15
{
@@ -37,14 +38,22 @@ public override object Execute(Expression expression, bool completeResponse)
37
38
// Remove from the expressions tree all IQueryable methods not supported by CouchDB and put them into the list
38
39
var unsupportedMethodCallExpressions = new List < MethodCallExpression > ( ) ;
39
40
expression = RemoveUnsupportedMethodExpressions ( expression , out var hasUnsupportedMethods , unsupportedMethodCallExpressions ) ;
40
-
41
+
41
42
var body = Translate ( ref expression ) ;
42
43
Type elementType = TypeSystem . GetElementType ( expression . Type ) ;
43
44
44
45
// Create generic GetCouchList method and invoke it, sending the request to CouchDB
45
46
MethodInfo method = typeof ( CouchQueryProvider ) . GetMethod ( nameof ( CouchQueryProvider . GetCouchList ) ) ;
46
47
MethodInfo generic = method . MakeGenericMethod ( elementType ) ;
47
- var result = generic . Invoke ( this , new [ ] { body } ) ;
48
+ object result = null ;
49
+ try
50
+ {
51
+ result = generic . Invoke ( this , new [ ] { body } ) ;
52
+ }
53
+ catch ( TargetInvocationException ex )
54
+ {
55
+ ExceptionDispatchInfo . Capture ( ex . InnerException ) . Throw ( ) ;
56
+ }
48
57
49
58
// If no unsupported methods, return the result
50
59
if ( ! hasUnsupportedMethods )
@@ -73,7 +82,7 @@ private string Translate(ref Expression e)
73
82
}
74
83
75
84
public object GetCouchList < T > ( string body )
76
- {
85
+ {
77
86
FindResult < T > result = _flurlClient
78
87
. Request ( _connectionString )
79
88
. AppendPathSegments ( _db , "_find" )
@@ -82,7 +91,7 @@ public object GetCouchList<T>(string body)
82
91
. SendRequest ( ) ;
83
92
84
93
var couchList = new CouchList < T > ( result . Docs . ToList ( ) , result . Bookmark , result . ExecutionStats ) ;
85
- return couchList ;
94
+ return couchList ;
86
95
}
87
96
88
97
private Expression RemoveUnsupportedMethodExpressions ( Expression expression , out bool hasUnsupportedMethods , IList < MethodCallExpression > unsupportedMethodCallExpressions )
@@ -177,7 +186,7 @@ MethodInfo FindEnumerableMethod()
177
186
throw ;
178
187
}
179
188
}
180
-
189
+
181
190
private object GetArgumentValueFromExpression ( Expression e )
182
191
{
183
192
if ( e is ConstantExpression c )
@@ -190,7 +199,7 @@ private object GetArgumentValueFromExpression(Expression e)
190
199
}
191
200
throw new NotImplementedException ( $ "Expression of type { e . NodeType } not supported.") ;
192
201
}
193
-
202
+
194
203
private static MethodInfo FindEnumerableMinMax ( MethodInfo queryableMethodInfo )
195
204
{
196
205
Type [ ] genericParams = queryableMethodInfo . GetGenericArguments ( ) ;
@@ -203,6 +212,6 @@ private static MethodInfo FindEnumerableMinMax(MethodInfo queryableMethodInfo)
203
212
enumerableMethodInfo . ReturnType == genericParams [ 1 ] ;
204
213
} ) ;
205
214
return finalMethodInfo ;
206
- }
215
+ }
207
216
}
208
217
}
0 commit comments