I am seeing some strange behavior in my try/catch-rethrow code blocks.
I have a method which instantiates a class of ClientGet and gets the Result value from that class.
public Task<T> Get<T>(ClientRequest<T> Request)
{
try
{
return new ClientGet<T>(UpdateRequest(Request)).Result;
}
catch (Exception)
{
throw;
}
}
The ClientGet class looks like this:
public class ClientGet<T> : IClientAction<T>
{
public Task<T> Result { get; set; }
public ClientGet(ClientRequest<T> request)
{
try
{
Result = GetResult(request);
}
catch (Exception)
{
throw;
}
}
private async Task<T> GetResult(ClientRequest<T> request)
{
if (string.IsNullOrEmpty(request.FullPath))
{
throw new ApplicationException("You must specify a path in your request.");
}
try
{
using (HttpClient client = new HttpClient())
{
if (!string.IsNullOrEmpty(request.Settings.Token)) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", request.Settings.Token); }
var response = await client.GetAsync(request.FullPath);
if (response.IsSuccessStatusCode)
{
return JsonConvert.DeserializeObject<T>(await response.Content.ReadAsStringAsync());
}
throw new HttpRequestException((int)response.StatusCode + ": " + response.ReasonPhrase);
}
}
catch (Exception)
{
throw;
}
}
}
If the response.IsSuccessStatusCode is false, for some reason the code stops at the catch of my GetResult method and posts it as an ASP.NET Unhandled Exception error on my throw; line. I have all of the re-throws so that the errors can be passed back up to the original caller at a higher level of the application.
Here is the exact message I get back:
401: Unauthorized Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Net.Http.HttpRequestException: 401: Unauthorized
Source Error:
Line 45: catch (Exception)
Line 46: {
Line 47: throw;
Line 48: }
Line 49: }