Преглед изворни кода

Improved exception handling for Posting

Kenric Nugteren пре 1 година
родитељ
комит
68a48f755c
1 измењених фајлова са 18 додато и 12 уклоњено
  1. 18 12
      InABox.Core/Postable/PosterEngine.cs

+ 18 - 12
InABox.Core/Postable/PosterEngine.cs

@@ -103,6 +103,16 @@ namespace InABox.Core
         /// <param name="model"></param>
         public abstract void AfterPost(IDataModel<TPostable> model);
 
+        private static void SetFailed(IList<TPostable> entities)
+        {
+            foreach (var post in entities)
+            {
+                post.PostedStatus = PostedStatus.PostFailed;
+                post.PostedNote = "Post failed.";
+            }
+            new Client<TPostable>().Save(entities, "Post failed by user.");
+        }
+
         public bool Process(IDataModel<TPostable> model)
         {
             if (!BeforePost(model))
@@ -144,26 +154,22 @@ namespace InABox.Core
                 }
                 else
                 {
-                    foreach (var post in entities)
-                    {
-                        post.PostedStatus = PostedStatus.PostFailed;
-                        post.PostedNote = "Post failed.";
-                    }
-                    new Client<TPostable>().Save(entities, "Post failed by user.");
+                    SetFailed(entities);
                 }
                 return success;
             }
+            catch (PostCancelledException)
+            {
+                var entities = data.ToObjects<TPostable>().ToList();
+                SetFailed(entities);
+                throw;
+            }
             catch(Exception e)
             {
                 Logger.Send(LogType.Error, "", $"Post Failed: {CoreUtils.FormatException(e)}");
 
                 var entities = data.ToObjects<TPostable>().ToList();
-                foreach (var post in entities)
-                {
-                    post.PostedStatus = PostedStatus.PostFailed;
-                    post.PostedNote = e.Message;
-                }
-                new Client<TPostable>().Save(entities, "Post failed by user.");
+                SetFailed(entities);
                 throw;
             }
         }