< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Dispatcher.ErrorBehavior
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Dispatcher/ErrorBehavior.cs
Line coverage
76%
Covered lines: 81
Uncovered lines: 25
Coverable lines: 106
Total lines: 283
Line coverage: 76.4%
Branch coverage
68%
Covered branches: 44
Total branches: 64
Branch coverage: 68.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)50%4487.5%
InitializeFault(...)100%66100%
ProvideMessageFault(...)100%22100%
ProvideMessageFaultCore(...)100%11100%
ProvideFaultOfLastResort(...)70%202094.11%
ProvideMessageFaultCoreCoda(...)100%22100%
ProvideOnlyFaultOfLastResort(...)100%110%
ProvideFault(...)100%22100%
ProvideWellKnownFault(...)37.5%8830%
HandleError(...)100%22100%
HandleErrorCore(...)100%22100%
HandleErrorCommon(...)62.5%8861.53%
HandleError(...)100%110%
HandleError(...)100%11100%
ShouldRethrowExceptionAsIs(...)100%11100%
ShouldRethrowClientSideExceptionAsIs(...)100%110%
ThrowAndCatch(...)37.5%8853.84%
ThrowAndCatch(...)100%11100%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Dispatcher/ErrorBehavior.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System;
 5using CoreWCF.Channels;
 6using CoreWCF.Diagnostics;
 7using CoreWCF.Runtime;
 8
 9namespace CoreWCF.Dispatcher
 10{
 11    internal class ErrorBehavior
 12    {
 13        private readonly bool _debug;
 14        private readonly bool _isOnServer;
 15        private readonly MessageVersion _messageVersion;
 16
 132417        internal ErrorBehavior(ChannelDispatcher channelDispatcher)
 18        {
 132419            if (channelDispatcher?.ErrorHandlers == null)
 20            {
 021                Handlers = EmptyArray<IErrorHandler>.Allocate(0);
 22            }
 23            else
 24            {
 132425                Handlers = EmptyArray<IErrorHandler>.ToArray(channelDispatcher.ErrorHandlers);
 26            }
 132427            _debug = channelDispatcher.IncludeExceptionDetailInFaults;
 28            //isOnServer = channelDispatcher.IsOnServer;
 132429            _isOnServer = true;
 132430            _messageVersion = channelDispatcher.MessageVersion;
 132431        }
 32
 33        private void InitializeFault(MessageRpc rpc)
 34        {
 16335            Exception error = rpc.Error;
 16336            if (error is FaultException fault)
 37            {
 10338                MessageFault messageFault = rpc.Operation.FaultFormatter.Serialize(fault, out string action);
 10339                if (action == null)
 40                {
 3841                    action = rpc.RequestVersion.Addressing.DefaultFaultAction;
 42                }
 43
 10344                if (messageFault != null)
 45                {
 10346                    rpc.FaultInfo.Fault = Message.CreateMessage(rpc.RequestVersion, messageFault, action);
 47                }
 48            }
 16349        }
 50
 36551        internal IErrorHandler[] Handlers { get; }
 52
 53        internal void ProvideMessageFault(MessageRpc rpc)
 54        {
 253855            if (rpc.Error != null)
 56            {
 16357                ProvideMessageFaultCore(rpc);
 58            }
 253859        }
 60
 61        private void ProvideMessageFaultCore(MessageRpc rpc)
 62        {
 16363            if (_messageVersion != rpc.RequestVersion)
 64            {
 65                Fx.Assert("CoreWCF.Dispatcher.ErrorBehavior.ProvideMessageFaultCore(): (this.messageVersion != rpc.Reque
 66            }
 67
 16368            InitializeFault(rpc);
 69
 16370            ProvideFault(rpc.Error, rpc.Channel.GetProperty<FaultConverter>(), ref rpc.FaultInfo);
 71
 16372            ProvideMessageFaultCoreCoda(rpc);
 16373        }
 74
 75        private void ProvideFaultOfLastResort(Exception error, ref ErrorHandlerFaultInfo faultInfo)
 76        {
 18277            if (faultInfo.Fault == null)
 78            {
 7579                FaultCode code = new FaultCode(FaultCodeConstants.Codes.InternalServiceFault, FaultCodeConstants.Namespa
 7580                code = FaultCode.CreateReceiverFaultCode(code);
 7581                string action = FaultCodeConstants.Actions.NetDispatcher;
 82                MessageFault fault;
 7583                if (_debug)
 84                {
 485                    faultInfo.DefaultFaultAction = action;
 486                    fault = MessageFault.CreateFault(code, new FaultReason(error.Message), new ExceptionDetail(error));
 87                }
 88                else
 89                {
 7190                    string reason = _isOnServer ? SR.SFxInternalServerError : SR.SFxInternalCallbackError;
 7191                    fault = MessageFault.CreateFault(code, new FaultReason(reason));
 92                }
 7593                faultInfo.IsConsideredUnhandled = true;
 7594                faultInfo.Fault = Message.CreateMessage(_messageVersion, fault, action);
 95            }
 96            //if this is an InternalServiceFault coming from another service dispatcher we should treat it as unhandled 
 10797            else if (error != null)
 98            {
 10799                if (error is FaultException e && e.Fault != null && e.Fault.Code != null && e.Fault.Code.SubCode != null
 107100                    string.Compare(e.Fault.Code.SubCode.Namespace, FaultCodeConstants.Namespaces.NetDispatch, StringComp
 107101                    string.Compare(e.Fault.Code.SubCode.Name, FaultCodeConstants.Codes.InternalServiceFault, StringCompa
 102                {
 0103                    faultInfo.IsConsideredUnhandled = true;
 104                }
 105            }
 107106        }
 107
 108        private void ProvideMessageFaultCoreCoda(MessageRpc rpc)
 109        {
 163110            if (rpc.FaultInfo.Fault.Headers.Action == null)
 111            {
 32112                rpc.FaultInfo.Fault.Headers.Action = rpc.RequestVersion.Addressing.DefaultFaultAction;
 113            }
 114
 163115            rpc.Reply = rpc.FaultInfo.Fault;
 163116        }
 117
 118        internal void ProvideOnlyFaultOfLastResort(ref MessageRpc rpc)
 119        {
 0120            ProvideFaultOfLastResort(rpc.Error, ref rpc.FaultInfo);
 0121            ProvideMessageFaultCoreCoda(rpc);
 0122        }
 123
 124        internal void ProvideFault(Exception e, FaultConverter faultConverter, ref ErrorHandlerFaultInfo faultInfo)
 125        {
 182126            ProvideWellKnownFault(e, faultConverter, ref faultInfo);
 372127            for (int i = 0; i < Handlers.Length; i++)
 128            {
 4129                Message m = faultInfo.Fault;
 4130                Handlers[i].ProvideFault(e, _messageVersion, ref m);
 4131                faultInfo.Fault = m;
 132                //if (TD.FaultProviderInvokedIsEnabled())
 133                //{
 134                //    TD.FaultProviderInvoked(handlers[i].GetType().FullName, e.Message);
 135                //}
 136            }
 182137            ProvideFaultOfLastResort(e, ref faultInfo);
 182138        }
 139
 140        private void ProvideWellKnownFault(Exception e, FaultConverter faultConverter, ref ErrorHandlerFaultInfo faultIn
 141        {
 182142            if (faultConverter != null && faultConverter.TryCreateFaultMessage(e, out Message faultMessage))
 143            {
 0144                faultInfo.Fault = faultMessage;
 0145                return;
 146            }
 182147            else if (e is NetDispatcherFaultException)
 148            {
 0149                NetDispatcherFaultException ndfe = e as NetDispatcherFaultException;
 0150                if (_debug)
 151                {
 0152                    ExceptionDetail detail = new ExceptionDetail(ndfe);
 0153                    faultInfo.Fault = Message.CreateMessage(_messageVersion, MessageFault.CreateFault(ndfe.Code, ndfe.Re
 154                }
 155                else
 156                {
 0157                    faultInfo.Fault = Message.CreateMessage(_messageVersion, ndfe.CreateMessageFault(), ndfe.Action);
 158                }
 159            }
 182160        }
 161
 162        internal void HandleError(MessageRpc rpc)
 163        {
 2538164            if (rpc.Error != null)
 165            {
 163166                HandleErrorCore(rpc);
 167            }
 2538168        }
 169
 170        private void HandleErrorCore(MessageRpc rpc)
 171        {
 163172            bool handled = HandleErrorCommon(rpc.Error, ref rpc.FaultInfo);
 163173            if (handled)
 174            {
 105175                rpc.Error = null;
 176            }
 163177        }
 178
 179        private bool HandleErrorCommon(Exception error, ref ErrorHandlerFaultInfo faultInfo)
 180        {
 181            bool handled;
 167182            if (faultInfo.Fault != null   // there is a message
 167183                && !faultInfo.IsConsideredUnhandled) // and it's not the internal-server-error one
 184            {
 107185                handled = true;
 186            }
 187            else
 188            {
 60189                handled = false;
 190            }
 191
 192            try
 193            {
 194                //if (TD.ServiceExceptionIsEnabled())
 195                //{
 196                //    TD.ServiceException(null, error.ToString(), error.GetType().FullName);
 197                //}
 342198                for (int i = 0; i < Handlers.Length; i++)
 199                {
 4200                    bool handledByThis = Handlers[i].HandleError(error);
 4201                    handled = handledByThis || handled;
 202                    //if (TD.ErrorHandlerInvokedIsEnabled())
 203                    //{
 204                    //    TD.ErrorHandlerInvoked(handlers[i].GetType().FullName, handledByThis, error.GetType().FullName
 205                    //}
 206                }
 167207            }
 0208            catch (Exception e)
 209            {
 0210                if (Fx.IsFatal(e))
 211                {
 0212                    throw;
 213                }
 0214                throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(e);
 215            }
 167216            return handled;
 217        }
 218
 219        internal bool HandleError(Exception error)
 220        {
 0221            ErrorHandlerFaultInfo faultInfo = new ErrorHandlerFaultInfo(_messageVersion.Addressing.DefaultFaultAction);
 0222            return HandleError(error, ref faultInfo);
 223        }
 224
 225        internal bool HandleError(Exception error, ref ErrorHandlerFaultInfo faultInfo)
 226        {
 4227            return HandleErrorCommon(error, ref faultInfo);
 228        }
 229
 230        internal static bool ShouldRethrowExceptionAsIs(Exception e)
 231        {
 1232            return true;
 233        }
 234
 235        internal static bool ShouldRethrowClientSideExceptionAsIs(Exception e)
 236        {
 0237            return true;
 238        }
 239
 240        // This ensures that people debugging first-chance Exceptions see this Exception,
 241        // and that the Exception shows up in the trace logs.
 242        internal static void ThrowAndCatch(Exception e, Message message)
 243        {
 244            try
 245            {
 2246                if (System.Diagnostics.Debugger.IsAttached)
 247                {
 0248                    if (message == null)
 249                    {
 0250                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(e);
 251                    }
 252                    else
 253                    {
 0254                        throw TraceUtility.ThrowHelperError(e, message);
 255                    }
 256                }
 257                else
 258                {
 2259                    if (message == null)
 260                    {
 2261                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(e);
 262                    }
 263                    else
 264                    {
 0265                        TraceUtility.ThrowHelperError(e, message);
 266                    }
 267                }
 0268            }
 2269            catch (Exception e2)
 270            {
 2271                if (!ReferenceEquals(e, e2))
 272                {
 0273                    throw;
 274                }
 2275            }
 2276        }
 277
 278        internal static void ThrowAndCatch(Exception e)
 279        {
 2280            ThrowAndCatch(e, null);
 2281        }
 282    }
 283}