< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Channels.FaultConverter
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Channels/FaultConverter.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 99
Coverable lines: 99
Total lines: 241
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 72
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
GetDefaultFaultConverter(...)100%110%
TryCreateException(...)0%10100%
TryCreateFaultMessage(...)0%660%
.ctor(...)100%110%
OnTryCreateException(...)0%42420%
OnTryCreateFaultMessage(...)0%14140%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Channels/FaultConverter.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 System.Globalization;
 6
 7namespace CoreWCF.Channels
 8{
 9    public abstract class FaultConverter
 10    {
 11        public static FaultConverter GetDefaultFaultConverter(MessageVersion version)
 12        {
 013            return new DefaultFaultConverter(version);
 14        }
 15
 16        protected abstract bool OnTryCreateException(Message message, MessageFault fault, out Exception exception);
 17        protected abstract bool OnTryCreateFaultMessage(Exception exception, out Message message);
 18
 19        public bool TryCreateException(Message message, MessageFault fault, out Exception exception)
 20        {
 021            if (message == null)
 22            {
 023                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(message));
 24            }
 025            if (fault == null)
 26            {
 027                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(fault));
 28            }
 29
 030            bool created = OnTryCreateException(message, fault, out exception);
 31
 032            if (created)
 33            {
 034                if (exception == null)
 35                {
 036                    string text = SR.Format(SR.FaultConverterDidNotCreateException, GetType().Name);
 037                    Exception error = new InvalidOperationException(text);
 038                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(error);
 39                }
 40            }
 41            else
 42            {
 043                if (exception != null)
 44                {
 045                    string text = SR.Format(SR.FaultConverterCreatedException, GetType().Name);
 046                    Exception error = new InvalidOperationException(text, exception);
 047                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(error);
 48                }
 49            }
 50
 051            return created;
 52        }
 53
 54        public bool TryCreateFaultMessage(Exception exception, out Message message)
 55        {
 056            bool created = OnTryCreateFaultMessage(exception, out message);
 57
 058            if (created)
 59            {
 060                if (message == null)
 61                {
 062                    string text = SR.Format(SR.FaultConverterDidNotCreateFaultMessage, GetType().Name);
 063                    Exception error = new InvalidOperationException(text);
 064                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(error);
 65                }
 66            }
 67            else
 68            {
 069                if (message != null)
 70                {
 071                    string text = SR.Format(SR.FaultConverterCreatedFaultMessage, GetType().Name);
 072                    Exception error = new InvalidOperationException(text);
 073                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(error);
 74                }
 75            }
 76
 077            return created;
 78        }
 79
 80        private class DefaultFaultConverter : FaultConverter
 81        {
 82            private readonly MessageVersion _version;
 83
 084            internal DefaultFaultConverter(MessageVersion version)
 85            {
 086                _version = version;
 087            }
 88
 89            protected override bool OnTryCreateException(Message message, MessageFault fault, out Exception exception)
 90            {
 091                exception = null;
 92
 93                // SOAP MustUnderstand
 094                if (string.Compare(fault.Code.Namespace, _version.Envelope.Namespace, StringComparison.Ordinal) == 0
 095                    && string.Compare(fault.Code.Name, MessageStrings.MustUnderstandFault, StringComparison.Ordinal) == 
 96                {
 097                    exception = new ProtocolException(fault.Reason.GetMatchingTranslation(CultureInfo.CurrentCulture).Te
 098                    return true;
 99                }
 100
 101                bool checkSender;
 102                bool checkReceiver;
 103                FaultCode code;
 104
 0105                if (_version.Envelope == EnvelopeVersion.Soap11)
 106                {
 0107                    checkSender = true;
 0108                    checkReceiver = true;
 0109                    code = fault.Code;
 110                }
 111                else
 112                {
 0113                    checkSender = fault.Code.IsSenderFault;
 0114                    checkReceiver = fault.Code.IsReceiverFault;
 0115                    code = fault.Code.SubCode;
 116                }
 117
 0118                if (code == null)
 119                {
 0120                    return false;
 121                }
 122
 0123                if (code.Namespace == null)
 124                {
 0125                    return false;
 126                }
 127
 0128                if (checkSender)
 129                {
 130                    // WS-Addressing
 0131                    if (string.Compare(code.Namespace, _version.Addressing.Namespace, StringComparison.Ordinal) == 0)
 132                    {
 0133                        if (string.Compare(code.Name, AddressingStrings.ActionNotSupported, StringComparison.Ordinal) ==
 134                        {
 0135                            exception = new ActionNotSupportedException(fault.Reason.GetMatchingTranslation(CultureInfo.
 0136                            return true;
 137                        }
 0138                        else if (string.Compare(code.Name, AddressingStrings.DestinationUnreachable, StringComparison.Or
 139                        {
 0140                            exception = new EndpointNotFoundException(fault.Reason.GetMatchingTranslation(CultureInfo.Cu
 0141                            return true;
 142                        }
 0143                        else if (string.Compare(code.Name, Addressing10Strings.InvalidAddressingHeader, StringComparison
 144                        {
 0145                            if (code.SubCode != null && string.Compare(code.SubCode.Namespace, _version.Addressing.Names
 0146                                string.Compare(code.SubCode.Name, Addressing10Strings.InvalidCardinality, StringComparis
 147                            {
 0148                                exception = new MessageHeaderException(fault.Reason.GetMatchingTranslation(CultureInfo.C
 0149                                return true;
 150                            }
 151                        }
 0152                        else if (_version.Addressing == AddressingVersion.WSAddressing10)
 153                        {
 0154                            if (string.Compare(code.Name, Addressing10Strings.MessageAddressingHeaderRequired, StringCom
 155                            {
 0156                                exception = new MessageHeaderException(fault.Reason.GetMatchingTranslation(CultureInfo.C
 0157                                return true;
 158                            }
 0159                            else if (string.Compare(code.Name, Addressing10Strings.InvalidAddressingHeader, StringCompar
 160                            {
 0161                                exception = new ProtocolException(fault.Reason.GetMatchingTranslation(CultureInfo.Curren
 0162                                return true;
 163                            }
 164                        }
 165                        else
 166                        {
 0167                            if (string.Compare(code.Name, Addressing200408Strings.MessageInformationHeaderRequired, Stri
 168                            {
 0169                                exception = new ProtocolException(fault.Reason.GetMatchingTranslation(CultureInfo.Curren
 0170                                return true;
 171                            }
 0172                            else if (string.Compare(code.Name, Addressing200408Strings.InvalidMessageInformationHeader, 
 173                            {
 0174                                exception = new ProtocolException(fault.Reason.GetMatchingTranslation(CultureInfo.Curren
 0175                                return true;
 176                            }
 177                        }
 178                    }
 179                }
 180
 0181                if (checkReceiver)
 182                {
 183                    // WS-Addressing
 0184                    if (string.Compare(code.Namespace, _version.Addressing.Namespace, StringComparison.Ordinal) == 0)
 185                    {
 0186                        if (string.Compare(code.Name, AddressingStrings.EndpointUnavailable, StringComparison.Ordinal) =
 187                        {
 0188                            exception = new ServerTooBusyException(fault.Reason.GetMatchingTranslation(CultureInfo.Curre
 0189                            return true;
 190                        }
 191                    }
 192                }
 193
 0194                return false;
 195            }
 196
 197            protected override bool OnTryCreateFaultMessage(Exception exception, out Message message)
 198            {
 199                // WSA
 0200                if (_version.Addressing == AddressingVersion.WSAddressing10)
 201                {
 0202                    if (exception is MessageHeaderException)
 203                    {
 0204                        MessageHeaderException mhe = exception as MessageHeaderException;
 0205                        if (mhe.HeaderNamespace == AddressingVersion.WSAddressing10.Namespace)
 206                        {
 0207                            message = mhe.ProvideFault(_version);
 0208                            return true;
 209                        }
 210                    }
 0211                    else if (exception is ActionMismatchAddressingException)
 212                    {
 0213                        ActionMismatchAddressingException amae = exception as ActionMismatchAddressingException;
 0214                        message = amae.ProvideFault(_version);
 0215                        return true;
 216                    }
 217                }
 0218                if (_version.Addressing != AddressingVersion.None)
 219                {
 0220                    if (exception is ActionNotSupportedException)
 221                    {
 0222                        ActionNotSupportedException anse = exception as ActionNotSupportedException;
 0223                        message = anse.ProvideFault(_version);
 0224                        return true;
 225                    }
 226                }
 227
 228                // SOAP
 0229                if (exception is MustUnderstandSoapException)
 230                {
 0231                    MustUnderstandSoapException muse = exception as MustUnderstandSoapException;
 0232                    message = muse.ProvideFault(_version);
 0233                    return true;
 234                }
 235
 0236                message = null;
 0237                return false;
 238            }
 239        }
 240    }
 241}