[java-metadata-aggregator] branch main updated: MDA-301 - Add detail to IllegalArgumentException handling in BaseAsValidator
Ian Young
ian at iay.org.uk
Tue Jan 30 16:37:41 UTC 2024
This is an automated email from the git hooks/post-receive script.
iay pushed a commit to branch main
in repository java-metadata-aggregator.
View the commit online:
http://git.shibboleth.net/view/?p=java-metadata-aggregator.git;a=commit;h=0ea335c538756219afc2f80f01c84be691164d1a
The following commit(s) were added to refs/heads/main by this push:
new 0ea335c MDA-301 - Add detail to IllegalArgumentException handling in BaseAsValidator
0ea335c is described below
commit 0ea335c538756219afc2f80f01c84be691164d1a
Author: Ian Young <ian at iay.org.uk>
AuthorDate: Tue Jan 30 14:49:32 2024 +0000
MDA-301 - Add detail to IllegalArgumentException handling in BaseAsValidator
https://shibboleth.atlassian.net/browse/MDA-301
---
.../metadata/validate/BaseAsValidator.java | 2 +-
.../metadata/validate/BaseValidator.java | 66 ++++++++++++++++++++-
.../metadata/validate/BaseAsValidatorTest.java | 68 ++++++++++++++++++++++
3 files changed, 132 insertions(+), 4 deletions(-)
diff --git a/mda-framework/src/main/java/net/shibboleth/metadata/validate/BaseAsValidator.java b/mda-framework/src/main/java/net/shibboleth/metadata/validate/BaseAsValidator.java
index fb8eeb7..62154b7 100644
--- a/mda-framework/src/main/java/net/shibboleth/metadata/validate/BaseAsValidator.java
+++ b/mda-framework/src/main/java/net/shibboleth/metadata/validate/BaseAsValidator.java
@@ -130,7 +130,7 @@ public abstract class BaseAsValidator<V, A> extends BaseValidator implements Val
return applyValidators(v, item);
} catch (final IllegalArgumentException e) {
if (isConversionRequired()) {
- addErrorMessage(t, item, stageId);
+ addErrorMessage(t, item, stageId, e);
return Action.DONE;
} else {
return Action.CONTINUE;
diff --git a/mda-framework/src/main/java/net/shibboleth/metadata/validate/BaseValidator.java b/mda-framework/src/main/java/net/shibboleth/metadata/validate/BaseValidator.java
index 3b8d35c..6a624ec 100644
--- a/mda-framework/src/main/java/net/shibboleth/metadata/validate/BaseValidator.java
+++ b/mda-framework/src/main/java/net/shibboleth/metadata/validate/BaseValidator.java
@@ -15,6 +15,7 @@
package net.shibboleth.metadata.validate;
import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.ThreadSafe;
@@ -129,11 +130,53 @@ public abstract class BaseValidator extends AbstractIdentifiableInitializableCom
}
}
+ /**
+ * Formats the message with the given subject.
+ *
+ * @param subject subject of the validation, to be provided as an argument to the
+ * formatting string.
+ * @return formatted message
+ *
+ * @since 0.10.0
+ */
+ private @Nonnull String formatMessage(final @Nonnull Object subject) {
+ final String mess = String.format(getMessage(), subject);
+ assert mess != null;
+ return mess;
+ }
+
+ /**
+ * Formats the message with the given subject.
+ *
+ * <p>
+ * If the supplied {@link Throwable} has a non-<code>null</code> detail
+ * message, this is added to the resulting formatted string.
+ * </p>
+ *
+ * @param subject subject of the validation, to be provided as an argument to the
+ * formatting string.
+ * @param cause a {@link Throwable} which is to be interpreted as the cause of the
+ * validation failure.
+ * @return formatted message
+ *
+ * @since 0.10.0
+ */
+ private @Nonnull String formatMessage(final @Nonnull Object subject, final @Nonnull Throwable cause) {
+ final @Nullable var cmsg = cause.getMessage();
+ if (cmsg == null) {
+ return formatMessage(subject);
+ } else {
+ return formatMessage(subject) + " (" + cmsg + ")";
+ }
+ }
+
/**
* Add an {@link ErrorStatus} to the given {@link Item}.
*
+ * <p>
* The status message included in the {@link ErrorStatus} is generated
* by formatting the provided value with the {@link #message} field.
+ * </p>
*
* @param extra extra value to include in the status metadata
* @param item {@link Item} to add the status metadata to
@@ -143,9 +186,26 @@ public abstract class BaseValidator extends AbstractIdentifiableInitializableCom
*/
protected void addErrorMessage(@Nonnull final Object extra, @Nonnull final Item<?> item,
@Nonnull final String stageId) {
- final String mess = String.format(getMessage(), extra);
- assert mess != null;
- addError(mess, item, stageId);
+ addError(formatMessage(extra), item, stageId);
}
+ /**
+ * Add an {@link ErrorStatus} to the given {@link Item}.
+ *
+ * <p>
+ * The status message included in the {@link ErrorStatus} is generated
+ * by formatting the provided value with the {@link #message} field.
+ * </p>
+ *
+ * @param extra extra value to include in the status metadata
+ * @param item {@link Item} to add the status metadata to
+ * @param stageId component identifier for the calling stage
+ * @param cause reason for the validation failure, as a {@link Throwable}
+ *
+ * @since 0.10.0
+ */
+ protected void addErrorMessage(@Nonnull final Object extra, @Nonnull final Item<?> item,
+ @Nonnull final String stageId, @Nonnull final Throwable cause) {
+ addError(formatMessage(extra, cause), item, stageId);
+ }
}
diff --git a/mda-framework/src/test/java/net/shibboleth/metadata/validate/BaseAsValidatorTest.java b/mda-framework/src/test/java/net/shibboleth/metadata/validate/BaseAsValidatorTest.java
new file mode 100644
index 0000000..bea4466
--- /dev/null
+++ b/mda-framework/src/test/java/net/shibboleth/metadata/validate/BaseAsValidatorTest.java
@@ -0,0 +1,68 @@
+package net.shibboleth.metadata.validate;
+
+import javax.annotation.Nonnull;
+
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+import net.shibboleth.metadata.ErrorStatus;
+import net.shibboleth.metadata.testing.MockItem;
+import net.shibboleth.metadata.validate.Validator.Action;
+
+public class BaseAsValidatorTest {
+
+
+ /**
+ * Test an "as" validator which always fails to convert.
+ */
+ private class BoomValidator extends BaseAsValidator<String, String> {
+
+ @Override
+ protected @Nonnull String convert(@Nonnull String from) throws IllegalArgumentException {
+ if (from.equals("nothing")) {
+ throw new IllegalArgumentException();
+ } else {
+ throw new IllegalArgumentException("something");
+ }
+ }
+
+ }
+
+ @Test
+ public void convertTestNoDetail() throws Exception {
+
+ final var validator = new BoomValidator();
+ validator.setId("test");
+ validator.initialize();
+
+ final var item = new MockItem("test");
+
+ final var res = validator.validate("nothing", item, "stage");
+ Assert.assertEquals(res, Action.DONE);
+
+ final var errors = item.getItemMetadata().get(ErrorStatus.class);
+ Assert.assertEquals(errors.size(), 1);
+ final var err = errors.get(0);
+ Assert.assertEquals(err.getStatusMessage(), "value rejected: 'nothing'");
+ Assert.assertEquals(err.getComponentId(), "stage/test");
+ }
+
+ @Test
+ public void convertTestWithDetail() throws Exception {
+
+ final var validator = new BoomValidator();
+ validator.setId("test");
+ validator.initialize();
+
+ final var item = new MockItem("test");
+
+ final var res = validator.validate("not nothing", item, "stage");
+ Assert.assertEquals(res, Action.DONE);
+
+ final var errors = item.getItemMetadata().get(ErrorStatus.class);
+ Assert.assertEquals(errors.size(), 1);
+ final var err = errors.get(0);
+ Assert.assertEquals(err.getStatusMessage(), "value rejected: 'not nothing' (something)");
+ Assert.assertEquals(err.getComponentId(), "stage/test");
+ }
+}
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list