/// <summary>Builds exception message based on sent command and server's response.</summary> /// <param name="sentCommand">Command sent to the server.</param> /// <param name="response">Server's response.</param> /// <returns>Build exception message.</returns> private static string BuildDefaultMessage(string sentCommand, IWolfResponse response) { StringBuilder builder = new StringBuilder($"{sentCommand} > Server responded with non-success status code: {(int)response.StatusCode} ({response.StatusCode})"); if (response is WolfResponse wolfResponse && wolfResponse.ErrorCode != null) { builder.AppendLine(); builder.Append("Error "); builder.Append((int)wolfResponse.ErrorCode); builder.Append(": "); builder.Append(wolfResponse.ErrorCode.Value.GetDescription(sentCommand)); if (!string.IsNullOrWhiteSpace(wolfResponse.ErrorMessage)) { builder.Append(" - "); builder.Append(wolfResponse.ErrorMessage); } } return(builder.ToString()); }
/// <summary>Creates a new instance of event arguments.</summary> /// <param name="message">Wolf message sent.</param> /// <param name="response">Server's response.</param> public WolfMessageSentEventArgs(IWolfMessage message, IWolfResponse response) : base(message) { this.Response = response; }
/// <summary>Is response a success?</summary> /// <returns>True if response is a success; otherwise false.</returns> public static bool IsSuccess(this IWolfResponse response) { int code = (int)response.StatusCode; return(code >= 200 && code <= 299); }
/// <summary>Is response an error?</summary> /// <returns>True if response is an error; otherwise false.</returns> public static bool IsError(this IWolfResponse response) => !response.IsSuccess();
/// <summary>Creates a new instance of the exception.</summary> /// <param name="sentMessage">Message sent to the server.</param> /// <param name="response">Server's response.</param> public MessageSendingException(IWolfMessage sentMessage, IWolfResponse response) : this(sentMessage, response, BuildDefaultMessage(sentMessage.EventName, response), null) { }
/// <summary>Creates a new instance of the exception.</summary> /// <param name="sentMessage">Message sent to the server.</param> /// <param name="response">Server's response.</param> /// <param name="message">Exception message.</param> public MessageSendingException(IWolfMessage sentMessage, IWolfResponse response, string message) : this(sentMessage, response, message, null) { }
/// <summary>Creates a new instance of the exception.</summary> /// <param name="sentMessage">Message sent to the server.</param> /// <param name="response">Server's response.</param> /// <param name="message">Exception message.</param> /// <param name="innerException">Inner exception.</param> public MessageSendingException(IWolfMessage sentMessage, IWolfResponse response, string message, Exception innerException) : base(message, innerException) { this.SentMessage = sentMessage; this.Response = response; }
/// <summary>Creates a new instance of the exception.</summary> /// <param name="sentMessage">Message sent to the server.</param> /// <param name="response">Server's response.</param> /// <param name="innerException">Inner exception.</param> public MessageSendingException(IWolfMessage sentMessage, IWolfResponse response, Exception innerException) : this(sentMessage, response, BuildDefaultMessage(sentMessage.Command, response), innerException) { }