Skip to content

Commit

Permalink
Merge pull request #9 from jordansjones/serializable-exceptions
Browse files Browse the repository at this point in the history
Serializable Exceptions
  • Loading branch information
jordansjones authored Aug 24, 2016
2 parents 79748e4 + 1965bcc commit 1a4d858
Show file tree
Hide file tree
Showing 41 changed files with 415 additions and 16 deletions.
3 changes: 3 additions & 0 deletions ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### New in 1.0.3 (Release 2016/08/24)
* Fixed: All Exceptions are now `[Serializable]`

### New in 1.0.2 (Release 2016/08/01)
* New: `IEtcdClient.Cluster.GetHealth()` - Retrieve the health status of the etcd cluster.

Expand Down
1 change: 1 addition & 0 deletions source/Draft/Draft-Net45.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
<Compile Include="Exceptions\ValueRequiredException.cs" />
<Compile Include="Exceptions\WatcherClearedException.cs" />
<Compile Include="Extensions\FormBodyBuilder.Extensions.cs" />
<Compile Include="Extensions\SerializationInfoExtensions.cs" />
<Compile Include="Extensions\TimeToLiveExtensions.cs" />
<Compile Include="Extensions\ValueConverter.RequestExtensions.cs" />
<Compile Include="Extensions\ValueConverter.ResponseExtensions.cs" />
Expand Down
9 changes: 9 additions & 0 deletions source/Draft/Exceptions/BadRequestException.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Permissions;

