This is an automated email from the ASF dual-hosted git repository. tmaret pushed a commit to branch SLING-10095 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-distribution-journal.git
commit 61d00770146081b80d86ecddd224cd0b5e855401 Author: tmaret <[email protected]> AuthorDate: Fri Dec 17 11:32:34 2021 +0100 SLING-10095 - Surface latest content package extractor error in the exception --- .../bookkeeper/ContentPackageExtractor.java | 45 +++++++------- .../journal/bookkeeper/ErrorListener.java | 52 ++++++++++++++++ .../journal/bookkeeper/ErrorListenerTest.java | 70 ++++++++++++++++++++++ 3 files changed, 147 insertions(+), 20 deletions(-) diff --git a/src/main/java/org/apache/sling/distribution/journal/bookkeeper/ContentPackageExtractor.java b/src/main/java/org/apache/sling/distribution/journal/bookkeeper/ContentPackageExtractor.java index 1ec394c..f0df32b 100644 --- a/src/main/java/org/apache/sling/distribution/journal/bookkeeper/ContentPackageExtractor.java +++ b/src/main/java/org/apache/sling/distribution/journal/bookkeeper/ContentPackageExtractor.java @@ -20,12 +20,10 @@ package org.apache.sling.distribution.journal.bookkeeper; import static java.util.Objects.requireNonNull; -import java.io.IOException; import java.util.List; import javax.jcr.Node; import javax.jcr.RepositoryException; -import javax.jcr.Session; import javax.jcr.nodetype.NodeType; import org.apache.jackrabbit.vault.fs.io.ImportOptions; @@ -76,7 +74,28 @@ class ContentPackageExtractor { if (resource != null) { Node node = resource.adaptTo(Node.class); if (isContentPackage(node)) { - installPackage(path, node); + // Note that we inline the code to minimise + // the depth of the stack trace produced + log.info("Content package received at {}. Starting import.\n", path); + JcrPackageManager packMgr = packageService.getPackageManager(node.getSession()); + ErrorListener listener = new ErrorListener(); + try (JcrPackage pack = packMgr.open(node)) { + if (pack != null) { + ImportOptions opts = newImportOptions(listener); + if (packageHandling == PackageHandling.Extract) { + pack.extract(opts); + } else { + pack.install(opts); + } + } + } catch (PackageException e) { + String message = listener.getLastErrorMessage(); + if (message != null) { + throw new PackageException(message, e); + } else { + throw e; + } + } } } else { log.warn("Imported node {} does not exist. Skipping.", path); @@ -94,25 +113,11 @@ class ContentPackageExtractor { return node!= null && node.isNodeType(NodeType.NT_FILE); } - private void installPackage(String path, Node node) throws RepositoryException, PackageException, IOException { - log.info("Content package received at {}. Starting import.\n", path); - Session session = node.getSession(); - JcrPackageManager packMgr = packageService.getPackageManager(session); - try (JcrPackage pack = packMgr.open(node)) { - if (pack != null) { - installPackage(pack); - } - } - } - - private void installPackage(JcrPackage pack) throws RepositoryException, PackageException, IOException { + private ImportOptions newImportOptions(ErrorListener listener) { ImportOptions opts = new ImportOptions(); + opts.setListener(listener); opts.setStrict(true); - if (packageHandling == PackageHandling.Extract) { - pack.extract(opts); - } else { - pack.install(opts); - } + return opts; } } diff --git a/src/main/java/org/apache/sling/distribution/journal/bookkeeper/ErrorListener.java b/src/main/java/org/apache/sling/distribution/journal/bookkeeper/ErrorListener.java new file mode 100644 index 0000000..84ac7fd --- /dev/null +++ b/src/main/java/org/apache/sling/distribution/journal/bookkeeper/ErrorListener.java @@ -0,0 +1,52 @@ +/* + * 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.journal.bookkeeper; + +import javax.annotation.CheckForNull; + +import org.apache.jackrabbit.vault.fs.api.ProgressTrackerListener; + +import static java.lang.String.format; + +public class ErrorListener implements ProgressTrackerListener { + + private String errorMessage = null; + + /** + * @return the error message of the last FileVault error logged ; or + * {@code null} if no error message has been logged. + */ + @CheckForNull + public String getLastErrorMessage() { + return errorMessage; + } + + @Override + public void onMessage(Mode mode, String action, String path) { + } + + @Override + public void onError(Mode mode, String path, Exception e) { + errorMessage = message(mode, path, e); + } + + private String message(Mode mode, String path, Exception e) { + return format("Failed to import %s (%s)", path, e.toString()); + } +} diff --git a/src/test/java/org/apache/sling/distribution/journal/bookkeeper/ErrorListenerTest.java b/src/test/java/org/apache/sling/distribution/journal/bookkeeper/ErrorListenerTest.java new file mode 100644 index 0000000..a1a3295 --- /dev/null +++ b/src/test/java/org/apache/sling/distribution/journal/bookkeeper/ErrorListenerTest.java @@ -0,0 +1,70 @@ +/* + * 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.journal.bookkeeper; + +import org.junit.Before; +import org.junit.Test; + +import static org.apache.jackrabbit.vault.fs.api.ProgressTrackerListener.Mode.PATHS; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +public class ErrorListenerTest { + + private ErrorListener errorListener; + + private static final String path = "/some/path"; + + private static final String errorMsg = "Failed to import XYZ"; + + private static final Exception exception = new Exception(errorMsg); + + @Before + public void before() { + errorListener = new ErrorListener(); + } + + @Test + public void testDefaultErrorIsNull() { + assertNull(errorListener.getLastErrorMessage()); + } + + @Test + public void testError() { + errorListener.onError(PATHS, path, exception); + String message = errorListener.getLastErrorMessage(); + assertNotNull(message); + assertMessage(message); + } + + @Test + public void testLastError() { + errorListener.onError(PATHS, path, new Exception()); + errorListener.onError(PATHS, path, exception); + String message = errorListener.getLastErrorMessage(); + assertNotNull(message); + assertMessage(message); + } + + private void assertMessage(String message) { + assertTrue(message.contains(errorMsg)); + assertTrue(message.contains(path)); + } +} \ No newline at end of file
