refactor: consolidate [x]TooLongErrors into StringTooLongError

This commit is contained in:
spiral 2021-09-13 01:13:57 -04:00
parent b9d090d8cf
commit bae883c11f
No known key found for this signature in database
GPG Key ID: A6059F0CA0E1BD31
6 changed files with 15 additions and 15 deletions

View File

@ -194,7 +194,7 @@ namespace PluralKit.Bot
{
var description = ctx.RemainderOrNull().NormalizeLineEndSpacing();
if (description.IsLongerThan(Limits.MaxDescriptionLength))
throw Errors.DescriptionTooLongError(description.Length);
throw Errors.StringTooLongError("Description", description.Length, Limits.MaxDescriptionLength);
var patch = new GroupPatch { Description = Partial<string>.Present(description) };
await _db.Execute(conn => _repo.UpdateGroup(conn, target.Id, patch));

View File

@ -36,7 +36,8 @@ namespace PluralKit.Bot
var memberName = ctx.RemainderOrNull() ?? throw new PKSyntaxError("You must pass a member name.");
// Hard name length cap
if (memberName.Length > Limits.MaxMemberNameLength) throw Errors.MemberNameTooLongError(memberName.Length);
if (memberName.Length > Limits.MaxMemberNameLength)
throw Errors.StringTooLongError("Member name", memberName.Length, Limits.MaxMemberNameLength);
// Warn if there's already a member by this name
var existingMember = await _db.Execute(c => _repo.GetMemberByName(c, ctx.System.Id, memberName));

View File

@ -31,7 +31,8 @@ namespace PluralKit.Bot
var newName = ctx.RemainderOrNull() ?? throw new PKSyntaxError("You must pass a new name for the member.");
// Hard name length cap
if (newName.Length > Limits.MaxMemberNameLength) throw Errors.MemberNameTooLongError(newName.Length);
if (newName.Length > Limits.MaxMemberNameLength)
throw Errors.StringTooLongError("Member name", newName.Length, Limits.MaxMemberNameLength);
// Warn if there's already a member by this name
var existingMember = await _db.Execute(conn => _repo.GetMemberByName(conn, ctx.System.Id, newName));
@ -100,7 +101,7 @@ namespace PluralKit.Bot
{
var description = ctx.RemainderOrNull().NormalizeLineEndSpacing();
if (description.IsLongerThan(Limits.MaxDescriptionLength))
throw Errors.DescriptionTooLongError(description.Length);
throw Errors.StringTooLongError("Description", description.Length, Limits.MaxDescriptionLength);
var patch = new MemberPatch { Description = Partial<string>.Present(description) };
await _db.Execute(conn => _repo.UpdateMember(conn, target.Id, patch));
@ -148,7 +149,7 @@ namespace PluralKit.Bot
{
var pronouns = ctx.RemainderOrNull().NormalizeLineEndSpacing();
if (pronouns.IsLongerThan(Limits.MaxPronounsLength))
throw Errors.MemberPronounsTooLongError(pronouns.Length);
throw Errors.StringTooLongError("Pronouns", pronouns.Length, Limits.MaxPronounsLength);
var patch = new MemberPatch { Pronouns = Partial<string>.Present(pronouns) };
await _db.Execute(conn => _repo.UpdateMember(conn, target.Id, patch));

View File

@ -30,7 +30,7 @@ namespace PluralKit.Bot
var systemName = ctx.RemainderOrNull();
if (systemName != null && systemName.Length > Limits.MaxSystemNameLength)
throw Errors.SystemNameTooLongError(systemName.Length);
throw Errors.StringTooLongError("System name", systemName.Length, Limits.MaxSystemNameLength);
var system = _db.Execute(async c =>
{

View File

@ -62,7 +62,7 @@ namespace PluralKit.Bot
var newSystemName = ctx.RemainderOrNull();
if (newSystemName.Length > Limits.MaxSystemNameLength)
throw Errors.SystemNameTooLongError(newSystemName.Length);
throw Errors.StringTooLongError("System name", newSystemName.Length, Limits.MaxSystemNameLength);
var patch = new SystemPatch { Name = newSystemName };
await _db.Execute(conn => _repo.UpdateSystem(conn, ctx.System.Id, patch));
@ -109,7 +109,8 @@ namespace PluralKit.Bot
else
{
var newDescription = ctx.RemainderOrNull()?.NormalizeLineEndSpacing();
if (newDescription.Length > Limits.MaxDescriptionLength) throw Errors.DescriptionTooLongError(newDescription.Length);
if (newDescription.Length > Limits.MaxDescriptionLength)
throw Errors.StringTooLongError("Description", newDescription.Length, Limits.MaxDescriptionLength);
var patch = new SystemPatch { Description = newDescription };
await _db.Execute(conn => _repo.UpdateSystem(conn, ctx.System.Id, patch));
@ -195,7 +196,7 @@ namespace PluralKit.Bot
var newTag = ctx.RemainderOrNull(skipFlags: false);
if (newTag != null)
if (newTag.Length > Limits.MaxSystemTagLength)
throw Errors.SystemTagTooLongError(newTag.Length);
throw Errors.StringTooLongError("System tag", newTag.Length, Limits.MaxSystemTagLength);
var patch = new SystemPatch { Tag = newTag };
await _db.Execute(conn => _repo.UpdateSystem(conn, ctx.System.Id, patch));
@ -234,7 +235,7 @@ namespace PluralKit.Bot
{
var newTag = ctx.RemainderOrNull(skipFlags: false);
if (newTag != null && newTag.Length > Limits.MaxSystemTagLength)
throw Errors.SystemTagTooLongError(newTag.Length);
throw Errors.StringTooLongError("System server tag", newTag.Length, Limits.MaxSystemTagLength);
var patch = new SystemGuildPatch { Tag = newTag };
await _db.Execute(conn => _repo.UpsertSystemGuild(conn, ctx.System.Id, ctx.Guild.Id, patch));

View File

@ -41,11 +41,8 @@ namespace PluralKit.Bot
public static PKError ExistingSystemError => new PKError("You already have a system registered with PluralKit. To view it, type `pk;system`. If you'd like to delete your system and start anew, type `pk;system delete`, or if you'd like to unlink this account from it, type `pk;unlink`.");
public static PKError MissingMemberError => new PKSyntaxError("You need to specify a member to run this command on.");
public static PKError SystemNameTooLongError(int length) => new PKError($"System name too long ({length}/{Limits.MaxSystemNameLength} characters).");
public static PKError SystemTagTooLongError(int length) => new PKError($"System tag too long ({length}/{Limits.MaxSystemTagLength} characters).");
public static PKError DescriptionTooLongError(int length) => new PKError($"Description too long ({length}/{Limits.MaxDescriptionLength} characters).");
public static PKError MemberNameTooLongError(int length) => new PKError($"Member name too long ({length}/{Limits.MaxMemberNameLength} characters).");
public static PKError MemberPronounsTooLongError(int length) => new PKError($"Member pronouns too long ({length}/{Limits.MaxMemberNameLength} characters).");
public static PKError StringTooLongError(string name, int length, int maxLength) => new PKError($"{name} too long ({length}/{maxLength} characters).");
public static PKError MemberLimitReachedError(int limit) => new PKError($"System has reached the maximum number of members ({limit}). Please delete unused members first in order to create new ones.");
public static PKError InvalidColorError(string color) => new PKError($"\"{color}\" is not a valid color. Color must be in 6-digit RGB hex format (eg. #ff0000).");