Skip to content

Commit

Permalink
Handle ToCid error from IPFS library, log to Error output
Browse files Browse the repository at this point in the history
  • Loading branch information
drasticactions committed Jan 2, 2024
1 parent 7df45c1 commit 1c280a3
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/FishyFlip/ATWebSocketProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ private void HandleMessage(byte[] byteArray)
switch (frameType)
{
case "#commit":
var frameCommit = new FrameCommit(objects[1]);
var frameCommit = new FrameCommit(objects[1], this.logger);

// this.logger?.LogDebug($"FrameBody: {objects[1].ToJSONString()}");
message.Commit = frameCommit;
Expand All @@ -217,7 +217,7 @@ void HandleProgressStatus(CarProgressStatusEvent e)
}
else if (blockObj["sig"] is not null)
{
message.Footer = FrameFooter.FromCBORObject(blockObj);
message.Footer = FrameFooter.FromCBORObject(blockObj, this.logger);
}
else
{
Expand Down
6 changes: 3 additions & 3 deletions src/FishyFlip/Models/ATRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public ATRecord()
[JsonPropertyName("$type")]
public string? Type { get; internal set; }

public static ATRecord? FromCBORObject(CBORObject blockObj)
public static ATRecord? FromCBORObject(CBORObject blockObj, ILogger? logger = default)
{
if (blockObj["$type"] is not null)
{
Expand All @@ -34,11 +34,11 @@ public ATRecord()
case Constants.FeedType.Post:
return new Post(blockObj);
case Constants.FeedType.Like:
return new Like(blockObj);
return new Like(blockObj, logger);
case Constants.FeedType.Generator:
return new FeedGenerator(blockObj);
case Constants.FeedType.Repost:
return new Repost(blockObj);
return new Repost(blockObj, logger);
case Constants.GraphTypes.Follow:
return new Follow(blockObj);
case Constants.GraphTypes.List:
Expand Down
6 changes: 3 additions & 3 deletions src/FishyFlip/Models/FrameCommit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ namespace FishyFlip.Models;

public class FrameCommit
{
public FrameCommit(CBORObject obj)
public FrameCommit(CBORObject obj, ILogger? logger = default)
{
this.Ops = obj["ops"]?.Values.Select(n => new Ops(n)).ToArray();
this.Seq = obj["seq"].AsInt32();
this.Blocks = obj["blocks"]?.GetByteString();
#pragma warning disable CS0618
this.Prev = obj["prev"]?.ToCid();
this.Prev = obj["prev"]?.ToCid(logger);
#pragma warning restore CS0618
this.Rev = obj["rev"]?.ToRawString();
this.Since = obj["since"]?.ToRawString();
this.Commit = obj["commit"].ToCid();
this.Commit = obj["commit"].ToCid(logger);
this.Repo = obj["repo"] is not null ? ATDid.Create(obj["repo"].AsString()) : null;
this.Handle = obj["handle"] is not null ? ATHandle.Create(obj["handle"].AsString()) : null;
this.Rebase = obj["rebase"]?.AsBoolean() ?? false;
Expand Down
10 changes: 5 additions & 5 deletions src/FishyFlip/Models/FrameFooter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ namespace FishyFlip.Models;

public class FrameFooter
{
public FrameFooter(CBORObject obj)
public FrameFooter(CBORObject obj, ILogger? logger = default)
{
this.Did = ATDid.Create(obj["did"].AsString());
this.Version = obj["version"].AsInt32();
this.Prev = obj["prev"].ToCid();
this.Data = obj["data"].ToCid();
this.Prev = obj["prev"].ToCid(logger);
this.Data = obj["data"].ToCid(logger);
this.Sig = obj["sig"].GetByteString();
}

Expand All @@ -28,11 +28,11 @@ public FrameFooter(CBORObject obj)

public byte[] Sig { get; }

public static FrameFooter? FromCBORObject(CBORObject blockObj)
public static FrameFooter? FromCBORObject(CBORObject blockObj, ILogger? logger = default)
{
if (blockObj["sig"] is not null)
{
return new FrameFooter(blockObj);
return new FrameFooter(blockObj, logger);
}

return null;
Expand Down
4 changes: 2 additions & 2 deletions src/FishyFlip/Models/Like.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public Like(Subject? subject, DateTime? createdAt, string? type)
this.CreatedAt = createdAt;
}

public Like(CBORObject obj)
public Like(CBORObject obj, ILogger? logger = default)
{
var cid = obj["subject"]["cid"].ToCid();
var cid = obj["subject"]["cid"].ToCid(logger);
var uri = new ATUri(obj["subject"]["uri"].AsString());
this.Subject = new Subject(cid, uri);
this.CreatedAt = obj["createdAt"].ToDateTime();
Expand Down
4 changes: 2 additions & 2 deletions src/FishyFlip/Models/Repost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public Repost(Cid? cid, ATUri? uri, DateTime? createdAt, string? type)
this.CreatedAt = createdAt;
}

public Repost(CBORObject obj)
public Repost(CBORObject obj, ILogger? logger = default)
{
this.Cid = obj["subject"]["cid"].ToCid();
this.Cid = obj["subject"]["cid"].ToCid(logger);
this.Uri = new ATUri(obj["subject"]["uri"].AsString());
this.CreatedAt = obj["createdAt"].ToDateTime();
this.Type = Constants.FeedType.Repost;
Expand Down
22 changes: 15 additions & 7 deletions src/FishyFlip/Tools/Cbor/CborExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ internal static class CborExtensions
/// CBOR to Cid.
/// </summary>
/// <param name="obj">Nullable CBORobject.</param>
/// <param name="logger">ILogger.</param>
/// <returns>Cid.</returns>
public static Cid? ToCid(this CBORObject? obj)
public static Cid? ToCid(this CBORObject? obj, ILogger? logger = default)
{
if (obj is null)
{
Expand All @@ -26,13 +27,20 @@ internal static class CborExtensions
return null;
}

switch (obj.Type)
try
{
switch (obj.Type)
{
case CBORType.ByteString:
var cid = obj.GetByteString();
return Cid.Read(cid);
case CBORType.TextString:
return Cid.Decode(obj.AsString());
}
}
catch (Exception ex)
{
case CBORType.ByteString:
var cid = obj.GetByteString();
return Cid.Read(cid);
case CBORType.TextString:
return Cid.Decode(obj.AsString());
logger?.LogError(ex, "Failed to convert to Cid.");
}

return null;
Expand Down

0 comments on commit 1c280a3

Please sign in to comment.