Adding reference source for System.Net
[mono.git] / mcs / class / referencesource / System / net / System / Net / WebSockets / WebSocketException.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="WebSocketException.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //------------------------------------------------------------------------------
6
7 namespace System.Net.WebSockets
8 {
9     using System;
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; 
16
17     [Serializable]
18     public sealed class WebSocketException : Win32Exception
19     {
20         private WebSocketError m_WebSocketErrorCode;
21
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())
26         {         
27         }
28
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))
33         {
34         }
35
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)
39         {
40             m_WebSocketErrorCode = error;
41         }
42
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)
47         {
48         }
49
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)
54         {
55             m_WebSocketErrorCode = error;
56         }
57
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)
61             : base(nativeError)
62         {
63             m_WebSocketErrorCode = !WebSocketProtocolComponent.Succeeded(nativeError) ? WebSocketError.NativeError : WebSocketError.Success;
64             this.SetErrorCodeOnError(nativeError);
65         }
66
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)
71         {
72             m_WebSocketErrorCode = !WebSocketProtocolComponent.Succeeded(nativeError) ? WebSocketError.NativeError : WebSocketError.Success;
73             this.SetErrorCodeOnError(nativeError);
74         }
75
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)
80         {
81             m_WebSocketErrorCode = !WebSocketProtocolComponent.Succeeded(nativeError) ? WebSocketError.NativeError : WebSocketError.Success;
82             this.SetErrorCodeOnError(nativeError);
83         }
84
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))
89         {
90         }
91
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)
95             : base(message)
96         {
97             m_WebSocketErrorCode = error;
98             this.SetErrorCodeOnError(nativeError);
99         }
100
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)
105         {
106         }
107
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)
112         {
113             m_WebSocketErrorCode = error;
114             this.SetErrorCodeOnError(nativeError);
115         }
116
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)
120             : base(message)
121         {
122         }
123
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)
128         { 
129         }
130
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)
135         {
136         }
137
138         public override int ErrorCode
139         {
140             get
141             {
142                 return base.NativeErrorCode;
143             }
144         }
145
146         public WebSocketError WebSocketErrorCode
147         {
148             get
149             {
150                 return m_WebSocketErrorCode; 
151             }
152         }
153
154         private static string GetErrorMessage(WebSocketError error)
155         {
156             // provide a canned message for the error type
157             switch (error)
158             {
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);
177                 default:
178                     return SR.GetString(SR.net_WebSockets_Generic);
179             }
180         }
181
182         [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
183         public override void GetObjectData(SerializationInfo info, StreamingContext context)
184         {
185             if (info == null)
186             {
187                 throw new ArgumentNullException("info");
188             }
189             info.AddValue("WebSocketErrorCode", m_WebSocketErrorCode);
190             base.GetObjectData(info, context);
191         }
192
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)
196         {
197             if (!WebSocketProtocolComponent.Succeeded(nativeError))
198             {
199                 this.HResult = nativeError;
200             }
201         }
202     }
203 }