From: David ‘Bombe’ Roden Date: Sun, 15 Mar 2015 10:28:34 +0000 (+0100) Subject: Add custom exception for adding duplicate fields X-Git-Tag: 0.9-rc1^2~3^2~4 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=28667abdadc0573ac106542f1fd42ab5775ec415;p=Sone.git Add custom exception for adding duplicate fields --- diff --git a/src/main/java/net/pterodactylus/sone/core/SoneParser.java b/src/main/java/net/pterodactylus/sone/core/SoneParser.java index 5851b18..e234965 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneParser.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneParser.java @@ -20,6 +20,7 @@ import net.pterodactylus.sone.data.Image; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.PostReply; import net.pterodactylus.sone.data.Profile; +import net.pterodactylus.sone.data.Profile.DuplicateField; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.database.PostBuilder; import net.pterodactylus.sone.database.PostReplyBuilder; @@ -150,8 +151,8 @@ public class SoneParser { } try { profile.addField(fieldName.trim()).setValue(fieldValue); - } catch (IllegalArgumentException iae1) { - logger.log(Level.WARNING, String.format("Duplicate field: %s", fieldName), iae1); + } catch (DuplicateField df1) { + logger.log(Level.WARNING, String.format("Duplicate field: %s", fieldName), df1); return null; } } diff --git a/src/main/java/net/pterodactylus/sone/data/Profile.java b/src/main/java/net/pterodactylus/sone/data/Profile.java index 4bf26cc..1f80248 100644 --- a/src/main/java/net/pterodactylus/sone/data/Profile.java +++ b/src/main/java/net/pterodactylus/sone/data/Profile.java @@ -326,7 +326,9 @@ public class Profile implements Fingerprintable { public Field addField(String fieldName) throws IllegalArgumentException { checkNotNull(fieldName, "fieldName must not be null"); checkArgument(fieldName.length() > 0, "fieldName must not be empty"); - checkArgument(getFieldByName(fieldName) == null, "fieldName must be unique"); + if (getFieldByName(fieldName) != null) { + throw new DuplicateField(); + } @SuppressWarnings("synthetic-access") Field field = new Field().setName(fieldName); fields.add(field); @@ -553,4 +555,11 @@ public class Profile implements Fingerprintable { } + /** + * Exception that signals the addition of a field that already exists. + * + * @author David ‘Bombe’ Roden + */ + public static class DuplicateField extends IllegalArgumentException { } + } diff --git a/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java b/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java index 39c2561..d7d729f 100644 --- a/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java +++ b/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java @@ -23,6 +23,7 @@ import static net.pterodactylus.sone.utils.NumberParsers.parseInt; import java.util.List; import net.pterodactylus.sone.data.Profile; +import net.pterodactylus.sone.data.Profile.DuplicateField; import net.pterodactylus.sone.data.Profile.Field; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.web.page.FreenetRequest; @@ -101,7 +102,7 @@ public class EditProfilePage extends SoneTemplatePage { fields = profile.getFields(); webInterface.getCore().touchConfiguration(); throw new RedirectException("editProfile.html#profile-fields"); - } catch (IllegalArgumentException iae1) { + } catch (DuplicateField df1) { templateContext.set("fieldName", fieldName); templateContext.set("duplicateFieldName", true); }