Skip to content

Commit

Permalink
Wrap more exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
drasticactions committed Nov 6, 2023
1 parent 921b829 commit 5d58e00
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions src/FishyFlip/Tools/Cbor/CborExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace FishyFlip.Tools.Cbor;

/// <summary>
/// CBOR Extensions.
/// </summary>
internal static class CborExtensions
{
/// <summary>
Expand Down Expand Up @@ -35,16 +38,33 @@ internal static class CborExtensions
return null;
}

/// <summary>
/// Cast CBOR to string.
/// </summary>
/// <param name="obj">CBORObject.</param>
/// <returns>string.</returns>
public static string? ToString(this CBORObject obj)
{
if (obj.IsNull)
{
return null;
}

return obj.AsString();
try
{
return obj.AsString();
}
catch
{
return null;
}
}

/// <summary>
/// Cast CBOR to DateTime.
/// </summary>
/// <param name="obj">CBORObject.</param>
/// <returns>DateTime.</returns>
public static DateTime? ToDateTime(this CBORObject obj)
{
if (obj.IsNull)
Expand All @@ -57,12 +77,30 @@ internal static class CborExtensions
return null;
}

return DateTime.Parse(obj.AsString());
var time = obj.ToString();
if (time is null)
{
return null;
}

try
{
return DateTime.Parse(time);
}
catch
{
return null;
}
}

/// <summary>
/// Cast CBOR to Embed.
/// </summary>
/// <param name="obj">CBORObject.</param>
/// <returns>Embed.</returns>
public static Embed ToEmbed(this CBORObject obj)
{
var type = obj["$type"].AsString();
var type = obj["$type"].ToString() ?? string.Empty;
switch (type)
{
case Constants.EmbedTypes.Record:
Expand Down

0 comments on commit 5d58e00

Please sign in to comment.