< Summary

Information
Class: System.Net.Http.HttpProtocolException
Assembly: System.Net.Http
File(s): D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\HttpProtocolException.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 59
Coverable lines: 59
Total lines: 108
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 39
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%110%
CreateHttp2StreamException(...)100%110%
CreateHttp2ConnectionException(...)0%220%
CreateHttp3StreamException(...)100%110%
CreateHttp3ConnectionException(...)0%220%
GetName(...)0%15150%
GetName(...)0%20200%

File(s)

D:\runner\runtime\src\libraries\System.Net.Http\src\System\Net\Http\HttpProtocolException.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.IO;
 5using System.Net.Quic;
 6
 7namespace System.Net.Http
 8{
 9    /// <summary>
 10    /// The exception thrown when an HTTP/2 or an HTTP/3 protocol error occurs.
 11    /// </summary>
 12    /// <remarks>
 13    /// When calling <see cref="HttpClient"/> or <see cref="SocketsHttpHandler"/> methods, <see cref="HttpProtocolExcept
 14    /// <see cref="HttpRequestException"/> if a protocol error occurs.
 15    /// When calling <see cref="Stream"/> methods on the stream returned by <see cref="HttpContent.ReadAsStream()"/> or
 16    /// <see cref="HttpContent.ReadAsStreamAsync(Threading.CancellationToken)"/>, <see cref="HttpProtocolException"/> ca
 17    /// </remarks>
 18    public sealed class HttpProtocolException : HttpIOException
 19    {
 20        /// <summary>
 21        /// Initializes a new instance of the <see cref="HttpProtocolException"/> class with the specified error code,
 22        /// message, and inner exception.
 23        /// </summary>
 24        /// <param name="errorCode">The HTTP/2 or HTTP/3 error code.</param>
 25        /// <param name="message">The error message that explains the reason for the exception.</param>
 26        /// <param name="innerException">The exception that is the cause of the current exception.</param>
 27        public HttpProtocolException(long errorCode, string message, Exception? innerException)
 028            : base(Http.HttpRequestError.HttpProtocolError, message, innerException)
 029        {
 030            ErrorCode = errorCode;
 031        }
 32
 33        /// <summary>
 34        /// Gets the HTTP/2 or HTTP/3 error code associated with this exception.
 35        /// </summary>
 036        public long ErrorCode { get; }
 37
 38#if !TARGET_BROWSER && !TARGET_WASI
 39        internal static HttpProtocolException CreateHttp2StreamException(Http2ProtocolErrorCode protocolError)
 040        {
 041            string message = SR.Format(SR.net_http_http2_stream_error, GetName(protocolError), ((int)protocolError).ToSt
 042            return new HttpProtocolException((long)protocolError, message, null);
 043        }
 44
 45        internal static HttpProtocolException CreateHttp2ConnectionException(Http2ProtocolErrorCode protocolError, strin
 046        {
 047            message = SR.Format(message ?? SR.net_http_http2_connection_error, GetName(protocolError), ((int)protocolErr
 048            return new HttpProtocolException((long)protocolError, message, null);
 049        }
 50
 51        internal static HttpProtocolException CreateHttp3StreamException(Http3ErrorCode protocolError, QuicException inn
 052        {
 053            string message = SR.Format(SR.net_http_http3_stream_error, GetName(protocolError), ((int)protocolError).ToSt
 054            return new HttpProtocolException((long)protocolError, message, innerException);
 055        }
 56
 57        internal static HttpProtocolException CreateHttp3ConnectionException(Http3ErrorCode protocolError, string? messa
 058        {
 059            message = SR.Format(message ?? SR.net_http_http3_connection_error, GetName(protocolError), ((int)protocolErr
 060            return new HttpProtocolException((long)protocolError, message, null);
 061        }
 62
 63        private static string GetName(Http2ProtocolErrorCode code) =>
 64            // These strings are the names used in the HTTP2 spec and should not be localized.
 065            code switch
 066            {
 067                Http2ProtocolErrorCode.NoError => "NO_ERROR",
 068                Http2ProtocolErrorCode.ProtocolError => "PROTOCOL_ERROR",
 069                Http2ProtocolErrorCode.InternalError => "INTERNAL_ERROR",
 070                Http2ProtocolErrorCode.FlowControlError => "FLOW_CONTROL_ERROR",
 071                Http2ProtocolErrorCode.SettingsTimeout => "SETTINGS_TIMEOUT",
 072                Http2ProtocolErrorCode.StreamClosed => "STREAM_CLOSED",
 073                Http2ProtocolErrorCode.FrameSizeError => "FRAME_SIZE_ERROR",
 074                Http2ProtocolErrorCode.RefusedStream => "REFUSED_STREAM",
 075                Http2ProtocolErrorCode.Cancel => "CANCEL",
 076                Http2ProtocolErrorCode.CompressionError => "COMPRESSION_ERROR",
 077                Http2ProtocolErrorCode.ConnectError => "CONNECT_ERROR",
 078                Http2ProtocolErrorCode.EnhanceYourCalm => "ENHANCE_YOUR_CALM",
 079                Http2ProtocolErrorCode.InadequateSecurity => "INADEQUATE_SECURITY",
 080                Http2ProtocolErrorCode.Http11Required => "HTTP_1_1_REQUIRED",
 081                _ => "(unknown error)",
 082            };
 83
 84        private static string GetName(Http3ErrorCode code) =>
 85            // These strings come from the H3 spec and should not be localized.
 086            code switch
 087            {
 088                Http3ErrorCode.NoError => "H3_NO_ERROR",
 089                Http3ErrorCode.ProtocolError => "H3_GENERAL_PROTOCOL_ERROR",
 090                Http3ErrorCode.InternalError => "H3_INTERNAL_ERROR",
 091                Http3ErrorCode.StreamCreationError => "H3_STREAM_CREATION_ERROR",
 092                Http3ErrorCode.ClosedCriticalStream => "H3_CLOSED_CRITICAL_STREAM",
 093                Http3ErrorCode.UnexpectedFrame => "H3_FRAME_UNEXPECTED",
 094                Http3ErrorCode.FrameError => "H3_FRAME_ERROR",
 095                Http3ErrorCode.ExcessiveLoad => "H3_EXCESSIVE_LOAD",
 096                Http3ErrorCode.IdError => "H3_ID_ERROR",
 097                Http3ErrorCode.SettingsError => "H3_SETTINGS_ERROR",
 098                Http3ErrorCode.MissingSettings => "H3_MISSING_SETTINGS",
 099                Http3ErrorCode.RequestRejected => "H3_REQUEST_REJECTED",
 0100                Http3ErrorCode.RequestCancelled => "H3_REQUEST_CANCELLED",
 0101                Http3ErrorCode.RequestIncomplete => "H3_REQUEST_INCOMPLETE",
 0102                Http3ErrorCode.ConnectError => "H3_CONNECT_ERROR",
 0103                Http3ErrorCode.VersionFallback => "H3_VERSION_FALLBACK",
 0104                _ => "(unknown error)"
 0105            };
 106#endif
 107    }
 108}