Skip to content

Commit

Permalink
Handle SerializationException of SerializationInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
Gh0stWalk3r committed Aug 18, 2019
1 parent 00c281e commit 1a0faa4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Collections.Generic;
using System.Runtime.Serialization;

using Extensions;

using Response.Models;

public class BadRequestException : PushCrewException
Expand All @@ -12,13 +14,13 @@ public class BadRequestException : PushCrewException
public BadRequestException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
var statusString = info.GetString("status");
var statusString = info.GetSafeString("status");
if (Enum.TryParse(statusString, true, out Status status))
{
this.Status = status;
}

this.InvalidList = (ICollection<string>)(info.GetValue("invalid_list", typeof(ICollection<string>)) ?? new List<string>());
this.InvalidList = info.GetSafeValue("invalid_list", new List<string>());
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System;
using System.Runtime.Serialization;

using Extensions;

using Response.Models;

public class InternalServerErrorException : PushCrewException
Expand All @@ -11,7 +13,7 @@ public class InternalServerErrorException : PushCrewException
public InternalServerErrorException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
var statusString = info.GetString("status");
var statusString = info.GetSafeString("status");
if (Enum.TryParse(statusString, true, out Status status))
{
this.Status = status;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System;
using System.Runtime.Serialization;

using Extensions;

public class PushCrewException : Exception
{
/// <summary>Initializes a new instance of the <see cref="PushCrewException" /> class with serialized data.</summary>
Expand All @@ -11,7 +13,7 @@ public class PushCrewException : Exception
/// <exception cref="ArgumentNullException">The <paramref name="info" /> parameter is null.</exception>
/// <exception cref="SerializationException">The class name is null or <see cref="Exception.HResult" /> is zero (0).</exception>
public PushCrewException(SerializationInfo info, StreamingContext context)
: base(info.GetString("message"))
: base(info.GetSafeString("message") ?? info.GetSafeString("error"))
{
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace GregsStack.PushCrew.Net.Api.Extensions
{
using System;
using System.Runtime.Serialization;

using Attributes;

public static class SerializationInfoExtension
{
public static T GetSafeValue<T>([ValidatedNotNull] this SerializationInfo info, [ValidatedNotNull] string name, T defaultValue = default)
{
var serializationInfo = info ?? throw new ArgumentNullException(nameof(info));
var elementName = name ?? throw new ArgumentNullException(nameof(name));

try
{
return (T)serializationInfo.GetValue(elementName, typeof(T));
}
catch (SerializationException)
{
return defaultValue;
}
}

public static string GetSafeString([ValidatedNotNull] this SerializationInfo info, [ValidatedNotNull] string name, string defaultValue = default) => info.GetSafeValue(name, defaultValue);
}
}

0 comments on commit 1a0faa4

Please sign in to comment.