| | | 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 | | |
| | | 4 | | using System.ComponentModel; |
| | | 5 | | using System.Diagnostics.CodeAnalysis; |
| | | 6 | | using System.Runtime.InteropServices; |
| | | 7 | | using System.Runtime.Serialization; |
| | | 8 | | |
| | | 9 | | namespace 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() |
| | 0 | 18 | | : this(Marshal.GetLastPInvokeError()) |
| | 0 | 19 | | { |
| | 0 | 20 | | } |
| | | 21 | | |
| | | 22 | | public WebSocketException(WebSocketError error) |
| | 1558 | 23 | | : this(error, GetErrorMessage(error)) |
| | 1558 | 24 | | { |
| | 1558 | 25 | | } |
| | | 26 | | |
| | 1558 | 27 | | public WebSocketException(WebSocketError error, string? message) : base(message ?? SR.net_WebSockets_Generic) |
| | 1558 | 28 | | { |
| | 1558 | 29 | | _webSocketErrorCode = error; |
| | 1558 | 30 | | } |
| | | 31 | | |
| | | 32 | | public WebSocketException(WebSocketError error, Exception? innerException) |
| | 0 | 33 | | : this(error, GetErrorMessage(error), innerException) |
| | 0 | 34 | | { |
| | 0 | 35 | | } |
| | | 36 | | |
| | | 37 | | public WebSocketException(WebSocketError error, string? message, Exception? innerException) |
| | 7055 | 38 | | : base(message ?? SR.net_WebSockets_Generic, innerException) |
| | 7055 | 39 | | { |
| | 7055 | 40 | | _webSocketErrorCode = error; |
| | 7055 | 41 | | } |
| | | 42 | | |
| | | 43 | | public WebSocketException(int nativeError) |
| | 0 | 44 | | : base(nativeError) |
| | 0 | 45 | | { |
| | 0 | 46 | | _webSocketErrorCode = !Succeeded(nativeError) ? WebSocketError.NativeError : WebSocketError.Success; |
| | 0 | 47 | | SetErrorCodeOnError(nativeError); |
| | 0 | 48 | | } |
| | | 49 | | |
| | | 50 | | public WebSocketException(int nativeError, string? message) |
| | 0 | 51 | | : base(nativeError, message) |
| | 0 | 52 | | { |
| | 0 | 53 | | _webSocketErrorCode = !Succeeded(nativeError) ? WebSocketError.NativeError : WebSocketError.Success; |
| | 0 | 54 | | SetErrorCodeOnError(nativeError); |
| | 0 | 55 | | } |
| | | 56 | | |
| | | 57 | | public WebSocketException(int nativeError, Exception? innerException) |
| | 0 | 58 | | : base(SR.net_WebSockets_Generic, innerException) |
| | 0 | 59 | | { |
| | 0 | 60 | | _webSocketErrorCode = !Succeeded(nativeError) ? WebSocketError.NativeError : WebSocketError.Success; |
| | 0 | 61 | | SetErrorCodeOnError(nativeError); |
| | 0 | 62 | | } |
| | | 63 | | |
| | | 64 | | public WebSocketException(WebSocketError error, int nativeError) |
| | 0 | 65 | | : this(error, nativeError, GetErrorMessage(error)) |
| | 0 | 66 | | { |
| | 0 | 67 | | } |
| | | 68 | | |
| | | 69 | | public WebSocketException(WebSocketError error, int nativeError, string? message) |
| | 0 | 70 | | : base(message ?? SR.net_WebSockets_Generic) |
| | 0 | 71 | | { |
| | 0 | 72 | | _webSocketErrorCode = error; |
| | 0 | 73 | | SetErrorCodeOnError(nativeError); |
| | 0 | 74 | | } |
| | | 75 | | |
| | | 76 | | public WebSocketException(WebSocketError error, int nativeError, Exception? innerException) |
| | 0 | 77 | | : this(error, nativeError, GetErrorMessage(error), innerException) |
| | 0 | 78 | | { |
| | 0 | 79 | | } |
| | | 80 | | |
| | | 81 | | public WebSocketException(WebSocketError error, int nativeError, string? message, Exception? innerException) |
| | 0 | 82 | | : base(message ?? SR.net_WebSockets_Generic, innerException) |
| | 0 | 83 | | { |
| | 0 | 84 | | _webSocketErrorCode = error; |
| | 0 | 85 | | SetErrorCodeOnError(nativeError); |
| | 0 | 86 | | } |
| | | 87 | | |
| | | 88 | | public WebSocketException(string? message) |
| | 29 | 89 | | : base(message ?? SR.net_WebSockets_Generic) |
| | 29 | 90 | | { |
| | 29 | 91 | | } |
| | | 92 | | |
| | | 93 | | public WebSocketException(string? message, Exception? innerException) |
| | 0 | 94 | | : base(message ?? SR.net_WebSockets_Generic, innerException) |
| | 0 | 95 | | { |
| | 0 | 96 | | } |
| | | 97 | | |
| | | 98 | | [Obsolete(Obsoletions.LegacyFormatterImplMessage, DiagnosticId = Obsoletions.LegacyFormatterImplDiagId, UrlForma |
| | | 99 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| | | 100 | | private WebSocketException(SerializationInfo serializationInfo, StreamingContext streamingContext) |
| | 0 | 101 | | : base(serializationInfo, streamingContext) |
| | 0 | 102 | | { |
| | 0 | 103 | | } |
| | | 104 | | |
| | | 105 | | [Obsolete(Obsoletions.LegacyFormatterImplMessage, DiagnosticId = Obsoletions.LegacyFormatterImplDiagId, UrlForma |
| | | 106 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| | | 107 | | public override void GetObjectData(SerializationInfo info, StreamingContext context) |
| | 0 | 108 | | { |
| | 0 | 109 | | base.GetObjectData(info, context); |
| | 0 | 110 | | info.AddValue(nameof(WebSocketErrorCode), _webSocketErrorCode); |
| | 0 | 111 | | } |
| | | 112 | | |
| | | 113 | | public override int ErrorCode |
| | | 114 | | { |
| | | 115 | | get |
| | 0 | 116 | | { |
| | 0 | 117 | | return base.NativeErrorCode; |
| | 0 | 118 | | } |
| | | 119 | | } |
| | | 120 | | |
| | | 121 | | public WebSocketError WebSocketErrorCode |
| | | 122 | | { |
| | | 123 | | get |
| | 0 | 124 | | { |
| | 0 | 125 | | return _webSocketErrorCode; |
| | 0 | 126 | | } |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | private static string GetErrorMessage(WebSocketError error) => |
| | | 130 | | // Provide a canned message for the error type. |
| | 1558 | 131 | | error switch |
| | 1558 | 132 | | { |
| | 0 | 133 | | WebSocketError.InvalidMessageType => SR.Format(SR.net_WebSockets_InvalidMessageType_Generic, |
| | 0 | 134 | | $"{nameof(WebSocket)}.{nameof(WebSocket.CloseAsync)}", |
| | 0 | 135 | | $"{nameof(WebSocket)}.{nameof(WebSocket.CloseOutputAsync)}"), |
| | 0 | 136 | | WebSocketError.Faulted => SR.net_Websockets_WebSocketBaseFaulted, |
| | 0 | 137 | | WebSocketError.NotAWebSocket => SR.net_WebSockets_NotAWebSocket_Generic, |
| | 0 | 138 | | WebSocketError.UnsupportedVersion => SR.net_WebSockets_UnsupportedWebSocketVersion_Generic, |
| | 0 | 139 | | WebSocketError.UnsupportedProtocol => SR.net_WebSockets_UnsupportedProtocol_Generic, |
| | 0 | 140 | | WebSocketError.HeaderError => SR.net_WebSockets_HeaderError_Generic, |
| | 1558 | 141 | | WebSocketError.ConnectionClosedPrematurely => SR.net_WebSockets_ConnectionClosedPrematurely_Generic, |
| | 0 | 142 | | WebSocketError.InvalidState => SR.net_WebSockets_InvalidState_Generic, |
| | 0 | 143 | | _ => SR.net_WebSockets_Generic, |
| | 1558 | 144 | | }; |
| | | 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) |
| | 0 | 149 | | { |
| | 0 | 150 | | if (!Succeeded(nativeError)) |
| | 0 | 151 | | { |
| | 0 | 152 | | HResult = nativeError; |
| | 0 | 153 | | } |
| | 0 | 154 | | } |
| | | 155 | | |
| | | 156 | | private static bool Succeeded(int hr) |
| | 0 | 157 | | { |
| | 0 | 158 | | return (hr >= 0); |
| | 0 | 159 | | } |
| | | 160 | | } |
| | | 161 | | } |
| | | 162 | | |