@@ -133,13 +133,18 @@ def is_responder_id(id_):
133133
134134
135135def validate_public_key (key ):
136- if not isinstance (key , bytes ) or len (key ) != KEY_LENGTH :
137- raise MessageError ('Invalid key' )
136+ if not isinstance (key , bytes ):
137+ raise MessageError ('Invalid key: Must be `bytes` (is `{}`)' .format (type (key )))
138+ key_length = len (key )
139+ if key_length != KEY_LENGTH :
140+ raise MessageError ('Invalid key: Invalid length ({} != {})' .format (
141+ key_length , KEY_LENGTH ))
138142
139143
140144def validate_cookie (cookie ):
141145 if not isinstance (cookie , bytes ):
142- raise MessageError ('Invalid cookie: Must be `bytes` instance' )
146+ raise MessageError ('Invalid cookie: Must be `bytes` (is `{}`)' .format (
147+ type (cookie )))
143148 if len (cookie ) != COOKIE_LENGTH :
144149 raise MessageError ('Invalid cookie: Invalid length ({} != {})' .format (
145150 len (cookie ), COOKIE_LENGTH ))
@@ -149,28 +154,37 @@ def validate_subprotocols(subprotocols):
149154 try :
150155 iter (subprotocols )
151156 except TypeError as exc :
152- raise MessageError ('Sub-protocol list is not iterable' ) from exc
157+ raise MessageError ('Sub-protocol list is not iterable (type `{}`)' .format (
158+ type (subprotocols ))) from exc
153159
154160
155161def validate_signed_keys (signed_keys ):
156- max_length = SIGNED_KEYS_CIPHERTEXT_LENGTH
157- if not isinstance (signed_keys , bytes ) or len (signed_keys ) != max_length :
158- raise MessageError ("Invalid value for field 'signed_keys'" )
162+ expected_length = SIGNED_KEYS_CIPHERTEXT_LENGTH
163+ if not isinstance (signed_keys , bytes ):
164+ error = "Invalid value for field 'signed_keys', must be `bytes` (is `{}`)" .format (
165+ type (signed_keys ))
166+ raise MessageError (error )
167+ signed_keys_length = len (signed_keys )
168+ if signed_keys_length != expected_length :
169+ raise MessageError ("Invalid length of field 'signed_keys' ({} != {})" .format (
170+ signed_keys_length , expected_length ))
159171
160172
161173def validate_initiator_connected (initiator_connected ):
162174 if not isinstance (initiator_connected , bool ):
163- raise MessageError ("Invalid value for field 'initiator_connected'" )
175+ error = "Invalid value for field 'initiator_connected', must be `bool` " \
176+ "(is `{}`)" .format (type (initiator_connected ))
177+ raise MessageError (error )
164178
165179
166180def validate_client_id (id_ ):
167181 if not is_client_id (id_ ):
168- raise MessageError ('Invalid client id' )
182+ raise MessageError ('Invalid client id: {}' . format ( id_ ) )
169183
170184
171185def validate_responder_id (id_ ):
172186 if not is_responder_id (id_ ):
173- raise MessageError ('Invalid responder id in responder list' )
187+ raise MessageError ('Invalid responder id: {}' . format ( id_ ) )
174188
175189
176190def validate_responder_ids (ids ):
@@ -183,13 +197,20 @@ def validate_responder_ids(ids):
183197
184198
185199def validate_hash (hash_ ):
186- if not isinstance (hash_ , bytes ) or len (hash_ ) != HASH_LENGTH :
187- raise MessageError ('Invalid hash' )
200+ if not isinstance (hash_ , bytes ):
201+ raise MessageError ('Invalid hash: Must be `bytes` (is `{}`)' .format (type (hash_ )))
202+ hash_length = len (hash_ )
203+ if hash_length != HASH_LENGTH :
204+ raise MessageError ('Invalid hash: Invalid length ({} != {})' .format (
205+ hash_length , HASH_LENGTH ))
188206
189207
190208def validate_ping_interval (ping_interval ):
191- if not isinstance (ping_interval , int ) or ping_interval < 0 :
192- raise MessageError ('Invalid ping interval' )
209+ if not isinstance (ping_interval , int ):
210+ raise MessageError ('Invalid ping interval: Must be `int` (is `{}`)' .format (
211+ type (ping_interval )))
212+ if ping_interval < 0 :
213+ raise MessageError ('Invalid ping interval ({} >= 0)' .format (ping_interval ))
193214
194215
195216def validate_drop_reason (reason ):
@@ -201,7 +222,7 @@ def validate_drop_reason(reason):
201222 try :
202223 reason = CloseCode (reason )
203224 except ValueError :
204- raise MessageError ('Invalid close code' )
225+ raise MessageError ('Invalid close code: {}' . format ( reason ) )
205226 if not reason .is_valid_drop_reason :
206227 raise MessageError ('Reason not from acceptable range of close codes' )
207228
0 commit comments