Skip to content

Commit

Permalink
more verbose error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
nleach999 committed Sep 22, 2021
1 parent a5ae985 commit d043b50
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion CxRestClient/Utility/WebOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -121,7 +122,7 @@ private static T ExecuteOperation<T>(Func<String, CxRestClient.IO.CxRestClient>

inRecovery = true;

nonRecoveryException = new UnrecoverableOperationException($"Last exception caught", ex);
nonRecoveryException = new UnrecoverableOperationException($"Last exception caught: {ex.GetType().Name}: {ex.Message}");

if (exceptionErrorLogic != null && !exceptionErrorLogic(ex))
throw ex;
Expand All @@ -131,11 +132,32 @@ private static T ExecuteOperation<T>(Func<String, CxRestClient.IO.CxRestClient>
Task.Delay(delay, token).Wait();
delay *= RETRY_DELAY_INCREASE_FACTOR;
}

if (inRecovery)
_log.Debug("Retry time exceeded, while loop exited during recover.");
}

throw nonRecoveryException;
}

private static void LogAggregateException(AggregateException aex)
{
StringBuilder sb = new StringBuilder();

aex.Handle((x) => {

sb.AppendLine("----- EXCEPTION -----");
if (x is UnrecoverableOperationException)
sb.AppendLine(x.Message);
else
sb.AppendLine($"Type: {x.GetType().FullName} Message: {x.Message}");

return true;
});


}

public static T ExecuteGet<T>(Func<String, CxRestClient.IO.CxRestClient> clientFactory, Func<HttpResponseMessage, T> onSuccess,
String url, CxRestContext ctx, CancellationToken token, Func<HttpResponseMessage, Boolean> responseErrorLogic = null,
Func<Exception, Boolean> exceptionErrorLogic = null, String apiVersion = "1.0")
Expand All @@ -158,6 +180,11 @@ public static T ExecuteGet<T>(Func<String, CxRestClient.IO.CxRestClient> clientF
return result;
}
}
catch(AggregateException aex)
{
LogAggregateException(aex);
throw aex;
}
catch (Exception ex)
{
_log.Error($"GET operation failed for [{url}]", ex);
Expand Down Expand Up @@ -198,6 +225,11 @@ public static T ExecutePost<T>(Func<String, CxRestClient.IO.CxRestClient> client
return result;
}
}
catch (AggregateException aex)
{
LogAggregateException(aex);
throw aex;
}
catch (Exception ex)
{
_log.Error($"POST operation failed for [{url}]", ex);
Expand Down

0 comments on commit d043b50

Please sign in to comment.