Repository: websockets-rs/rust-websocket (language: rust)

Issue:
Allow retrieval of StatusCode in connection error
Currently, when a remote server returns an error status code when connecting, all we get is a meaningless ["Status code must be Switching Protocols" message](https://github.com/websockets-rs/rust-websocket/blob/master/src/client/builder.rs#L846). This leads to hard to debug issues like #160.

Instead, when a unexpected status code is encountered it should be returned so that the use can take appropriate action.

Example use case: I am connecting tons of websockets to a remote endpoint and I need to be able to receive the `429 Too many requests` status code to throttle back my rate of ws creation.

I'll make a PR.


Discussion / hints:

Fix patch:
diff --git a/src/client/builder.rs b/src/client/builder.rs
index 350b14e70b..aece892530 100644
--- a/src/client/builder.rs
+++ b/src/client/builder.rs
@@ -844,9 +844,7 @@ impl<'u> ClientBuilder<'u> {
 		let status = StatusCode::from_u16(response.subject.0);
 
 		if status != StatusCode::SwitchingProtocols {
-			return Err(WebSocketError::ResponseError(
-				"Status code must be Switching Protocols",
-			));
+			return Err(WebSocketError::StatusCodeError(status));
 		}
 
 		let key = self
diff --git a/src/result.rs b/src/result.rs
index 3d947e16b0..f83a4ebd40 100644
--- a/src/result.rs
+++ b/src/result.rs
@@ -1,5 +1,6 @@
 //! The result type used within Rust-WebSocket
 
+pub use hyper::status::StatusCode;
 use hyper::Error as HttpError;
 use server::upgrade::HyperIntoWsError;
 use std::convert::From;
@@ -40,6 +41,8 @@ pub enum WebSocketError {
 	ResponseError(&'static str),
 	/// Invalid WebSocket data frame error
 	DataFrameError(&'static str),
+	/// Received unexpected status code
+	StatusCodeError(StatusCode),
 	/// No data available
 	NoDataAvailable,
 	/// An input/output error
@@ -90,6 +93,7 @@ impl Error for WebSocketError {
 			WebSocketError::TlsHandshakeInterruption => "TLS Handshake interrupted",
 			WebSocketError::Utf8Error(_) => "UTF-8 failure",
 			WebSocketError::WebSocketUrlError(_) => "WebSocket URL failure",
+			WebSocketError::StatusCodeError(_) => "Received unexpected status code",
 		}
 	}
 


Test patch: