Skip to content

Commit

Permalink
FIX: make 'SecretNotFound' exception corectly serializable (#51)
Browse files Browse the repository at this point in the history
* FIX: make 'SecretNotFound' exception corectly serializable

Exceptions requires a empty ctor to be correctly serializable.

* PR-FIX: make sure we add serialization info of custom prop at 'GetObjectData'

* PR-SUG: add 'undefined' as default value for the SecretNotFound.Name
stijnmoreels authored and tomkerkhove committed Jan 11, 2019
1 parent eeecba8 commit 3a513a2
Showing 1 changed file with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Runtime.Serialization;
using GuardNet;

namespace Arcus.Security.Secrets.Core.Exceptions
@@ -9,6 +10,13 @@ namespace Arcus.Security.Secrets.Core.Exceptions
[Serializable]
public class SecretNotFoundException : Exception
{
/// <summary>
/// Creates <see cref="SecretNotFoundException"/>
/// </summary>
public SecretNotFoundException() : base("The secret was not found.")
{
}

/// <summary>
/// Creates <see cref="SecretNotFoundException"/> , using the given name
/// </summary>
@@ -32,9 +40,32 @@ public SecretNotFoundException(string name, Exception innerException) : base($"T
Name = name;
}

/// <summary>
/// Creates <see cref="SecretNotFoundException"/> used for serialization.
/// </summary>
/// <param name="info">The <see cref="T:SerializationInfo"></see> that holds the serialized object data about the exception being thrown.</param>
/// <param name="context">The <see cref="T:StreamingContext"></see> that contains contextual information about the source or destination.</param>
protected SecretNotFoundException(SerializationInfo info, StreamingContext context) : base(info, context)
{
Name = info.GetString(nameof(Name));
}

/// <summary>
/// Name of the missing key
/// </summary>
public string Name { get; }
public string Name { get; } = "undefined";

/// <summary>
/// When overridden in a derived class, sets the <see cref="T:SerializationInfo"></see> with information about the exception.
/// </summary>
/// <param name="info">The <see cref="T:SerializationInfo"></see> that holds the serialized object data about the exception being thrown.</param>
/// <param name="context">The <see cref="T:StreamingContext"></see> that contains contextual information about the source or destination.</param>
/// <exception cref="T:ArgumentNullException">The <paramref name="info">info</paramref> parameter is a null reference (Nothing in Visual Basic).</exception>
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);

info.AddValue(nameof(Name), Name);
}
}
}

0 comments on commit 3a513a2

Please sign in to comment.