This is an automated email from the ASF dual-hosted git repository. tmaret pushed a commit to branch SLING-10095-1 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-distribution-journal.git
commit 8d16f90c17f7a7d3fe33d69d98b99405e10a5ec1 Author: tmaret <[email protected]> AuthorDate: Fri Dec 17 15:14:27 2021 +0100 SLING-10095 - Surface latest content package extractor error in the exception --- .../bookkeeper/ContentPackageExtractor.java | 20 ++++++- .../journal/bookkeeper/ErrorListener.java | 52 ++++++++++++++++ .../journal/bookkeeper/ErrorListenerTest.java | 70 ++++++++++++++++++++++ 3 files changed, 139 insertions(+), 3 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..90ead6d 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 @@ -18,11 +18,13 @@ */ package org.apache.sling.distribution.journal.bookkeeper; +import static java.lang.String.format; import static java.util.Objects.requireNonNull; import java.io.IOException; import java.util.List; +import javax.annotation.Nullable; import javax.jcr.Node; import javax.jcr.RepositoryException; import javax.jcr.Session; @@ -82,7 +84,7 @@ class ContentPackageExtractor { log.warn("Imported node {} does not exist. Skipping.", path); } } catch (Exception e) { - throw new DistributionException("Error trying to extract package at path " + path, e); + throw new DistributionException(format("Error trying to extract package at path %s because of '%s'", path, e.getMessage()), e); } } @@ -98,15 +100,19 @@ class ContentPackageExtractor { log.info("Content package received at {}. Starting import.\n", path); Session session = node.getSession(); JcrPackageManager packMgr = packageService.getPackageManager(session); + ErrorListener listener = new ErrorListener(); try (JcrPackage pack = packMgr.open(node)) { if (pack != null) { - installPackage(pack); + installPackage(pack, listener); } + } catch (PackageException e) { + failed(listener.getLastErrorMessage(), e); } } - private void installPackage(JcrPackage pack) throws RepositoryException, PackageException, IOException { + private void installPackage(JcrPackage pack, ErrorListener listener) throws RepositoryException, PackageException, IOException { ImportOptions opts = new ImportOptions(); + opts.setListener(listener); opts.setStrict(true); if (packageHandling == PackageHandling.Extract) { pack.extract(opts); @@ -115,4 +121,12 @@ class ContentPackageExtractor { } } + private void failed(@Nullable String errorMsg, PackageException e) throws PackageException { + if (errorMsg != null) { + throw new PackageException(errorMsg, e); + } else { + throw e; + } + } + } 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
