< Summary

Line coverage
20%
Covered lines: 18
Uncovered lines: 72
Coverable lines: 90
Total lines: 162
Line coverage: 20%
Branch coverage
13%
Covered branches: 4
Total branches: 30
Branch coverage: 13.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor()100%110%
.ctor(...)100%11100%
.ctor(...)50%22100%
.ctor(...)100%110%
.ctor(...)50%22100%
.ctor(...)0%220%
.ctor(...)0%220%
.ctor(...)0%220%
.ctor(...)100%110%
.ctor(...)0%220%
.ctor(...)100%110%
.ctor(...)0%220%
.ctor(...)50%22100%
.ctor(...)0%220%
.ctor(...)100%110%
GetObjectData(...)100%110%
GetErrorMessage(...)10%101028.57%
SetErrorCodeOnError(...)0%220%
Succeeded(...)100%110%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Net.WebSockets/src/System/Net/WebSockets/WebSocketException.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.ComponentModel;
 5using System.Diagnostics.CodeAnalysis;
 6using System.Runtime.InteropServices;
 7using System.Runtime.Serialization;
 8
 9namespace System.Net.WebSockets
 10{
 11    [Serializable]
 12    [System.Runtime.CompilerServices.TypeForwardedFrom("System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c5
 13    public sealed class WebSocketException : Win32Exception
 14    {
 15        private readonly WebSocketError _webSocketErrorCode;
 16
 17        public WebSocketException()
 018            : this(Marshal.GetLastPInvokeError())
 019        {
 020        }
 21
 22        public WebSocketException(WebSocketError error)
 155823            : this(error, GetErrorMessage(error))
 155824        {
 155825        }
 26
 155827        public WebSocketException(WebSocketError error, string? message) : base(message ?? SR.net_WebSockets_Generic)
 155828        {
 155829            _webSocketErrorCode = error;
 155830        }
 31
 32        public WebSocketException(WebSocketError error, Exception? innerException)
 033            : this(error, GetErrorMessage(error), innerException)
 034        {
 035        }
 36
 37        public WebSocketException(WebSocketError error, string? message, Exception? innerException)
 705538            : base(message ?? SR.net_WebSockets_Generic, innerException)
 705539        {
 705540            _webSocketErrorCode = error;
 705541        }
 42
 43        public WebSocketException(int nativeError)
 044            : base(nativeError)
 045        {
 046            _webSocketErrorCode = !Succeeded(nativeError) ? WebSocketError.NativeError : WebSocketError.Success;
 047            SetErrorCodeOnError(nativeError);
 048        }
 49
 50        public WebSocketException(int nativeError, string? message)
 051            : base(nativeError, message)
 052        {
 053            _webSocketErrorCode = !Succeeded(nativeError) ? WebSocketError.NativeError : WebSocketError.Success;
 054            SetErrorCodeOnError(nativeError);
 055        }
 56
 57        public WebSocketException(int nativeError, Exception? innerException)
 058            : base(SR.net_WebSockets_Generic, innerException)
 059        {
 060            _webSocketErrorCode = !Succeeded(nativeError) ? WebSocketError.NativeError : WebSocketError.Success;
 061            SetErrorCodeOnError(nativeError);
 062        }
 63
 64        public WebSocketException(WebSocketError error, int nativeError)
 065            : this(error, nativeError, GetErrorMessage(error))
 066        {
 067        }
 68
 69        public WebSocketException(WebSocketError error, int nativeError, string? message)
 070            : base(message ?? SR.net_WebSockets_Generic)
 071        {
 072            _webSocketErrorCode = error;
 073            SetErrorCodeOnError(nativeError);
 074        }
 75
 76        public WebSocketException(WebSocketError error, int nativeError, Exception? innerException)
 077            : this(error, nativeError, GetErrorMessage(error), innerException)
 078        {
 079        }
 80
 81        public WebSocketException(WebSocketError error, int nativeError, string? message, Exception? innerException)
 082            : base(message ?? SR.net_WebSockets_Generic, innerException)
 083        {
 084            _webSocketErrorCode = error;
 085            SetErrorCodeOnError(nativeError);
 086        }
 87
 88        public WebSocketException(string? message)
 2989            : base(message ?? SR.net_WebSockets_Generic)
 2990        {
 2991        }
 92
 93        public WebSocketException(string? message, Exception? innerException)
 094            : base(message ?? SR.net_WebSockets_Generic, innerException)
 095        {
 096        }
 97
 98        [Obsolete(Obsoletions.LegacyFormatterImplMessage, DiagnosticId = Obsoletions.LegacyFormatterImplDiagId, UrlForma
 99        [EditorBrowsable(EditorBrowsableState.Never)]
 100        private WebSocketException(SerializationInfo serializationInfo, StreamingContext streamingContext)
 0101            : base(serializationInfo, streamingContext)
 0102        {
 0103        }
 104
 105        [Obsolete(Obsoletions.LegacyFormatterImplMessage, DiagnosticId = Obsoletions.LegacyFormatterImplDiagId, UrlForma
 106        [EditorBrowsable(EditorBrowsableState.Never)]
 107        public override void GetObjectData(SerializationInfo info, StreamingContext context)
 0108        {
 0109            base.GetObjectData(info, context);
 0110            info.AddValue(nameof(WebSocketErrorCode), _webSocketErrorCode);
 0111        }
 112
 113        public override int ErrorCode
 114        {
 115            get
 0116            {
 0117                return base.NativeErrorCode;
 0118            }
 119        }
 120
 121        public WebSocketError WebSocketErrorCode
 122        {
 123            get
 0124            {
 0125                return _webSocketErrorCode;
 0126            }
 127        }
 128
 129        private static string GetErrorMessage(WebSocketError error) =>
 130            // Provide a canned message for the error type.
 1558131            error switch
 1558132            {
 0133                WebSocketError.InvalidMessageType => SR.Format(SR.net_WebSockets_InvalidMessageType_Generic,
 0134                       $"{nameof(WebSocket)}.{nameof(WebSocket.CloseAsync)}",
 0135                       $"{nameof(WebSocket)}.{nameof(WebSocket.CloseOutputAsync)}"),
 0136                WebSocketError.Faulted => SR.net_Websockets_WebSocketBaseFaulted,
 0137                WebSocketError.NotAWebSocket => SR.net_WebSockets_NotAWebSocket_Generic,
 0138                WebSocketError.UnsupportedVersion => SR.net_WebSockets_UnsupportedWebSocketVersion_Generic,
 0139                WebSocketError.UnsupportedProtocol => SR.net_WebSockets_UnsupportedProtocol_Generic,
 0140                WebSocketError.HeaderError => SR.net_WebSockets_HeaderError_Generic,
 1558141                WebSocketError.ConnectionClosedPrematurely => SR.net_WebSockets_ConnectionClosedPrematurely_Generic,
 0142                WebSocketError.InvalidState => SR.net_WebSockets_InvalidState_Generic,
 0143                _ => SR.net_WebSockets_Generic,
 1558144            };
 145
 146        // Set the error code only if there is an error (i.e. nativeError >= 0). Otherwise the code fails during deseria
 147        // as the Exception..ctor() throws on setting HResult to 0. The default for HResult is -2147467259.
 148        private void SetErrorCodeOnError(int nativeError)
 0149        {
 0150            if (!Succeeded(nativeError))
 0151            {
 0152                HResult = nativeError;
 0153            }
 0154        }
 155
 156        private static bool Succeeded(int hr)
 0157        {
 0158            return (hr >= 0);
 0159        }
 160    }
 161}
 162