add exception handling
This commit is contained in:
@@ -32,7 +32,7 @@ namespace UptimeSharp.Tests
|
||||
[Fact]
|
||||
public void AddInvalidAlertWithTypeSms()
|
||||
{
|
||||
Assert.Throws<APIException>(() =>
|
||||
Assert.Throws<UptimeSharpException>(() =>
|
||||
{
|
||||
client.AddAlert(Models.AlertType.SMS, "+436601289172");
|
||||
});
|
||||
@@ -42,7 +42,7 @@ namespace UptimeSharp.Tests
|
||||
[Fact]
|
||||
public void AddInvalidAlertWithTypeTwitter()
|
||||
{
|
||||
Assert.Throws<APIException>(() =>
|
||||
Assert.Throws<UptimeSharpException>(() =>
|
||||
{
|
||||
client.AddAlert(Models.AlertType.Twitter, "artistandsocial");
|
||||
});
|
||||
|
||||
@@ -5,6 +5,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UptimeSharp", "UptimeSharp\
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UptimeSharp.Tests", "UptimeSharp.Tests\UptimeSharp.Tests.csproj", "{7AB536B7-1A99-44A7-B5AA-E36E9C7F09F4}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApplication1", "ConsoleApplication1\ConsoleApplication1.csproj", "{2A5CC10E-451C-4978-A9B7-9F2889A18BCF}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -19,6 +21,10 @@ Global
|
||||
{7AB536B7-1A99-44A7-B5AA-E36E9C7F09F4}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{7AB536B7-1A99-44A7-B5AA-E36E9C7F09F4}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{7AB536B7-1A99-44A7-B5AA-E36E9C7F09F4}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{2A5CC10E-451C-4978-A9B7-9F2889A18BCF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{2A5CC10E-451C-4978-A9B7-9F2889A18BCF}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{2A5CC10E-451C-4978-A9B7-9F2889A18BCF}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{2A5CC10E-451C-4978-A9B7-9F2889A18BCF}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
@@ -1,60 +0,0 @@
|
||||
using System;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace UptimeSharp
|
||||
{
|
||||
/// <summary>
|
||||
/// custom UptimeRobot API Exceptions
|
||||
/// </summary>
|
||||
public class APIException : Exception
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the UptimeRobot error code.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The pocket error code.
|
||||
/// </value>
|
||||
public int? UptimeErrorCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the UptimeRobot error.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The pocket error.
|
||||
/// </value>
|
||||
public string UptimeError { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="APIException"/> class.
|
||||
/// </summary>
|
||||
public APIException()
|
||||
: base() { }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="APIException"/> class.
|
||||
/// </summary>
|
||||
/// <param name="message">The message that describes the error.</param>
|
||||
public APIException(string message)
|
||||
: base(message) { }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="APIException"/> class.
|
||||
/// </summary>
|
||||
/// <param name="message">The error message that explains the reason for the exception.</param>
|
||||
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified.</param>
|
||||
public APIException(string message, Exception innerException)
|
||||
: base(message, innerException) { }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="APIException"/> class.
|
||||
/// </summary>
|
||||
/// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo" /> that holds the serialized object data about the exception being thrown.</param>
|
||||
/// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext" /> that contains contextual information about the source or destination.</param>
|
||||
protected APIException(SerializationInfo info, StreamingContext context)
|
||||
: base(info, context) { }
|
||||
}
|
||||
}
|
||||
@@ -47,7 +47,7 @@ namespace UptimeSharp
|
||||
{
|
||||
if (type == AlertType.SMS || type == AlertType.Twitter)
|
||||
{
|
||||
throw new APIException("AlertType.SMS and AlertType.Twitter are not supported by the UptimeRobot API");
|
||||
throw new UptimeSharpException("AlertType.SMS and AlertType.Twitter are not supported by the UptimeRobot API");
|
||||
}
|
||||
|
||||
return Get<DefaultResponse>("newAlertContact",
|
||||
|
||||
@@ -8,6 +8,25 @@ namespace UptimeSharp.Models
|
||||
[DataContract]
|
||||
internal class ResponseBase
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the error code.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The error code.
|
||||
/// </value>
|
||||
[DataMember(Name = "id")]
|
||||
public string ErrorCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the error message.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The error message.
|
||||
/// </value>
|
||||
[DataMember(Name = "message")]
|
||||
public string ErrorMessage { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether this <see cref="ResponseBase"/> is status.
|
||||
/// </summary>
|
||||
|
||||
+43
-27
@@ -2,6 +2,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Reflection;
|
||||
|
||||
namespace UptimeSharp
|
||||
{
|
||||
@@ -76,7 +77,7 @@ namespace UptimeSharp
|
||||
IRestResponse<T> response = _restClient.Execute<T>(request);
|
||||
|
||||
LastRequestData = response;
|
||||
ValidateResponse(response);
|
||||
ValidateResponse<T>(response);
|
||||
|
||||
return response.Data;
|
||||
}
|
||||
@@ -134,7 +135,7 @@ namespace UptimeSharp
|
||||
/// <param name="name">The name.</param>
|
||||
/// <param name="value">The value.</param>
|
||||
/// <returns></returns>
|
||||
public static Parameter Parameter(string name, object value)
|
||||
internal static Parameter Parameter(string name, object value)
|
||||
{
|
||||
return new Parameter()
|
||||
{
|
||||
@@ -150,45 +151,60 @@ namespace UptimeSharp
|
||||
/// </summary>
|
||||
/// <param name="response">The response.</param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="APIException">
|
||||
/// <exception cref="UptimeSharpException">
|
||||
/// Error retrieving response
|
||||
/// </exception>
|
||||
protected void ValidateResponse(IRestResponse response)
|
||||
protected void ValidateResponse<T>(IRestResponse<T> response) where T : new()
|
||||
{
|
||||
if (response.StatusCode != HttpStatusCode.OK)
|
||||
Type responseType = response.Data.GetType();
|
||||
|
||||
// get status property from ResponseBase POCO
|
||||
PropertyInfo statusProp = responseType.GetProperty("Status");
|
||||
object status = statusProp.GetValue(response.Data, null);
|
||||
|
||||
// Error from UptimeSharp
|
||||
if (status.Equals(false))
|
||||
{
|
||||
//Parameter error = response.Headers[1];
|
||||
//Parameter errorCode = response.Headers[2];
|
||||
// get error properties from response
|
||||
PropertyInfo errorProp = responseType.GetProperty("ErrorMessage");
|
||||
object error = errorProp.GetValue(response.Data, null);
|
||||
|
||||
//string exceptionString = response.Content;
|
||||
|
||||
//bool isPocketError = error.Name == "X-Error";
|
||||
|
||||
//// update message to include pocket response data
|
||||
//if (isPocketError)
|
||||
//{
|
||||
// exceptionString = exceptionString + "\nPocketResponse: (" + errorCode.Value + ") " + error.Value;
|
||||
//}
|
||||
PropertyInfo errorCodeProp = responseType.GetProperty("ErrorCode");
|
||||
object errorCode = errorCodeProp.GetValue(response.Data, null);
|
||||
|
||||
// create exception
|
||||
APIException exception = new APIException("Request Exception", response.ErrorException);
|
||||
UptimeSharpException exception = new UptimeSharpException(
|
||||
"UptimeRobot Exception: {0} (Code: {1})\n{2}",
|
||||
response.ErrorException,
|
||||
error,
|
||||
errorCode,
|
||||
"http://uptimerobot.com/api.asp#errorMessages"
|
||||
);
|
||||
|
||||
//if (isPocketError)
|
||||
//{
|
||||
// // add custom pocket fields
|
||||
// exception.PocketError = error.Value.ToString();
|
||||
// exception.PocketErrorCode = Convert.ToInt32(errorCode.Value);
|
||||
// add custom pocket fields
|
||||
exception.Error = error.ToString();
|
||||
exception.ErrorCode = Convert.ToInt32(errorCode);
|
||||
|
||||
// // add to generic exception data
|
||||
// exception.Data.Add(error.Name, error.Value);
|
||||
// exception.Data.Add(errorCode.Name, errorCode.Value);
|
||||
//}
|
||||
// add to generic exception data
|
||||
exception.Data.Add("Error", error);
|
||||
exception.Data.Add("ErrorCode", errorCode);
|
||||
|
||||
throw exception;
|
||||
}
|
||||
// HTTP Request Error
|
||||
else if (response.StatusCode != HttpStatusCode.OK)
|
||||
{
|
||||
throw new UptimeSharpException(
|
||||
"Request Exception: {0} (Code: {1})",
|
||||
response.ErrorException,
|
||||
response.ErrorMessage,
|
||||
response.StatusCode
|
||||
);
|
||||
}
|
||||
// Exception by RestSharp
|
||||
else if (response.ErrorException != null)
|
||||
{
|
||||
throw new APIException("Error retrieving response", response.ErrorException);
|
||||
throw new UptimeSharpException("Error retrieving response", response.ErrorException);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="APIException.cs" />
|
||||
<Compile Include="UptimeSharpException.cs" />
|
||||
<Compile Include="Components\Alert.cs" />
|
||||
<Compile Include="Components\Monitor.cs" />
|
||||
<Compile Include="JsonDeserializer.cs" />
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
using System;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace UptimeSharp
|
||||
{
|
||||
/// <summary>
|
||||
/// custom UptimeRobot API Exceptions
|
||||
/// </summary>
|
||||
public class UptimeSharpException : Exception
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the UptimeRobot error code.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The pocket error code.
|
||||
/// </value>
|
||||
public int? ErrorCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the UptimeRobot error.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The pocket error.
|
||||
/// </value>
|
||||
public string Error { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="UptimeSharpException"/> class.
|
||||
/// </summary>
|
||||
public UptimeSharpException()
|
||||
: base() { }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="UptimeSharpException"/> class.
|
||||
/// </summary>
|
||||
/// <param name="message">The message that describes the error.</param>
|
||||
public UptimeSharpException(string message)
|
||||
: base(message) { }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="UptimeSharpException" /> class.
|
||||
/// </summary>
|
||||
/// <param name="message">The message.</param>
|
||||
/// <param name="args">The arguments.</param>
|
||||
public UptimeSharpException(string message, params object[] args)
|
||||
: base(string.Format(message, args)) { }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="UptimeSharpException"/> class.
|
||||
/// </summary>
|
||||
/// <param name="message">The error message that explains the reason for the exception.</param>
|
||||
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified.</param>
|
||||
public UptimeSharpException(string message, Exception innerException)
|
||||
: base(message, innerException) { }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="UptimeSharpException"/> class.
|
||||
/// </summary>
|
||||
/// <param name="message">The message.</param>
|
||||
/// <param name="innerException">The inner exception.</param>
|
||||
/// <param name="args">The arguments.</param>
|
||||
public UptimeSharpException(string message, Exception innerException, params object[] args)
|
||||
: base(string.Format(message, args), innerException) { }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="UptimeSharpException"/> class.
|
||||
/// </summary>
|
||||
/// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo" /> that holds the serialized object data about the exception being thrown.</param>
|
||||
/// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext" /> that contains contextual information about the source or destination.</param>
|
||||
protected UptimeSharpException(SerializationInfo info, StreamingContext context)
|
||||
: base(info, context) { }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user