< Summary

Class:Azure.Identity.CredentialDiagnosticScope
Assembly:Azure.Identity
File(s):C:\Git\azure-sdk-for-net\sdk\identity\Azure.Identity\src\CredentialDiagnosticScope.cs
Covered lines:28
Uncovered lines:0
Coverable lines:28
Total lines:82
Line coverage:100% (28 of 28)
Covered branches:10
Total branches:10
Branch coverage:100% (10 of 10)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-100%100%
Start()-100%100%
Succeeded(...)-100%100%
FailWrapAndThrow(...)-100%100%
RegisterFailed(...)-100%100%
TryWrapException(...)-100%100%
Dispose()-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\identity\Azure.Identity\src\CredentialDiagnosticScope.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Linq;
 6using System.Runtime.ExceptionServices;
 7using Azure.Core;
 8using Azure.Core.Pipeline;
 9
 10namespace Azure.Identity
 11{
 12    internal readonly struct CredentialDiagnosticScope : IDisposable
 13    {
 14        private readonly string _name;
 15        private readonly DiagnosticScope _scope;
 16        private readonly TokenRequestContext _context;
 17        private readonly IScopeHandler _scopeHandler;
 18
 19        public CredentialDiagnosticScope(string name, TokenRequestContext context, IScopeHandler scopeHandler)
 20        {
 135021            _name = name;
 135022            _scope = scopeHandler.CreateScope(name);
 134623            _context = context;
 134624            _scopeHandler = scopeHandler;
 134625        }
 26
 27        public void Start()
 28        {
 134629            AzureIdentityEventSource.Singleton.GetToken(_name, _context);
 134630            _scopeHandler.Start(_name, _scope);
 134631        }
 32
 33        public AccessToken Succeeded(AccessToken token)
 34        {
 42835            AzureIdentityEventSource.Singleton.GetTokenSucceeded(_name, _context, token.ExpiresOn);
 42836            return token;
 37        }
 38
 39        public Exception FailWrapAndThrow(Exception ex)
 40        {
 91841            var wrapped = TryWrapException(ref ex);
 91842            RegisterFailed(ex);
 43
 91844            if (!wrapped)
 45            {
 73846                ExceptionDispatchInfo.Capture(ex).Throw();
 47            }
 48
 18049            throw ex;
 50        }
 51
 52        private void RegisterFailed(Exception ex)
 53        {
 91854            AzureIdentityEventSource.Singleton.GetTokenFailed(_name, _context, ex);
 91855            _scopeHandler.Fail(_name, _scope, ex);
 91856        }
 57
 58        private bool TryWrapException(ref Exception exception)
 59        {
 91860            if (exception is OperationCanceledException || exception is AuthenticationFailedException)
 61            {
 73862                return false;
 63            }
 64
 18065            if (exception is AggregateException aex)
 66            {
 467                CredentialUnavailableException firstCredentialUnavailable = aex.Flatten().InnerExceptions.OfType<Credent
 468                if (firstCredentialUnavailable != default)
 69                {
 470                    exception = new CredentialUnavailableException(firstCredentialUnavailable.Message, aex);
 471                    return true;
 72                }
 73            }
 74
 17675            exception = new AuthenticationFailedException($"{_name.Substring(0, _name.IndexOf('.'))} authentication fail
 17676            return true;
 77
 78        }
 79
 134680        public void Dispose() => _scopeHandler.Dispose(_name, _scope);
 81    }
 82}