1 //------------------------------------------------------------------------------
2 // <copyright file="WebSocketException.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 //------------------------------------------------------------------------------
7 namespace System.Net.WebSockets
10 using System.ComponentModel;
11 using System.Diagnostics.CodeAnalysis;
12 using System.Runtime.InteropServices;
13 using System.Runtime.Serialization;
14 using System.Security;
15 using System.Security.Permissions;
18 public sealed class WebSocketException : Win32Exception
20 private WebSocketError m_WebSocketErrorCode;
22 [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands",
23 Justification = "This ctor is harmless, because it does not pass arbitrary data into the native code.")]
24 public WebSocketException()
25 : this(Marshal.GetLastWin32Error())
29 [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands",
30 Justification = "This ctor is harmless, because it does not pass arbitrary data into the native code.")]
31 public WebSocketException(WebSocketError error)
32 : this(error, GetErrorMessage(error))
36 [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands",
37 Justification = "This ctor is harmless, because it does not pass arbitrary data into the native code.")]
38 public WebSocketException(WebSocketError error, string message) : base(message)
40 m_WebSocketErrorCode = error;
43 [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands",
44 Justification = "This ctor is harmless, because it does not pass arbitrary data into the native code.")]
45 public WebSocketException(WebSocketError error, Exception innerException)
46 : this(error, GetErrorMessage(error), innerException)
50 [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands",
51 Justification = "This ctor is harmless, because it does not pass arbitrary data into the native code.")]
52 public WebSocketException(WebSocketError error, string message, Exception innerException)
53 : base(message, innerException)
55 m_WebSocketErrorCode = error;
58 [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands",
59 Justification = "This ctor is harmless, because it does not pass arbitrary data into the native code.")]
60 public WebSocketException(int nativeError)
63 m_WebSocketErrorCode = !WebSocketProtocolComponent.Succeeded(nativeError) ? WebSocketError.NativeError : WebSocketError.Success;
64 this.SetErrorCodeOnError(nativeError);
67 [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands",
68 Justification = "This ctor is harmless, because it does not pass arbitrary data into the native code.")]
69 public WebSocketException(int nativeError, string message)
70 : base(nativeError, message)
72 m_WebSocketErrorCode = !WebSocketProtocolComponent.Succeeded(nativeError) ? WebSocketError.NativeError : WebSocketError.Success;
73 this.SetErrorCodeOnError(nativeError);
76 [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands",
77 Justification = "This ctor is harmless, because it does not pass arbitrary data into the native code.")]
78 public WebSocketException(int nativeError, Exception innerException)
79 : base(SR.GetString(SR.net_WebSockets_Generic), innerException)
81 m_WebSocketErrorCode = !WebSocketProtocolComponent.Succeeded(nativeError) ? WebSocketError.NativeError : WebSocketError.Success;
82 this.SetErrorCodeOnError(nativeError);
85 [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands",
86 Justification = "This ctor is harmless, because it does not pass arbitrary data into the native code.")]
87 public WebSocketException(WebSocketError error, int nativeError)
88 : this(error, nativeError, GetErrorMessage(error))
92 [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands",
93 Justification = "This ctor is harmless, because it does not pass arbitrary data into the native code.")]
94 public WebSocketException(WebSocketError error, int nativeError, string message)
97 m_WebSocketErrorCode = error;
98 this.SetErrorCodeOnError(nativeError);
101 [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands",
102 Justification = "This ctor is harmless, because it does not pass arbitrary data into the native code.")]
103 public WebSocketException(WebSocketError error, int nativeError, Exception innerException)
104 : this(error, nativeError, GetErrorMessage(error), innerException)
108 [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands",
109 Justification = "This ctor is harmless, because it does not pass arbitrary data into the native code.")]
110 public WebSocketException(WebSocketError error, int nativeError, string message, Exception innerException)
111 : base(message, innerException)
113 m_WebSocketErrorCode = error;
114 this.SetErrorCodeOnError(nativeError);
117 [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands",
118 Justification = "This ctor is harmless, because it does not pass arbitrary data into the native code.")]
119 public WebSocketException(string message)
124 [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands",
125 Justification = "This ctor is harmless, because it does not pass arbitrary data into the native code.")]
126 public WebSocketException(string message, Exception innerException)
127 : base(message, innerException)
131 [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands",
132 Justification = "This ctor is harmless, because it does not pass arbitrary data into the native code.")]
133 private WebSocketException(SerializationInfo serializationInfo, StreamingContext streamingContext)
134 : base(serializationInfo, streamingContext)
138 public override int ErrorCode
142 return base.NativeErrorCode;
146 public WebSocketError WebSocketErrorCode
150 return m_WebSocketErrorCode;
154 private static string GetErrorMessage(WebSocketError error)
156 // provide a canned message for the error type
159 case WebSocketError.InvalidMessageType:
160 return SR.GetString(SR.net_WebSockets_InvalidMessageType_Generic,
161 typeof(WebSocket).Name + WebSocketBase.Methods.CloseAsync,
162 typeof(WebSocket).Name + WebSocketBase.Methods.CloseOutputAsync);
163 case WebSocketError.Faulted:
164 return SR.GetString(SR.net_Websockets_WebSocketBaseFaulted);
165 case WebSocketError.NotAWebSocket:
166 return SR.GetString(SR.net_WebSockets_NotAWebSocket_Generic);
167 case WebSocketError.UnsupportedVersion:
168 return SR.GetString(SR.net_WebSockets_UnsupportedWebSocketVersion_Generic);
169 case WebSocketError.UnsupportedProtocol:
170 return SR.GetString(SR.net_WebSockets_UnsupportedProtocol_Generic);
171 case WebSocketError.HeaderError:
172 return SR.GetString(SR.net_WebSockets_HeaderError_Generic);
173 case WebSocketError.ConnectionClosedPrematurely:
174 return SR.GetString(SR.net_WebSockets_ConnectionClosedPrematurely_Generic);
175 case WebSocketError.InvalidState:
176 return SR.GetString(SR.net_WebSockets_InvalidState_Generic);
178 return SR.GetString(SR.net_WebSockets_Generic);
182 [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
183 public override void GetObjectData(SerializationInfo info, StreamingContext context)
187 throw new ArgumentNullException("info");
189 info.AddValue("WebSocketErrorCode", m_WebSocketErrorCode);
190 base.GetObjectData(info, context);
193 // Set the error code only if there is an error (i.e. nativeError >= 0). Otherwise the code ----s up on deserialization
194 // as the Exception..ctor() throws on setting HResult to 0. The default for HResult is -2147467259.
195 private void SetErrorCodeOnError(int nativeError)
197 if (!WebSocketProtocolComponent.Succeeded(nativeError))
199 this.HResult = nativeError;