From 0602437152c584aea05c2adea3b72783c6916a95 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sun, 15 Oct 2017 22:01:01 +0200 Subject: [PATCH] Continued: - normalized exceptions, like when an entity has already been added (with same title here) or when it was not found. Already added exceptions always indicate problems with calling methods by "not-found" exceptions may be triggered by invalid "show" links or such kinds (when a non-existing id number for example was provided by parameter). - added constructor with id number (common way) MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../CategoryAlreadyAddedException.java} | 22 +++++++++--- .../CategoryNotFoundException.java} | 35 +++++++++++++++---- .../ProductAlreadyAddedException.java} | 25 +++++++++---- .../ProductNotFoundException.java} | 24 +++++++++---- 4 files changed, 81 insertions(+), 25 deletions(-) rename src/org/mxchange/jproduct/exceptions/{CannotAddCategoryException.java => category/CategoryAlreadyAddedException.java} (60%) rename src/org/mxchange/jproduct/exceptions/{CategoryTitleAlreadyUsedException.java => category/CategoryNotFoundException.java} (51%) rename src/org/mxchange/jproduct/exceptions/{ProductTitleAlreadyUsedException.java => product/ProductAlreadyAddedException.java} (59%) rename src/org/mxchange/jproduct/exceptions/{CannotAddProductException.java => product/ProductNotFoundException.java} (65%) diff --git a/src/org/mxchange/jproduct/exceptions/CannotAddCategoryException.java b/src/org/mxchange/jproduct/exceptions/category/CategoryAlreadyAddedException.java similarity index 60% rename from src/org/mxchange/jproduct/exceptions/CannotAddCategoryException.java rename to src/org/mxchange/jproduct/exceptions/category/CategoryAlreadyAddedException.java index e461574..b724fb2 100644 --- a/src/org/mxchange/jproduct/exceptions/CannotAddCategoryException.java +++ b/src/org/mxchange/jproduct/exceptions/category/CategoryAlreadyAddedException.java @@ -14,15 +14,17 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -package org.mxchange.jproduct.exceptions; +package org.mxchange.jproduct.exceptions.category; + +import java.text.MessageFormat; +import org.mxchange.jproduct.model.category.Category; /** - * An exception thrown when the category cannot be added for a "low level" - * reason. + * An exception thrown when a category has already been added. *

* @author Roland Häder */ -public class CannotAddCategoryException extends Exception { +public class CategoryAlreadyAddedException extends Exception { /** * Serial number @@ -34,8 +36,18 @@ public class CannotAddCategoryException extends Exception { *

* @param cause Causing exception */ - public CannotAddCategoryException (final Throwable cause) { + public CategoryAlreadyAddedException (final Throwable cause) { // Call super constructor super(cause); } + + /** + * Constructor with category instance + *

+ * @param category Category instance + */ + public CategoryAlreadyAddedException (final Category category) { + // Call super constructor + super(MessageFormat.format("Category with title '{0}' already added.", category.getCategoryTitle())); //NOI18N + } } diff --git a/src/org/mxchange/jproduct/exceptions/CategoryTitleAlreadyUsedException.java b/src/org/mxchange/jproduct/exceptions/category/CategoryNotFoundException.java similarity index 51% rename from src/org/mxchange/jproduct/exceptions/CategoryTitleAlreadyUsedException.java rename to src/org/mxchange/jproduct/exceptions/category/CategoryNotFoundException.java index e06caa5..b02c11e 100644 --- a/src/org/mxchange/jproduct/exceptions/CategoryTitleAlreadyUsedException.java +++ b/src/org/mxchange/jproduct/exceptions/category/CategoryNotFoundException.java @@ -14,30 +14,51 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -package org.mxchange.jproduct.exceptions; +package org.mxchange.jproduct.exceptions.category; import java.text.MessageFormat; import org.mxchange.jproduct.model.category.Category; /** - * An exception thrown when the given title is already used + * An exception thrown when a category was not found. *

* @author Roland Häder */ -public class CategoryTitleAlreadyUsedException extends Exception { +public class CategoryNotFoundException extends Exception { /** * Serial number */ - private static final long serialVersionUID = 53_751_434_673_262L; + private static final long serialVersionUID = 34_295_843_957_952L; /** - * Constructor with HttpServletRequest instance + * Constructor with cause + *

+ * @param cause Causing exception + */ + public CategoryNotFoundException (final Throwable cause) { + // Call super constructor + super(cause); + } + + /** + * Constructor with category instance *

* @param category Category instance */ - public CategoryTitleAlreadyUsedException (final Category category) { + public CategoryNotFoundException (final Category category) { // Call super constructor - super(MessageFormat.format("Title {0} is already used.", category.getCategoryTitle())); //NOI18N + super(MessageFormat.format("Category with title '{0}' already added.", category.getCategoryTitle())); //NOI18N } + + /** + * Constructor with category id + *

+ * @param categoryId Category id + */ + public CategoryNotFoundException (final Long categoryId) { + // Call super constructor + super(MessageFormat.format("Category with id '{0}' not found.", categoryId)); //NOI18N + } + } diff --git a/src/org/mxchange/jproduct/exceptions/ProductTitleAlreadyUsedException.java b/src/org/mxchange/jproduct/exceptions/product/ProductAlreadyAddedException.java similarity index 59% rename from src/org/mxchange/jproduct/exceptions/ProductTitleAlreadyUsedException.java rename to src/org/mxchange/jproduct/exceptions/product/ProductAlreadyAddedException.java index ca60792..32accde 100644 --- a/src/org/mxchange/jproduct/exceptions/ProductTitleAlreadyUsedException.java +++ b/src/org/mxchange/jproduct/exceptions/product/ProductAlreadyAddedException.java @@ -14,30 +14,41 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -package org.mxchange.jproduct.exceptions; +package org.mxchange.jproduct.exceptions.product; import java.text.MessageFormat; import org.mxchange.jproduct.model.product.Product; /** - * An exception thrown when the given title is already used + * An exception thrown when a product was already found. *

* @author Roland Häder */ -public class ProductTitleAlreadyUsedException extends Exception { +public class ProductAlreadyAddedException extends Exception { /** * Serial number */ - private static final long serialVersionUID = 4_252_734_834_174L; + private static final long serialVersionUID = 48_574_857_485_748_175L; /** - * Constructor with HttpServletRequest instance + * Constructor with cause + *

+ * @param cause Causing exception + */ + public ProductAlreadyAddedException (final Throwable cause) { + // Call super constructor + super(cause); + } + + /** + * Constructor with product instance *

* @param product Product instance */ - public ProductTitleAlreadyUsedException (final Product product) { + public ProductAlreadyAddedException (final Product product) { // Call super constructor - super(MessageFormat.format("Title {0} is already used.", product.getProductTitle())); //NOI18N + super(MessageFormat.format("Product with title '{0}' already added.", product.getProductTitle())); //NOI18N } + } diff --git a/src/org/mxchange/jproduct/exceptions/CannotAddProductException.java b/src/org/mxchange/jproduct/exceptions/product/ProductNotFoundException.java similarity index 65% rename from src/org/mxchange/jproduct/exceptions/CannotAddProductException.java rename to src/org/mxchange/jproduct/exceptions/product/ProductNotFoundException.java index 713d465..3357852 100644 --- a/src/org/mxchange/jproduct/exceptions/CannotAddProductException.java +++ b/src/org/mxchange/jproduct/exceptions/product/ProductNotFoundException.java @@ -14,28 +14,40 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -package org.mxchange.jproduct.exceptions; +package org.mxchange.jproduct.exceptions.product; + +import java.text.MessageFormat; /** - * An exception thrown when the product cannot be added for a "low level" - * reason. + * An exception thrown when a product has not found. *

* @author Roland Häder */ -public class CannotAddProductException extends Exception { +public class ProductNotFoundException extends Exception { /** * Serial number */ - private static final long serialVersionUID = 48_574_857_485_748_175L; + private static final long serialVersionUID = 48_574_857_485_748_176L; /** * Constructor with cause *

* @param cause Causing exception */ - public CannotAddProductException (final Throwable cause) { + public ProductNotFoundException (final Throwable cause) { // Call super constructor super(cause); } + + /** + * Constructor with product id + *

+ * @param productId Product id + */ + public ProductNotFoundException (final Long productId) { + // Call super constructor + super(MessageFormat.format("Product with id {0} not found.", productId)); //NOI18N + } + } -- 2.39.5