Added better support for detection of duplicate users
Corrected and enhanced exception message for GetUser
This commit is contained in:
parent
098363a9cc
commit
d862d7922e
1 changed files with 15 additions and 4 deletions
|
|
@ -20,6 +20,7 @@ using System.Data;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using MySql.Data.MySqlClient;
|
using MySql.Data.MySqlClient;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
namespace TShockAPI.DB
|
namespace TShockAPI.DB
|
||||||
{
|
{
|
||||||
|
|
@ -62,6 +63,9 @@ namespace TShockAPI.DB
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
// Detect duplicate user using a regexp as Sqlite doesn't have well structured exceptions
|
||||||
|
if (Regex.IsMatch(ex.Message, "Username.*not unique"))
|
||||||
|
throw new UserExistsException(user.Name);
|
||||||
throw new UserManagerException("AddUser SQL returned an error (" + ex.Message + ")", ex);
|
throw new UserManagerException("AddUser SQL returned an error (" + ex.Message + ")", ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -254,15 +258,22 @@ namespace TShockAPI.DB
|
||||||
result = database.QueryReader("SELECT * FROM Users WHERE IP=@0", user.Address);
|
result = database.QueryReader("SELECT * FROM Users WHERE IP=@0", user.Address);
|
||||||
}
|
}
|
||||||
|
|
||||||
using (var reader = result)
|
if (result.Read())
|
||||||
{
|
{
|
||||||
if (reader.Read())
|
user = LoadUserFromResult(user, result);
|
||||||
return LoadUserFromResult(user, result);
|
// Check for multiple matches
|
||||||
|
if (!result.Read())
|
||||||
|
return user;
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(user.Address))
|
||||||
|
throw new UserManagerException(String.Format("Multiple users found for name '{0}'", user.Name));
|
||||||
|
else
|
||||||
|
throw new UserManagerException(String.Format("Multiple users found for ip '{0}'", user.Address));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
throw new UserManagerException("GetUserID SQL returned an error", ex);
|
throw new UserManagerException("GetUser SQL returned an error (" + ex.Message + ")", ex);
|
||||||
}
|
}
|
||||||
throw new UserNotExistException(string.IsNullOrEmpty(user.Address) ? user.Name : user.Address);
|
throw new UserNotExistException(string.IsNullOrEmpty(user.Address) ? user.Name : user.Address);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue