From 7ff2732913bb9970508d8091c44e6bba6ab68319 Mon Sep 17 00:00:00 2001
From: Felipe D'Abrantes <felidabrantes@gmail>
Date: Sat, 22 Apr 2023 00:33:09 +0100
Subject: [PATCH] Identify more errors when updating/deleting documents

---
 .../feed-service/app/utils/MongoConnection.scala   | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/backend-services/feed-service/app/utils/MongoConnection.scala b/backend-services/feed-service/app/utils/MongoConnection.scala
index da1ac5b8..9b4366e1 100644
--- a/backend-services/feed-service/app/utils/MongoConnection.scala
+++ b/backend-services/feed-service/app/utils/MongoConnection.scala
@@ -113,8 +113,16 @@ object MongoConnection {
         val futureResult: Future[UpdateResult] =  collection.updateOne(filter, updates).toFuture()
         
         futureResult.map[Unit]((result: UpdateResult) => {
+            if (result.getMatchedCount == 0) {
+                throw new RuntimeException("No document with the given ID.")
+            }
+
             if (!result.wasAcknowledged()) {
-                throw new RuntimeException("Update was not acknowledged")
+                throw new RuntimeException("Update was not acknowledged.")
+            }
+
+            if (result.getModifiedCount() == 0) {
+                throw new RuntimeException("No document was modified.")
             }
         })
     }
@@ -132,11 +140,11 @@ object MongoConnection {
         
         futureResult.map[Unit]((result: DeleteResult) => {
             if (!result.wasAcknowledged()) {
-                throw new RuntimeException("Delete was not acknowledged")
+                throw new RuntimeException("Delete was not acknowledged.")
             }
 
             if (result.getDeletedCount() == 0) {
-                throw new RuntimeException("Delete was not acknowledged")
+                throw new RuntimeException("No document was deleted.")
             }
         })
     }
-- 
GitLab