diff options
Diffstat (limited to 'BackEnd/Timeline/Services/Data/DataManagerExtensions.cs')
-rw-r--r-- | BackEnd/Timeline/Services/Data/DataManagerExtensions.cs | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/BackEnd/Timeline/Services/Data/DataManagerExtensions.cs b/BackEnd/Timeline/Services/Data/DataManagerExtensions.cs new file mode 100644 index 00000000..d149f3fa --- /dev/null +++ b/BackEnd/Timeline/Services/Data/DataManagerExtensions.cs @@ -0,0 +1,27 @@ +using System;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace Timeline.Services.Data
+{
+ public static class DataManagerExtensions
+ {
+ /// <summary>
+ /// Try to get an entry and throw <see cref="DatabaseCorruptedException"/> if not exist.
+ /// </summary>
+ public static async Task<byte[]> GetEntryAndCheck(this IDataManager dataManager, string tag, string notExistMessage, CancellationToken cancellationToken = default)
+ {
+ if (dataManager is null)
+ throw new ArgumentNullException(nameof(dataManager));
+ if (tag is null)
+ throw new ArgumentNullException(nameof(tag));
+ if (notExistMessage is null)
+ throw new ArgumentNullException(nameof(notExistMessage));
+
+ var data = await dataManager.GetEntryAsync(tag, cancellationToken);
+ if (data is null)
+ throw new DatabaseCorruptedException(string.Format(Resource.GetEntryAndCheckNotExist, tag, notExistMessage));
+ return data;
+ }
+ }
+}
|