Commit 3397dcdc authored by Nick Craver's avatar Nick Craver

Cleanup: ExceptionFactory

parent f7a4110e
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization;
using System.Text; using System.Text;
namespace StackExchange.Redis namespace StackExchange.Redis
{ {
internal static class ExceptionFactory internal static class ExceptionFactory
{ {
const string DataCommandKey = "redis-command", private const string
DataServerKey = "redis-server", DataCommandKey = "redis-command",
DataServerEndpoint = "server-endpoint", DataSentStatusKey = "request-sent-status",
DataConnectionState = "connection-state", DataServerKey = "redis-server";
DataLastFailure = "last-failure",
DataLastInnerException = "last-innerexception",
DataSentStatusKey = "request-sent-status";
internal static Exception AdminModeNotEnabled(bool includeDetail, RedisCommand command, Message message, ServerEndPoint server) internal static Exception AdminModeNotEnabled(bool includeDetail, RedisCommand command, Message message, ServerEndPoint server)
{ {
...@@ -24,6 +18,7 @@ internal static Exception AdminModeNotEnabled(bool includeDetail, RedisCommand c ...@@ -24,6 +18,7 @@ internal static Exception AdminModeNotEnabled(bool includeDetail, RedisCommand c
if (includeDetail) AddDetail(ex, message, server, s); if (includeDetail) AddDetail(ex, message, server, s);
return ex; return ex;
} }
internal static Exception CommandDisabled(bool includeDetail, RedisCommand command, Message message, ServerEndPoint server) internal static Exception CommandDisabled(bool includeDetail, RedisCommand command, Message message, ServerEndPoint server)
{ {
string s = GetLabel(includeDetail, command, message); string s = GetLabel(includeDetail, command, message);
...@@ -31,6 +26,7 @@ internal static Exception CommandDisabled(bool includeDetail, RedisCommand comma ...@@ -31,6 +26,7 @@ internal static Exception CommandDisabled(bool includeDetail, RedisCommand comma
if (includeDetail) AddDetail(ex, message, server, s); if (includeDetail) AddDetail(ex, message, server, s);
return ex; return ex;
} }
internal static Exception TooManyArgs(bool includeDetail, string command, Message message, ServerEndPoint server, int required) internal static Exception TooManyArgs(bool includeDetail, string command, Message message, ServerEndPoint server, int required)
{ {
string s = GetLabel(includeDetail, command, message); string s = GetLabel(includeDetail, command, message);
...@@ -38,6 +34,7 @@ internal static Exception TooManyArgs(bool includeDetail, string command, Messag ...@@ -38,6 +34,7 @@ internal static Exception TooManyArgs(bool includeDetail, string command, Messag
if (includeDetail) AddDetail(ex, message, server, s); if (includeDetail) AddDetail(ex, message, server, s);
return ex; return ex;
} }
internal static Exception CommandDisabled(bool includeDetail, string command, Message message, ServerEndPoint server) internal static Exception CommandDisabled(bool includeDetail, string command, Message message, ServerEndPoint server)
{ {
string s = GetLabel(includeDetail, command, message); string s = GetLabel(includeDetail, command, message);
...@@ -106,7 +103,7 @@ internal static string GetInnerMostExceptionMessage(Exception e) ...@@ -106,7 +103,7 @@ internal static string GetInnerMostExceptionMessage(Exception e)
return e.Message; return e.Message;
} }
} }
internal static Exception NoConnectionAvailable(bool includeDetail, bool includePerformanceCounters, RedisCommand command, Message message, ServerEndPoint server, ServerEndPoint[] serverSnapshot) internal static Exception NoConnectionAvailable(bool includeDetail, bool includePerformanceCounters, RedisCommand command, Message message, ServerEndPoint server, ServerEndPoint[] serverSnapshot)
{ {
string commandLabel = GetLabel(includeDetail, command, message); string commandLabel = GetLabel(includeDetail, command, message);
...@@ -135,7 +132,7 @@ internal static Exception NoConnectionAvailable(bool includeDetail, bool include ...@@ -135,7 +132,7 @@ internal static Exception NoConnectionAvailable(bool includeDetail, bool include
#endif #endif
var ex = new RedisConnectionException(ConnectionFailureType.UnableToResolvePhysicalConnection, exceptionmessage.ToString(), innerException, message?.Status ?? CommandStatus.Unknown); var ex = new RedisConnectionException(ConnectionFailureType.UnableToResolvePhysicalConnection, exceptionmessage.ToString(), innerException, message?.Status ?? CommandStatus.Unknown);
if (includeDetail) if (includeDetail)
{ {
AddDetail(ex, message, server, commandLabel); AddDetail(ex, message, server, commandLabel);
...@@ -145,7 +142,7 @@ internal static Exception NoConnectionAvailable(bool includeDetail, bool include ...@@ -145,7 +142,7 @@ internal static Exception NoConnectionAvailable(bool includeDetail, bool include
internal static Exception PopulateInnerExceptions(ServerEndPoint[] serverSnapshot) internal static Exception PopulateInnerExceptions(ServerEndPoint[] serverSnapshot)
{ {
List<Exception> innerExceptions = new List<Exception>(); var innerExceptions = new List<Exception>();
if (serverSnapshot != null) if (serverSnapshot != null)
{ {
if (serverSnapshot.Length > 0 && serverSnapshot[0].Multiplexer.LastException != null) if (serverSnapshot.Length > 0 && serverSnapshot[0].Multiplexer.LastException != null)
...@@ -180,11 +177,11 @@ internal static Exception NotSupported(bool includeDetail, RedisCommand command) ...@@ -180,11 +177,11 @@ internal static Exception NotSupported(bool includeDetail, RedisCommand command)
if (includeDetail) AddDetail(ex, null, null, s); if (includeDetail) AddDetail(ex, null, null, s);
return ex; return ex;
} }
internal static Exception NoCursor(RedisCommand command) internal static Exception NoCursor(RedisCommand command)
{ {
string s = GetLabel(false, command, null); string s = GetLabel(false, command, null);
var ex = new RedisCommandException("Command cannot be used with a cursor: " + s); return new RedisCommandException("Command cannot be used with a cursor: " + s);
return ex;
} }
internal static Exception Timeout(bool includeDetail, string errorMessage, Message message, ServerEndPoint server) internal static Exception Timeout(bool includeDetail, string errorMessage, Message message, ServerEndPoint server)
...@@ -203,17 +200,21 @@ private static void AddDetail(Exception exception, Message message, ServerEndPoi ...@@ -203,17 +200,21 @@ private static void AddDetail(Exception exception, Message message, ServerEndPoi
exception.Data.Add(DataCommandKey, message.CommandAndKey); exception.Data.Add(DataCommandKey, message.CommandAndKey);
exception.Data.Add(DataSentStatusKey, message.Status); exception.Data.Add(DataSentStatusKey, message.Status);
} }
else if (label != null) exception.Data.Add(DataCommandKey, label); else if (label != null)
{
exception.Data.Add(DataCommandKey, label);
}
if (server != null) exception.Data.Add(DataServerKey, Format.ToString(server.EndPoint)); if (server != null) exception.Data.Add(DataServerKey, Format.ToString(server.EndPoint));
} }
} }
static string GetLabel(bool includeDetail, RedisCommand command, Message message) private static string GetLabel(bool includeDetail, RedisCommand command, Message message)
{ {
return message == null ? command.ToString() : (includeDetail ? message.CommandAndKey : message.Command.ToString()); return message == null ? command.ToString() : (includeDetail ? message.CommandAndKey : message.Command.ToString());
} }
static string GetLabel(bool includeDetail, string command, Message message)
private static string GetLabel(bool includeDetail, string command, Message message)
{ {
return message == null ? command : (includeDetail ? message.CommandAndKey : message.Command.ToString()); return message == null ? command : (includeDetail ? message.CommandAndKey : message.Command.ToString());
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment