I'm creating an "SDK/Proxy" class using HttpClient and would like to pass exceptions thrown by the service the SDK is consuming to the application that is using this SDK. The original exception needs to be preserved unaltered and not wrapped. I've tried EnsureSuccessStatusCode() but it is no good because the original exception is lost to the consumer. Below is SDK code that is attempting to catch exceptions from base service and pass it on to the consumer;
public async Task<string> GetValue(string effect)
{
using (var client = GetHttpClient())
{
HttpResponseMessage resp = await client.GetAsync("api/values?effect=" + effect).ConfigureAwait(false);
if (resp.IsSuccessStatusCode)
{
return await resp.Content.ReadAsStringAsync();
}
throw await resp.Content.ReadAsAsync<Exception>();
}
}
The "consumer" service is using the SDK like so;
public async Task<string> Get(string effect)
{
return await baseSvc.GetValue(effect);
}
When testing this I am getting the following response;
{ Message: "An error has occurred." ExceptionMessage: "Member 'ClassName' was not found." ExceptionType: "System.Runtime.Serialization.SerializationException"...
the base service code throwing the exception is this;
public async Task<string> Get(string effect)
{
switch (effect.ToLower())
{
case "string":
return "this is a string";
case "exception":
throw new ApplicationException("this is an application exception.");
default:
return "effect requested does not exist.";
}
}
Is it possible to "flow" the original unaltered exception from the consumed service through the SDK to the consumer?