Skip to content

Commit 09dc44b

Browse files
committed
added RethrowKeepingStackTrace to all instance where throw ex; was necessary, add explicit returns everywhere we call RethrowKeepingStackTrace, won't be hit but helps code analysis
1 parent bfb2c1c commit 09dc44b

File tree

6 files changed

+22
-8
lines changed

6 files changed

+22
-8
lines changed

src/Elasticsearch.Net/Connection/RequestHandlers/RequestHandler.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,20 +158,17 @@ private ElasticsearchResponse<T> DoRequest<T>(TransportRequestState<T> requestSt
158158
}
159159
catch (MaxRetryException)
160160
{
161-
//TODO ifdef ExceptionDispatchInfo.Capture(ex).Throw();
162161
throw;
163162
}
164163
catch (ElasticsearchServerException)
165164
{
166-
//TODO ifdef ExceptionDispatchInfo.Capture(ex).Throw();
167165
throw;
168166
}
169167
catch (Exception e)
170168
{
171169
requestState.SeenExceptions.Add(e);
172170
if (maxRetries == 0 && retried == 0)
173171
{
174-
//TODO ifdef ExceptionDispatchInfo.Capture(ex).Throw();
175172
throw;
176173
}
177174
seenError = true;

src/Elasticsearch.Net/Connection/RequestHandlers/RequestHandlerAsync.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,10 @@ private Task<ElasticsearchResponse<T>> HandleStreamResponse<T>(Task<Elasticsearc
204204

205205
//If we are not using any pooling and we see an exception we rethrow
206206
if (!requestState.UsingPooling && t.IsFaulted && t.Exception != null && maxRetries == 0)
207+
{
207208
t.Exception.RethrowKeepingStackTrace();
208-
209+
return null; //won't be hit
210+
}
209211

210212
var retried = requestState.Retried;
211213

@@ -215,7 +217,10 @@ private Task<ElasticsearchResponse<T>> HandleStreamResponse<T>(Task<Elasticsearc
215217

216218
// If the response never recieved a status code and has a caught exception make sure we throw it
217219
if (streamResponse.HttpStatusCode.GetValueOrDefault(-1) <= 0 && streamResponse.OriginalException != null)
220+
{
218221
streamResponse.OriginalException.RethrowKeepingStackTrace();
222+
return null; //won't be hit
223+
}
219224

220225
// If the user explicitly wants a stream return the undisposed stream
221226
if (typeof(Stream).IsAssignableFrom(typeof(T)))

src/Elasticsearch.Net/Domain/Response/ElasticsearchResponse.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ public static Task<ElasticsearchResponse<DynamicDictionary>> WrapAsync(Task<Elas
2929
.ContinueWith(t =>
3030
{
3131
if (t.IsFaulted && t.Exception != null)
32-
throw t.Exception.Flatten().InnerException;
33-
32+
{
33+
t.Exception.Flatten().InnerException.RethrowKeepingStackTrace();
34+
return null; //won't be hit
35+
}
3436
return ToDynamicResponse(t.Result);
3537
});
3638
}

src/Elasticsearch.Net/Extensions/ExceptionExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public static void RethrowKeepingStackTrace(this Exception exception)
1313
{
1414
// In .Net 4.5 it would be simple : ExceptionDispatchInfo.Capture(exception).Throw();
1515
// But as NEST target .Net 4.0 the old internal method must be used
16+
//TODO NEST 2.0 Use ifdef NET45 to call ExceptionDispatchInfo.Capture(exception).Throw();
1617
if (preserveStackTraceMethodInfo.Value != null)
1718
{
1819
preserveStackTraceMethodInfo.Value.Invoke(exception, null);

src/Nest/ElasticClient-Source.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ public Task<T> SourceAsync<T>(Func<SourceDescriptor<T>, SourceDescriptor<T>> get
3333
var response = this.RawDispatch.GetSourceDispatchAsync<T>(pathInfo)
3434
.ContinueWith(t =>
3535
{
36-
if (t.IsFaulted) throw t.Exception.Flatten().InnerException;
36+
if (t.IsFaulted && t.Exception != null)
37+
{
38+
t.Exception.Flatten().InnerException.RethrowKeepingStackTrace();
39+
return null; //won't be hit
40+
}
3741
return t.Result.Response;
3842
});
3943
return response;
@@ -46,7 +50,11 @@ public Task<T> SourceAsync<T>(ISourceRequest sourceRequest) where T : class
4650
var response = this.RawDispatch.GetSourceDispatchAsync<T>(pathInfo)
4751
.ContinueWith(t =>
4852
{
49-
if (t.IsFaulted) throw t.Exception.Flatten().InnerException;
53+
if (t.IsFaulted && t.Exception != null)
54+
{
55+
t.Exception.Flatten().InnerException.RethrowKeepingStackTrace();
56+
return null; //won't be hit
57+
}
5058
return t.Result.Response;
5159
});
5260
return response;

src/Nest/Extensions/ExceptionExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public static void RethrowKeepingStackTrace(this Exception exception)
1313
{
1414
// In .Net 4.5 it would be simple : ExceptionDispatchInfo.Capture(exception).Throw();
1515
// But as NEST target .Net 4.0 the old internal method must be used
16+
//TODO NEST 2.0 Use ifdef NET45 to call ExceptionDispatchInfo.Capture(exception).Throw();
1617
if (preserveStackTraceMethodInfo.Value != null)
1718
{
1819
preserveStackTraceMethodInfo.Value.Invoke(exception, null);

0 commit comments

Comments
 (0)