From 2b765136bbdc611611634452fce959985828104c Mon Sep 17 00:00:00 2001 From: Nicolas Constant Date: Tue, 19 Jan 2021 22:15:19 -0500 Subject: [PATCH] add better parsing error handling --- .../SubTasks/SendTweetsToInboxTask.cs | 25 ++++++++++++++++--- .../SubTasks/SendTweetsToSharedInboxTask.cs | 24 +++++++++++++++--- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/BirdsiteLive.Pipeline/Processors/SubTasks/SendTweetsToInboxTask.cs b/src/BirdsiteLive.Pipeline/Processors/SubTasks/SendTweetsToInboxTask.cs index 7e0835d..2fc93fe 100644 --- a/src/BirdsiteLive.Pipeline/Processors/SubTasks/SendTweetsToInboxTask.cs +++ b/src/BirdsiteLive.Pipeline/Processors/SubTasks/SendTweetsToInboxTask.cs @@ -7,6 +7,7 @@ using BirdsiteLive.DAL.Contracts; using BirdsiteLive.DAL.Models; using BirdsiteLive.Domain; using BirdsiteLive.Twitter.Models; +using Microsoft.Extensions.Logging; namespace BirdsiteLive.Pipeline.Processors.SubTasks { @@ -20,13 +21,16 @@ namespace BirdsiteLive.Pipeline.Processors.SubTasks private readonly IActivityPubService _activityPubService; private readonly IStatusService _statusService; private readonly IFollowersDal _followersDal; + private readonly ILogger _logger; + #region Ctor - public SendTweetsToInboxTask(IActivityPubService activityPubService, IStatusService statusService, IFollowersDal followersDal) + public SendTweetsToInboxTask(IActivityPubService activityPubService, IStatusService statusService, IFollowersDal followersDal, ILogger logger) { _activityPubService = activityPubService; _statusService = statusService; _followersDal = followersDal; + _logger = logger; } #endregion @@ -46,8 +50,23 @@ namespace BirdsiteLive.Pipeline.Processors.SubTasks { foreach (var tweet in tweetsToSend) { - var note = _statusService.GetStatus(user.Acct, tweet); - await _activityPubService.PostNewNoteActivity(note, user.Acct, tweet.Id.ToString(), follower.Host, inbox); + try + { + var note = _statusService.GetStatus(user.Acct, tweet); + await _activityPubService.PostNewNoteActivity(note, user.Acct, tweet.Id.ToString(), follower.Host, inbox); + } + catch (ArgumentException e) + { + if (e.Message.Contains("Invalid pattern") && e.Message.Contains("at offset")) //Regex exception + { + _logger.LogError(e, "Can't parse {MessageContent} from Tweet {Id}", tweet.MessageContent, tweet.Id); + } + else + { + throw; + } + } + syncStatus = tweet.Id; } } diff --git a/src/BirdsiteLive.Pipeline/Processors/SubTasks/SendTweetsToSharedInboxTask.cs b/src/BirdsiteLive.Pipeline/Processors/SubTasks/SendTweetsToSharedInboxTask.cs index c620910..dcc6aca 100644 --- a/src/BirdsiteLive.Pipeline/Processors/SubTasks/SendTweetsToSharedInboxTask.cs +++ b/src/BirdsiteLive.Pipeline/Processors/SubTasks/SendTweetsToSharedInboxTask.cs @@ -6,6 +6,7 @@ using BirdsiteLive.DAL.Contracts; using BirdsiteLive.DAL.Models; using BirdsiteLive.Domain; using BirdsiteLive.Twitter.Models; +using Microsoft.Extensions.Logging; namespace BirdsiteLive.Pipeline.Processors.SubTasks { @@ -19,13 +20,15 @@ namespace BirdsiteLive.Pipeline.Processors.SubTasks private readonly IStatusService _statusService; private readonly IActivityPubService _activityPubService; private readonly IFollowersDal _followersDal; + private readonly ILogger _logger; #region Ctor - public SendTweetsToSharedInboxTask(IActivityPubService activityPubService, IStatusService statusService, IFollowersDal followersDal) + public SendTweetsToSharedInboxTask(IActivityPubService activityPubService, IStatusService statusService, IFollowersDal followersDal, ILogger logger) { _activityPubService = activityPubService; _statusService = statusService; _followersDal = followersDal; + _logger = logger; } #endregion @@ -47,8 +50,23 @@ namespace BirdsiteLive.Pipeline.Processors.SubTasks { foreach (var tweet in tweetsToSend) { - var note = _statusService.GetStatus(user.Acct, tweet); - await _activityPubService.PostNewNoteActivity(note, user.Acct, tweet.Id.ToString(), host, inbox); + try + { + var note = _statusService.GetStatus(user.Acct, tweet); + await _activityPubService.PostNewNoteActivity(note, user.Acct, tweet.Id.ToString(), host, inbox); + } + catch (ArgumentException e) + { + if (e.Message.Contains("Invalid pattern") && e.Message.Contains("at offset")) //Regex exception + { + _logger.LogError(e, "Can't parse {MessageContent} from Tweet {Id}", tweet.MessageContent, tweet.Id); + } + else + { + throw; + } + } + syncStatus = tweet.Id; } }