Skip to content

Commit

Permalink
Retry query on certain exceptions (fixes #109) (#110)
Browse files Browse the repository at this point in the history
* Retry query on certain exceptions (fixes #109)

* Use conditional access for fault exception properties
  • Loading branch information
tdanner committed Jan 18, 2018
1 parent 23fe07d commit 4d2696f
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions Src/SwqlStudio/ConnectionInfo.cs
Expand Up @@ -196,14 +196,14 @@ public DataTable Query(string swql)

public DataTable Query(string swql, out XmlDocument queryPlan, out XmlDocument queryStats)
{
EnsureConnection();

XmlDocument tmpQueryPlan = null; // can't reference out parameter from closure
XmlDocument tmpQueryStats = null; // can't reference out parameter from closure

DataTable result = DoWithExceptionTranslation(
delegate
{
EnsureConnection();

using (InformationServiceCommand command = new InformationServiceCommand(swql, Connection) { ApplicationTag = "SWQL Studio" })
{
foreach (var param in QueryParameters)
Expand Down Expand Up @@ -233,10 +233,11 @@ public static void DoWithExceptionTranslation(Action action)
});
}

public static T DoWithExceptionTranslation<T>(Func<T> action)
public static T DoWithExceptionTranslation<T>(Func<T> action, bool retryOnConnectionError = true)
{
string msg;
Exception inner;
bool couldRetry = false;

try
{
Expand All @@ -254,28 +255,40 @@ public static T DoWithExceptionTranslation<T>(Func<T> action)
}
catch (FaultException ex)
{
msg = (ex.InnerException != null) ? ex.InnerException.Message : ex.Message;
msg = ex.InnerException?.Message ?? ex.Message;
inner = ex.InnerException ?? ex;
}
catch (MessageSecurityException ex)
{
if (ex.InnerException != null && ex.InnerException is FaultException)
if (ex.InnerException is FaultException fault)
{
msg = (ex.InnerException as FaultException).Message;
inner = ex.InnerException;
msg = fault.Message;
inner = fault;
couldRetry = fault.Code?.SubCode?.Name == "BadContextToken";
}
else
{
msg = ex.Message;
inner = ex;
}
}
catch (CommunicationObjectFaultedException ex)
{
msg = ex.Message;
inner = ex;
couldRetry = true;
}
catch (Exception ex)
{
msg = ex.Message;
inner = ex;
}

if (couldRetry && retryOnConnectionError)
{
return DoWithExceptionTranslation(action, false);
}

throw new ApplicationException(msg, inner);
}

Expand Down

0 comments on commit 4d2696f

Please sign in to comment.