generated from Avanade/avanade-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathCosmosDbInvoker.cs
44 lines (40 loc) · 1.78 KB
/
CosmosDbInvoker.cs
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/CoreEx
using CoreEx.Invokers;
using CoreEx.Results;
using Microsoft.Azure.Cosmos;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace CoreEx.Cosmos
{
/// <summary>
/// Provides the standard <see cref="ICosmosDb"/> invoker functionality.
/// </summary>
/// <remarks>Catches any unhandled <see cref="CosmosException"/> and invokes <see cref="ICosmosDb.HandleCosmosException(CosmosException)"/> to handle before bubbling up.</remarks>
public class CosmosDbInvoker : InvokerBase<ICosmosDb>
{
/// <inheritdoc/>
protected override TResult OnInvoke<TResult>(InvokeArgs invokeArgs, ICosmosDb cosmos, Func<InvokeArgs, TResult> func) => throw new NotSupportedException();
/// <inheritdoc/>
protected async override Task<TResult> OnInvokeAsync<TResult>(InvokeArgs invokeArgs, ICosmosDb cosmos, Func<InvokeArgs, CancellationToken, Task<TResult>> func, CancellationToken cancellationToken)
{
try
{
return await base.OnInvokeAsync(invokeArgs, cosmos, func, cancellationToken).ConfigureAwait(false);
}
catch (CosmosException cex)
{
var eresult = cosmos.HandleCosmosException(cex);
if (eresult.HasValue && eresult.Value.IsFailure && eresult.Value.Error is CoreEx.Abstractions.IExtendedException)
{
var dresult = default(TResult);
if (dresult is IResult dir)
return (TResult)dir.ToFailure(eresult.Value.Error);
else
eresult.Value.ThrowOnError();
}
throw;
}
}
}
}