namespace Draft.Exceptions
{
/// <summary>
/// Represents a general problem with the requst to etcd.
/// </summary>
[Serializable]
public class BadRequestException : EtcdException
{

Expand All @@ -18,6 +21,12 @@ public BadRequestException() {}
/// Initializes a new <see cref="BadRequestException" /> instance with a specified error message.
/// </summary>
public BadRequestException(string message) : base(message) {}

/// <summary>
/// Initializes a new <see cref="BadRequestException" /> instance for use in BCL deserialization
/// </summary>
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
protected BadRequestException(SerializationInfo info, StreamingContext context) : base(info, context) {}

/// <summary>
/// Indicates that this exception is due to a general problem with the request to etcd.
Expand Down
9 changes: 9 additions & 0 deletions source/Draft/Exceptions/ClientInternalException.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Permissions;

namespace Draft.Exceptions
{
/// <summary>
/// Represents an internal client error.
/// </summary>
[Serializable]
public class ClientInternalException : EtcdException
{

Expand All @@ -18,6 +21,12 @@ public ClientInternalException() {}
/// Initializes a new <see cref="ClientInternalException" /> instance with a specified error message.
/// </summary>
public ClientInternalException(string message) : base(message) {}

/// <summary>
/// Initializes a new <see cref="ClientInternalException" /> instance for use in BCL deserialization
/// </summary>
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
protected ClientInternalException(SerializationInfo info, StreamingContext context) : base(info, context) {}

/// <summary>
/// Indicates that this exception is due to an internal client error
Expand Down
9 changes: 9 additions & 0 deletions source/Draft/Exceptions/DirectoryNotEmptyException.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Permissions;

namespace Draft.Exceptions
{
/// <summary>
/// Represents an error where the directory operation failed due to still present children.
/// </summary>
[Serializable]
public class DirectoryNotEmptyException : EtcdException
{

Expand All @@ -18,6 +21,12 @@ public DirectoryNotEmptyException() {}
/// Initializes a new <see cref="DirectoryNotEmptyException" /> instance with a specified error message.
/// </summary>
public DirectoryNotEmptyException(string message) : base(message) {}

/// <summary>
/// Initializes a new <see cref="DirectoryNotEmptyException" /> instance for use in BCL deserialization
/// </summary>
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
protected DirectoryNotEmptyException(SerializationInfo info, StreamingContext context) : base(info, context) {}

/// <summary>
/// Indicates that this exception is due to the passed directory still containing children.
Expand Down
57 changes: 53 additions & 4 deletions source/Draft/Exceptions/EtcdException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,47 @@
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Runtime.Serialization;
using System.Security.Permissions;

using Draft.Responses;

using Newtonsoft.Json;

namespace Draft.Exceptions
{
/// <summary>
/// Base exception that is thrown when etcd returns an error response.
/// </summary>
[ExcludeFromCodeCoverage]
[Serializable]
public abstract class EtcdException : Exception
{

/// <summary>
/// Initializes a new <see cref="EtcdException" /> instance.
/// </summary>
protected EtcdException() {}
protected EtcdException()
{
}

/// <summary>
/// Initializes a new <see cref="EtcdException" /> instance with a specified error message.
/// </summary>
protected EtcdException(string message) : base(message) {}
protected EtcdException(string message) : base(message)
{
}

/// <summary>
/// Initializes a new <see cref="EtcdException" /> instance for use in BCL deserialization
/// </summary>
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
protected EtcdException(SerializationInfo info, StreamingContext context) : base(info, context)
{
info.TryGetString(nameof(EtcdError), x => EtcdError = JsonConvert.DeserializeObject<EtcdError>(x));
info.TryGetString(nameof(EtcdError), x => HttpStatusCode = (HttpStatusCode) Enum.Parse(typeof(HttpStatusCode), x, true));
info.TryGetString(nameof(RequestMethod), x => RequestMethod = new HttpMethod(x));
info.TryGetString(nameof(RequestUrl), x => RequestUrl = x);
}

/// <summary>
/// The parsed etcd error message if available.
Expand Down Expand Up @@ -334,5 +354,34 @@ public virtual bool IsWatcherCleared
/// </summary>
public string RequestUrl { get; internal set; }

/// <inheritdoc />
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
if (info == null)
{
throw new ArgumentNullException(nameof(info));
}

if (EtcdError != null)
{
info.AddValue(nameof(EtcdError), JsonConvert.SerializeObject(EtcdError));
}
if (HttpStatusCode != null)
{
info.AddValue(nameof(HttpStatusCode), HttpStatusCode.Value.ToString());
}
if (RequestMethod != null)
{
info.AddValue(nameof(RequestMethod), RequestMethod.Method);
}
if (RequestUrl != null)
{
info.AddValue(nameof(RequestUrl), RequestUrl);
}

// Must call through to the base class to let it save it's own state
base.GetObjectData(info, context);
}
}
}
}
21 changes: 20 additions & 1 deletion source/Draft/Exceptions/EtcdTimeoutException.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Permissions;

namespace Draft.Exceptions
{
/// <summary>
/// Represents an error due to an HTTP request/response timeout.
/// </summary>
[Serializable]
public class EtcdTimeoutException : EtcdException
{


/// <summary>
/// Initializes a new <see cref="EtcdTimeoutException" /> instance.
/// </summary>
public EtcdTimeoutException() {}

/// <summary>
/// Initializes a new <see cref="EtcdTimeoutException" /> instance with a specified error message.
/// </summary>
public EtcdTimeoutException(string message) : base(message) {}

/// <summary>
/// Initializes a new <see cref="DirectoryNotEmptyException" /> instance for use in BCL deserialization
/// </summary>
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
protected EtcdTimeoutException(SerializationInfo info, StreamingContext context) : base(info, context) {}

/// <summary>
/// Indicates that this exception is due to an HTTP timeout error.
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions source/Draft/Exceptions/EventIndexClearedException.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Permissions;

namespace Draft.Exceptions
{
/// <summary>
/// Represents an error where the event in the requested index is outdated and cleared.
/// </summary>
[Serializable]
public class EventIndexClearedException : EtcdException
{

Expand All @@ -19,6 +22,12 @@ public EventIndexClearedException() {}
/// </summary>
public EventIndexClearedException(string message) : base(message) {}

/// <summary>
/// Initializes a new <see cref="EventIndexClearedException" /> instance for use in BCL deserialization
/// </summary>
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
protected EventIndexClearedException(SerializationInfo info, StreamingContext context) : base(info, context) {}

/// <summary>
/// Indicates that this exception is due to the event in the requested index is outdated and cleared.
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions source/Draft/Exceptions/ExistingPeerAddressException.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Permissions;

namespace Draft.Exceptions
{
/// <summary>
/// Represents an error where the operation's passed peer address value matched an existing peer address in etcd.
/// </summary>
[Serializable]
public class ExistingPeerAddressException : EtcdException
{

Expand All @@ -18,6 +21,12 @@ public ExistingPeerAddressException() {}
/// Initializes a new <see cref="ExistingPeerAddressException" /> instance with a specified error message.
/// </summary>
public ExistingPeerAddressException(string message) : base(message) {}

/// <summary>
/// Initializes a new <see cref="ExistingPeerAddressException" /> instance for use in BCL deserialization
/// </summary>
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
protected ExistingPeerAddressException(SerializationInfo info, StreamingContext context) : base(info, context) {}

/// <summary>
/// Indicates that this exception is due to there being an existing peer address that matches the value passed.
Expand Down
9 changes: 9 additions & 0 deletions source/Draft/Exceptions/HttpConnectionException.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Permissions;

namespace Draft.Exceptions
{
/// <summary>
/// Represents an error with the underlying http client when attempting to connect to an etcd endpoint.
/// </summary>
[Serializable]
public class HttpConnectionException : EtcdException
{

Expand All @@ -18,6 +21,12 @@ public HttpConnectionException() {}
/// Initializes a new <see cref="HttpConnectionException" /> instance with a specified error message.
/// </summary>
public HttpConnectionException(string message) : base(message) {}

/// <summary>
/// Initializes a new <see cref="HttpConnectionException" /> instance for use in BCL deserialization
/// </summary>
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
protected HttpConnectionException(SerializationInfo info, StreamingContext context) : base(info, context) {}

/// <summary>
/// Indicates that this exception is due to an underlying http client connection error.
Expand Down
9 changes: 9 additions & 0 deletions source/Draft/Exceptions/IndexNotANumberException.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Permissions;

namespace Draft.Exceptions
{
/// <summary>
/// Represents an error where etcd was unable parse the passed index value into a number.
/// </summary>
[Serializable]
public class IndexNotANumberException : EtcdException
{

Expand All @@ -18,6 +21,12 @@ public IndexNotANumberException() {}
/// Initializes a new <see cref="IndexNotANumberException" /> instance with a specified error message.
/// </summary>
public IndexNotANumberException(string message) : base(message) {}

/// <summary>
/// Initializes a new <see cref="IndexNotANumberException" /> instance for use in BCL deserialization
/// </summary>
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
protected IndexNotANumberException(SerializationInfo info, StreamingContext context) : base(info, context) {}

/// <summary>
/// Indicates that this exception is due to etcd being unable to parse the passed index value as a number.
Expand Down
9 changes: 9 additions & 0 deletions source/Draft/Exceptions/IndexOrValueRequiredException.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Permissions;

namespace Draft.Exceptions
{
/// <summary>
/// Represents an error where the etcd operation requires an index or value parameter, but neither was provided.
/// </summary>
[Serializable]
public class IndexOrValueRequiredException : EtcdException
{

Expand All @@ -18,6 +21,12 @@ public IndexOrValueRequiredException() {}
/// Initializes a new <see cref="IndexOrValueRequiredException" /> instance with a specified error message.
/// </summary>
public IndexOrValueRequiredException(string message) : base(message) {}

/// <summary>
/// Initializes a new <see cref="IndexOrValueRequiredException" /> instance for use in BCL deserialization
/// </summary>
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
protected IndexOrValueRequiredException(SerializationInfo info, StreamingContext context) : base(info, context) {}

/// <summary>
/// Indicates that this exception is due to the request missing the index or value property.
Expand Down
9 changes: 9 additions & 0 deletions source/Draft/Exceptions/IndexValueMutexException.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Permissions;

namespace Draft.Exceptions
{
/// <summary>
/// Represents an error where the etcd operation requires an index or value parameter, but both was provided.
/// </summary>
[Serializable]
public class IndexValueMutexException : EtcdException
{

Expand All @@ -18,6 +21,12 @@ public IndexValueMutexException() {}
/// Initializes a new <see cref="IndexValueMutexException" /> instance with a specified error message.
/// </summary>
public IndexValueMutexException(string message) : base(message) {}

/// <summary>
/// Initializes a new <see cref="IndexValueMutexException" /> instance for use in BCL deserialization
/// </summary>
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
protected IndexValueMutexException(SerializationInfo info, StreamingContext context) : base(info, context) {}

/// <summary>
/// Indicates that this exception is due to "Index and value cannot both be specified."
Expand Down
Loading

0 comments on commit 1a4d858

Please sign in to comment.