[Modify] Throw exceptions

This commit is contained in:
sta 2017-11-07 15:01:16 +09:00
parent db696c2249
commit d022eba6a1

View File

@ -3775,38 +3775,52 @@ namespace WebSocketSharp
/// </param> /// </param>
public void SetCredentials (string username, string password, bool preAuth) public void SetCredentials (string username, string password, bool preAuth)
{ {
string msg; string msg = null;
if (!checkIfAvailable (true, false, true, false, false, true, out msg)) {
_logger.Error (msg);
error ("An error has occurred in setting the credentials.", null);
return; if (!_client) {
msg = "This instance is not a client.";
throw new InvalidOperationException (msg);
} }
if (!checkParametersForSetCredentials (username, password, out msg)) { if (!username.IsNullOrEmpty ()) {
_logger.Error (msg); if (username.Contains (':') || !username.IsText ()) {
error ("An error has occurred in setting the credentials.", null); 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; return;
} }
lock (_forState) { lock (_forState) {
if (!checkIfAvailable (true, false, false, true, out msg)) { if (!canSet (out msg)) {
_logger.Error (msg); _logger.Warn (msg);
error ("An error has occurred in setting the credentials.", null);
return; return;
} }
if (username.IsNullOrEmpty ()) { if (username.IsNullOrEmpty ()) {
_logger.Warn ("The credentials are initialized."); msg = "The credentials are initialized.";
_logger.Warn (msg);
_credentials = null; _credentials = null;
_preAuth = false; _preAuth = false;
return; return;
} }
_credentials = new NetworkCredential (username, password, _uri.PathAndQuery); _credentials = new NetworkCredential (
username, password, _uri.PathAndQuery
);
_preAuth = preAuth; _preAuth = preAuth;
} }
} }