aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline/Services/Data/DataManagerExtensions.cs
blob: d149f3fa280dbab0e8c0c6b2522c21245ad2b174 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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;
        }
    }
}