From ec06e865881bdda96e9b995171bc7d3befe24032 Mon Sep 17 00:00:00 2001 From: liris Date: Thu, 30 Jul 2015 09:31:36 +0900 Subject: [PATCH] fixed #199 WebSocketBadStatusException for handshake error --- ChangeLog | 1 + websocket/_exceptions.py | 12 ++++++++++++ websocket/_handshake.py | 2 +- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 8dfdbec..c255cbc 100644 --- a/ChangeLog +++ b/ChangeLog @@ -11,6 +11,7 @@ ChangeLog - fix string formatting in exception (#196) - fix typo in README.rst (#197) - introduce on_data callback to pass data type. (#198) + - WebSocketBadStatusException for Handshake error (#199) - 0.32.0 diff --git a/websocket/_exceptions.py b/websocket/_exceptions.py index 9b1791d..ffc41ab 100644 --- a/websocket/_exceptions.py +++ b/websocket/_exceptions.py @@ -31,18 +31,21 @@ class WebSocketException(Exception): """ pass + class WebSocketProtocolException(WebSocketException): """ If the webscoket protocol is invalid, this exception will be raised. """ pass + class WebSocketPayloadException(WebSocketException): """ If the webscoket payload is invalid, this exception will be raised. """ pass + class WebSocketConnectionClosedException(WebSocketException): """ If remote host closed the connection or some network error happened, @@ -50,12 +53,14 @@ class WebSocketConnectionClosedException(WebSocketException): """ pass + class WebSocketTimeoutException(WebSocketException): """ WebSocketTimeoutException will be raised at socket timeout during read/write data. """ pass + class WebSocketProxyException(WebSocketException): """ WebSocketProxyException will be raised when proxy error occured. @@ -63,3 +68,10 @@ class WebSocketProxyException(WebSocketException): pass +class WebSocketBadStatusException(WebSocketException): + """ + WebSocketBadStatusException will be raised when we get bad handshake status code. + """ + def __init__(self, message, status_code): + super(WebSocketBadStatusException, self).__init__(message % status_code) + self.status_code = status_code diff --git a/websocket/_handshake.py b/websocket/_handshake.py index d8876e7..4d20fed 100644 --- a/websocket/_handshake.py +++ b/websocket/_handshake.py @@ -108,7 +108,7 @@ def _get_handshake_headers(resource, host, port, options): def _get_resp_headers(sock, success_status=101): status, resp_headers = read_headers(sock) if status != success_status: - raise WebSocketException("Handshake status %d" % status) + raise WebSocketBadStatusException("Handshake status %d", status) return status, resp_headers _HEADERS_TO_CHECK = {