You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:55:04 UTC
[sling-org-apache-sling-models-api] 24/34: SLING-3709 improve
exception handling by using dedicated exception classes instead of the one
global result object
This is an automated email from the ASF dual-hosted git repository.
rombert pushed a commit to annotated tag org.apache.sling.models.api-1.2.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-models-api.git
commit 2bf803b825f9263c96a5b2b43c7057b966767665
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Wed May 20 14:39:19 2015 +0000
SLING-3709 improve exception handling by using dedicated exception classes instead of the one global result object
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/models/api@1680581 13f79535-47bb-0310-9956-ffa450edef68
---
...Exception.java => MissingElementException.java} | 24 +++++++-----
.../models/factory/MissingElementsException.java | 43 +++++++++++++---------
.../sling/models/factory/ModelClassException.java | 4 ++
3 files changed, 45 insertions(+), 26 deletions(-)
diff --git a/src/main/java/org/apache/sling/models/factory/ModelClassException.java b/src/main/java/org/apache/sling/models/factory/MissingElementException.java
similarity index 59%
copy from src/main/java/org/apache/sling/models/factory/ModelClassException.java
copy to src/main/java/org/apache/sling/models/factory/MissingElementException.java
index 7718dd6..09195aa 100644
--- a/src/main/java/org/apache/sling/models/factory/ModelClassException.java
+++ b/src/main/java/org/apache/sling/models/factory/MissingElementException.java
@@ -18,18 +18,24 @@
*/
package org.apache.sling.models.factory;
+import java.lang.reflect.AnnotatedElement;
+
/**
- * Exception which is triggered when the Model class could not be instantiated due to
- * not having a model annotation, some reflection error, invalid constructors or
- * because some exception within the post construct method was triggered.
- *
- * @see ModelFactory
+ * Exception which is used whenever one element (field, method or constructor) could not be set.
+ * @see MissingElementsException
*/
-public final class ModelClassException extends RuntimeException {
+public class MissingElementException extends RuntimeException {
+
+ private static final long serialVersionUID = 5782291184414886658L;
+ private final AnnotatedElement element;
- private static final long serialVersionUID = 4323592065808565135L;
+ public MissingElementException(AnnotatedElement element, Throwable cause) {
+ super("Could not inject " + element, cause);
+ this.element = element;
+ }
- public ModelClassException(String message) {
- super(message);
+ public AnnotatedElement getElement() {
+ return element;
}
+
}
diff --git a/src/main/java/org/apache/sling/models/factory/MissingElementsException.java b/src/main/java/org/apache/sling/models/factory/MissingElementsException.java
index f8377fd..7c02dbe 100644
--- a/src/main/java/org/apache/sling/models/factory/MissingElementsException.java
+++ b/src/main/java/org/apache/sling/models/factory/MissingElementsException.java
@@ -18,13 +18,14 @@
*/
package org.apache.sling.models.factory;
-import java.lang.reflect.AnnotatedElement;
+import java.util.ArrayList;
import java.util.Collection;
/**
* Exception which is triggered whenever a Sling Model cannot be instantiated
- * due to some missing elements (i.e. required fields/methods/constructor params
+ * due to some missing elements (i.e. required fields/methods/constructor parameters
* could not be injected).
+ * Contains a number of {@link MissingElementException}s.
*
* @see ModelFactory
*
@@ -32,29 +33,37 @@ import java.util.Collection;
public final class MissingElementsException extends RuntimeException {
private static final long serialVersionUID = 7870762030809272254L;
- private final Collection<? extends AnnotatedElement> missingElements;
+ private Collection<MissingElementException> missingElements;
- private String formatString;
-
- private Class<?> type;
-
- public MissingElementsException(String format, Collection<? extends AnnotatedElement> elements, Class<?> type) {
- super();
- this.formatString = format;
- this.missingElements = elements;
- this.type = type;
+
+ public MissingElementsException(String message) {
+ super(message);
+ missingElements = new ArrayList<MissingElementException>();
}
@Override
public String getMessage() {
- return String.format(formatString, missingElements, type);
+ StringBuilder message = new StringBuilder(super.getMessage());
+ for (MissingElementException e : missingElements) {
+ message.append('\n');
+ message.append(e.getMessage());
+ if (e.getCause() != null) {
+ message.append(" caused by ");
+ message.append(e.getCause().getMessage());
+ }
+ }
+ return message.toString();
}
- public Class<?> getType() {
- return type;
+ public void addMissingElementExceptions(MissingElementException e) {
+ missingElements.add(e);
}
-
- public Collection<? extends AnnotatedElement> getMissingElements() {
+
+ public boolean isEmpty() {
+ return missingElements.isEmpty();
+ }
+
+ public Collection<MissingElementException> getMissingElements() {
return missingElements;
}
}
diff --git a/src/main/java/org/apache/sling/models/factory/ModelClassException.java b/src/main/java/org/apache/sling/models/factory/ModelClassException.java
index 7718dd6..6ef5118 100644
--- a/src/main/java/org/apache/sling/models/factory/ModelClassException.java
+++ b/src/main/java/org/apache/sling/models/factory/ModelClassException.java
@@ -32,4 +32,8 @@ public final class ModelClassException extends RuntimeException {
public ModelClassException(String message) {
super(message);
}
+
+ public ModelClassException(String message, Throwable e) {
+ super(message, e);
+ }
}
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.