From d022eba6a1368f96ab89fa6b30731c52f69d4b93 Mon Sep 17 00:00:00 2001 From: sta Date: Tue, 7 Nov 2017 15:01:16 +0900 Subject: [PATCH] [Modify] Throw exceptions --- websocket-sharp/WebSocket.cs | 42 ++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/websocket-sharp/WebSocket.cs b/websocket-sharp/WebSocket.cs index 46ea4fad..4cd8f5ad 100644 --- a/websocket-sharp/WebSocket.cs +++ b/websocket-sharp/WebSocket.cs @@ -3775,38 +3775,52 @@ namespace WebSocketSharp /// public void SetCredentials (string username, string password, bool preAuth) { - string msg; - if (!checkIfAvailable (true, false, true, false, false, true, out msg)) { - _logger.Error (msg); - error ("An error has occurred in setting the credentials.", null); + string msg = null; - return; + if (!_client) { + msg = "This instance is not a client."; + throw new InvalidOperationException (msg); } - if (!checkParametersForSetCredentials (username, password, out msg)) { - _logger.Error (msg); - error ("An error has occurred in setting the credentials.", null); + if (!username.IsNullOrEmpty ()) { + if (username.Contains (':') || !username.IsText ()) { + msg = "It contains an invalid character."; + throw new ArgumentException (msg, "username"); + } + } + if (!password.IsNullOrEmpty ()) { + if (!password.IsText ()) { + msg = "It contains an invalid character."; + throw new ArgumentException (msg, "password"); + } + } + + if (!canSet (out msg)) { + _logger.Warn (msg); return; } lock (_forState) { - if (!checkIfAvailable (true, false, false, true, out msg)) { - _logger.Error (msg); - error ("An error has occurred in setting the credentials.", null); - + if (!canSet (out msg)) { + _logger.Warn (msg); return; } if (username.IsNullOrEmpty ()) { - _logger.Warn ("The credentials are initialized."); + msg = "The credentials are initialized."; + _logger.Warn (msg); + _credentials = null; _preAuth = false; return; } - _credentials = new NetworkCredential (username, password, _uri.PathAndQuery); + _credentials = new NetworkCredential ( + username, password, _uri.PathAndQuery + ); + _preAuth = preAuth; } }