You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by tm...@apache.org on 2021/08/12 23:01:57 UTC

[sling-org-apache-sling-distribution-core] 01/01: SLING-10097 - Surface latest importer error as an exception

This is an automated email from the ASF dual-hosted git repository.

tmaret pushed a commit to branch SLING-10097
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-distribution-core.git

commit 25ca03bf4d0a8dd0be2418b25f55d9725dcb1e92
Author: tmaret <tm...@adobe.com>
AuthorDate: Fri Aug 13 01:01:29 2021 +0200

    SLING-10097 - Surface latest importer error as an exception
---
 .../serialization/impl/vlt/ErrorListener.java      | 49 ++++++++++++++++++
 .../impl/vlt/FileVaultContentSerializer.java       |  5 +-
 .../serialization/impl/vlt/ErrorListenerTest.java  | 60 ++++++++++++++++++++++
 3 files changed, 112 insertions(+), 2 deletions(-)

diff --git a/src/main/java/org/apache/sling/distribution/serialization/impl/vlt/ErrorListener.java b/src/main/java/org/apache/sling/distribution/serialization/impl/vlt/ErrorListener.java
new file mode 100644
index 0000000..99de55f
--- /dev/null
+++ b/src/main/java/org/apache/sling/distribution/serialization/impl/vlt/ErrorListener.java
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.distribution.serialization.impl.vlt;
+
+import org.apache.jackrabbit.vault.fs.api.ProgressTrackerListener;
+import org.apache.jackrabbit.vault.packaging.PackageException;
+
+import static java.lang.String.format;
+
+/**
+ * Keeps track of the latest error
+ */
+public class ErrorListener implements ProgressTrackerListener {
+
+    private Exception last = new PackageException("Failed to import");
+
+    public Exception getLastError() {
+        return last;
+    }
+
+    @Override
+    public void onMessage(Mode mode, String action, String path) {
+    }
+
+    @Override
+    public void onError(Mode mode, String path, Exception e) {
+        last = exception(mode, path, e);
+    }
+
+    private Exception exception(Mode mode, String path, Exception e) {
+        return new PackageException(format("Failed to import %s (%s)", path, e.toString()), e);
+    }
+}
diff --git a/src/main/java/org/apache/sling/distribution/serialization/impl/vlt/FileVaultContentSerializer.java b/src/main/java/org/apache/sling/distribution/serialization/impl/vlt/FileVaultContentSerializer.java
index 2b7e1cb..71bded7 100644
--- a/src/main/java/org/apache/sling/distribution/serialization/impl/vlt/FileVaultContentSerializer.java
+++ b/src/main/java/org/apache/sling/distribution/serialization/impl/vlt/FileVaultContentSerializer.java
@@ -40,7 +40,6 @@ import org.apache.jackrabbit.vault.fs.io.ImportOptions;
 import org.apache.jackrabbit.vault.fs.io.Importer;
 import org.apache.jackrabbit.vault.fs.io.ZipStreamArchive;
 import org.apache.jackrabbit.vault.packaging.ExportOptions;
-import org.apache.jackrabbit.vault.packaging.PackageException;
 import org.apache.jackrabbit.vault.packaging.Packaging;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.distribution.DistributionRequest;
@@ -133,6 +132,8 @@ public class FileVaultContentSerializer implements DistributionContentSerializer
         try {
             session = getSession(resourceResolver);
             ImportOptions importOptions = VltUtils.getImportOptions(aclHandling, cugHandling, importMode, autosaveThreshold, strict);
+            ErrorListener errorListener = new ErrorListener();
+            importOptions.setListener(errorListener);
             Importer importer = new Importer(importOptions);
             archive = new ZipStreamArchive(inputStream);
             archive.open(false);
@@ -161,7 +162,7 @@ public class FileVaultContentSerializer implements DistributionContentSerializer
             // now import the content
             importer.run(archive, session, "/");
             if (importer.hasErrors() && importOptions.isStrict()) {
-                throw new PackageException("Errors during import.");
+                throw errorListener.getLastError();
             }
             if (session.hasPendingChanges()) {
                 session.save();
diff --git a/src/test/java/org/apache/sling/distribution/serialization/impl/vlt/ErrorListenerTest.java b/src/test/java/org/apache/sling/distribution/serialization/impl/vlt/ErrorListenerTest.java
new file mode 100644
index 0000000..e085bd2
--- /dev/null
+++ b/src/test/java/org/apache/sling/distribution/serialization/impl/vlt/ErrorListenerTest.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.distribution.serialization.impl.vlt;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.apache.jackrabbit.vault.fs.api.ProgressTrackerListener.Mode.PATHS;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+public class ErrorListenerTest {
+
+    private ErrorListener errorListener;
+
+    private static final String path = "/some/path";
+
+    private static final Exception exception = new Exception();
+
+    @Before
+    public void before() {
+        errorListener = new ErrorListener();
+    }
+
+    @Test
+    public void testDefaultErrorNotNull() {
+        assertNotNull(errorListener.getLastError());
+    }
+
+    @Test
+    public void testError() {
+        errorListener.onError(PATHS, path, exception);
+        assertEquals(exception, errorListener.getLastError().getCause());
+        assertTrue(errorListener.getLastError().getMessage().contains(path));
+    }
+
+    @Test
+    public void testLastError() {
+        errorListener.onError(PATHS, path, new Exception());
+        errorListener.onError(PATHS, path, exception);
+        assertEquals(exception, errorListener.getLastError().getCause());
+    }
+}
\ No newline at end of file