From 66630324d795d35db152e853a1489072e7c729de Mon Sep 17 00:00:00 2001 From: Nicolas Constant Date: Wed, 22 Jul 2020 19:27:49 -0400 Subject: [PATCH] handle exception in deserializer --- .../ApDeserializer.cs | 76 ++++++++++--------- 1 file changed, 42 insertions(+), 34 deletions(-) diff --git a/src/BirdsiteLive.ActivityPub/ApDeserializer.cs b/src/BirdsiteLive.ActivityPub/ApDeserializer.cs index 1eaffa8..e577fbb 100644 --- a/src/BirdsiteLive.ActivityPub/ApDeserializer.cs +++ b/src/BirdsiteLive.ActivityPub/ApDeserializer.cs @@ -1,4 +1,5 @@ -using Newtonsoft.Json; +using System; +using Newtonsoft.Json; namespace BirdsiteLive.ActivityPub { @@ -6,41 +7,48 @@ namespace BirdsiteLive.ActivityPub { public static Activity ProcessActivity(string json) { - var activity = JsonConvert.DeserializeObject(json); - switch (activity.type) + try { - case "Follow": - return JsonConvert.DeserializeObject(json); - case "Undo": - var a = JsonConvert.DeserializeObject(json); - if(a.apObject.type == "Follow") - return JsonConvert.DeserializeObject(json); - break; - case "Accept": - var accept = JsonConvert.DeserializeObject(json); - //var acceptType = JsonConvert.DeserializeObject(accept.apObject); - switch ((accept.apObject as dynamic).type.ToString()) - { - case "Follow": - var acceptFollow = new ActivityAcceptFollow() - { - type = accept.type, - id = accept.id, - actor = accept.actor, - context = accept.context, - apObject = new ActivityFollow() + var activity = JsonConvert.DeserializeObject(json); + switch (activity.type) + { + case "Follow": + return JsonConvert.DeserializeObject(json); + case "Undo": + var a = JsonConvert.DeserializeObject(json); + if(a.apObject.type == "Follow") + return JsonConvert.DeserializeObject(json); + break; + case "Accept": + var accept = JsonConvert.DeserializeObject(json); + //var acceptType = JsonConvert.DeserializeObject(accept.apObject); + switch ((accept.apObject as dynamic).type.ToString()) + { + case "Follow": + var acceptFollow = new ActivityAcceptFollow() { - id = (accept.apObject as dynamic).id?.ToString(), - type = (accept.apObject as dynamic).type?.ToString(), - actor = (accept.apObject as dynamic).actor?.ToString(), - context = (accept.apObject as dynamic).context?.ToString(), - apObject = (accept.apObject as dynamic).@object?.ToString() - } - }; - return acceptFollow; - break; - } - break; + type = accept.type, + id = accept.id, + actor = accept.actor, + context = accept.context, + apObject = new ActivityFollow() + { + id = (accept.apObject as dynamic).id?.ToString(), + type = (accept.apObject as dynamic).type?.ToString(), + actor = (accept.apObject as dynamic).actor?.ToString(), + context = (accept.apObject as dynamic).context?.ToString(), + apObject = (accept.apObject as dynamic).@object?.ToString() + } + }; + return acceptFollow; + break; + } + break; + } + } + catch (Exception e) + { + Console.WriteLine(e); } return null;