Skip to content

Commit

Permalink
Merge pull request #312 from basho/features/lrb/dont-lose-error-messa…
Browse files Browse the repository at this point in the history
…ges-gh-311

Preserve error text.
  • Loading branch information
lukebakken authored Jun 24, 2016
2 parents 19702cd + d0a3a09 commit 58f542d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/ChaosMonkeyApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private static void FetchServerInfo()
var rsp = cmd.Response;
var n = rsp.Value.Node;
var v = rsp.Value.ServerVersion;
Console.WriteLine("[ChaosMonkeyApp] got server info: {0}, {1}", n, v);
// Console.WriteLine("[ChaosMonkeyApp] got server info: {0}, {1}", n, v);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/RiakClient/Exceptions/RiakException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public RiakException(string message, Exception innerException)
/// <param name="message">A message that describes the error.</param>
/// <param name="nodeOffline">A flag to mark if the node was offline or unreachable at the time of the error.</param>
public RiakException(int errorCode, string message, bool nodeOffline)
: this(string.Format("Riak returned an error. Code '{0}'. Message: {1}", errorCode, message))
: this(string.Format("Riak returned an error. Code '{0}'. Message: '{1}'", errorCode, message))
{
this.nodeOffline = nodeOffline;
this.errorCode = errorCode;
Expand Down
32 changes: 23 additions & 9 deletions src/RiakClient/RiakCluster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,21 +125,23 @@ public override RiakResult<IEnumerable<TResult>> UseDelayedConnection<TResult>(F
{
if (retryAttempts < 0)
{
return RiakResult<IEnumerable<TResult>>.FromError(ResultCode.NoRetries, "Unable to access a connection on the cluster.", false);
return RiakResult<IEnumerable<TResult>>.FromError(ResultCode.NoRetries, "Unable to access a connection on the cluster (no more retries).", false);
}

if (disposing)
{
return RiakResult<IEnumerable<TResult>>.FromError(ResultCode.ShuttingDown, "System currently shutting down", true);
}

var errorMessages = new List<string>();
var node = loadBalancer.SelectNode();

if (node != null)
{
var result = node.UseDelayedConnection(useFun);
if (!result.IsSuccess)
{
errorMessages.Add(result.ErrorMessage);

if (result.ResultCode == ResultCode.NoConnections)
{
Thread.Sleep(RetryWaitTime);
Expand All @@ -157,7 +159,8 @@ public override RiakResult<IEnumerable<TResult>> UseDelayedConnection<TResult>(F
return result;
}

return RiakResult<IEnumerable<TResult>>.FromError(ResultCode.ClusterOffline, "Unable to access functioning Riak node", true);
string msg = string.Format("Unable to access functioning Riak node, error(s): {0}", string.Join(", ", errorMessages));
return RiakResult<IEnumerable<TResult>>.FromError(ResultCode.ClusterOffline, msg, true);
}

protected override void Dispose(bool disposing)
Expand Down Expand Up @@ -196,7 +199,7 @@ protected override TRiakResult UseConnection<TRiakResult>(
{
if (retryAttempts < 0)
{
return onError(ResultCode.NoRetries, "Unable to access a connection on the cluster.", false);
return onError(ResultCode.NoRetries, "Unable to access a connection on the cluster (no more retries).", false);
}

if (disposing)
Expand All @@ -207,9 +210,12 @@ protected override TRiakResult UseConnection<TRiakResult>(
var node = loadBalancer.SelectNode();
if (node != null)
{
var errorMessages = new List<string>();
var result = node.UseConnection(useFun);
if (!result.IsSuccess)
{
errorMessages.Add(result.ErrorMessage);

TRiakResult nextResult = null;
if (result.ResultCode == ResultCode.NoConnections)
{
Expand All @@ -223,21 +229,29 @@ protected override TRiakResult UseConnection<TRiakResult>(
nextResult = UseConnection(useFun, onError, retryAttempts - 1);
}

// if the next result is successful then return that
if (nextResult != null && nextResult.IsSuccess)
if (nextResult != null)
{
return nextResult;
// if the next result is successful then return that
if (nextResult.IsSuccess)
{
return nextResult;
}
else
{
errorMessages.Add(nextResult.ErrorMessage);
}
}

// otherwise we'll return the result that we had at this call to make sure that
// the correct/initial error is shown
return onError(result.ResultCode, result.ErrorMessage, result.NodeOffline);
string errorMessage = string.Join(", ", errorMessages);
return onError(result.ResultCode, errorMessage, result.NodeOffline);
}

return (TRiakResult)result;
}

return onError(ResultCode.ClusterOffline, "Unable to access functioning Riak node", true);
return onError(ResultCode.ClusterOffline, "Unable to access functioning Riak node (load balancer returned no nodes).", true);
}

private void MaybeDeactivateNode(bool nodeOffline, IRiakNode node)
Expand Down

0 comments on commit 58f542d

Please sign in to comment.