Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package org.apache.sling.api.resource;

import java.io.IOException;
import java.util.Objects;
import java.util.Set;

/**
* This exception will be thrown during the commit to persist
Expand All @@ -29,6 +31,8 @@ public class PersistenceException extends IOException {

private static final long serialVersionUID = 2454225989618227698L;

private static final Set<Character> PUNCTUATION = Set.of('.', ':', '!');

/** Optional resource path. */
private final String resourcePath;

Expand Down Expand Up @@ -91,4 +95,34 @@ public String getResourcePath() {
public String getPropertyName() {
return this.propertyName;
}

/**
* Returns a message for this exception.
* If the cause is not null, it will be appended to the message.
* @return The message for this exception
*/
@Override
public String getMessage() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel we should fix this in SlingPostServlet and in the appropriate UI where this goes wrong, now we're adding this override in PersistenceException, but what if another exception bubbles up somewhere, do we also need to fix the getMessage-method there then?

Copy link
Member Author

@kwin kwin Jun 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general I agree with your point of view, however for PersistenceException everywhere only a very generic message is used and almost all of them are triggered by downstream exceptions in the providers. Also we don’t control all places which evaluate the exception. Do you see any potential downsides of this except for deviating a bit from Java standard?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I would appreciate your opinion whether this should be fixed rather in the consumer or when throwing the exception, I don't really know to be honest. I have seen patterns like this https://github.com/apache/sling-org-apache-sling-jcr-resource/blob/fe35d53a1b0f8ff1a7616909f16539b759bd0bdd/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProvider.java#L540 which seem wrong to me as that clearly duplicates information. However always exposing the root cause for every exception in https://github.com/apache/sling-org-apache-sling-servlets-post/blob/7ff7280c7ba89592186ac76d33b481d73abba323/src/main/java/org/apache/sling/servlets/post/AbstractPostResponse.java#L286 might be too much as well...

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think something can be done in SlingPostServlet to provide more information about the failure.

The other question would be: Do we actually want to expose this information? Isn't it fine to have this detailed information about the system (that is a blackbox for / transparent to the user) in the logs and just let the user know that something went wrong internally?

Indeed, https://github.com/apache/sling-org-apache-sling-jcr-resource/blob/fe35d53a1b0f8ff1a7616909f16539b759bd0bdd/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProvider.java#L540 is also wrong to me, it should provide additional information and context as to why this PersistenceException was thrown, which is coming from an IllegalArgumentException. It should at least tell that it was trying to provide properties which were not ignored, specifically what key and value as that would tell a developer immediately which property was causing the issue.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One example where the root cause is helpful for less technical users is a PersistenceException for ResourceResolver.create(). The most common causes (at least with the JCR provider) are:
a) Resource with that name does already exist
b) User does not have permission to create a resource there or
c) Resource with the given type is not allowed there

I think this is crucial to transmit even to end users because at least a) can be solved by the user himself without involving IT.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this is hard to generalize. In some cases it is ok to expose this information to users, while in other situations it would expose details which makes it easier for an attacker.

StringBuilder sb = new StringBuilder();
if (this.getCause() != null && !Objects.equals(this.getCause().getMessage(), super.getMessage())) {
sb.append(stripTailingPunctuation(super.getMessage()))
.append(" caused by ")
.append(this.getCause().getMessage());
} else {
sb.append(super.getMessage());
}

return sb.toString();
}

private static String stripTailingPunctuation(String str) {
if (str == null || str.isEmpty()) {
return str;
}
int len = str.length();
if (len > 0 && PUNCTUATION.contains(str.charAt(len - 1))) {
return str.substring(0, len - 1);
}
return str;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.api.resource;

import javax.jcr.RepositoryException;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class PersistenceExceptionTest {

@Test
public void testGetMessage() {
Throwable cause = new RepositoryException("JCR error");
PersistenceException e = new PersistenceException("Test message");
assertEquals("Test message", e.getMessage());
e = new PersistenceException("Test message", cause);
assertEquals("Test message caused by JCR error", e.getMessage());
e = new PersistenceException("Test message!", cause);
assertEquals("Test message caused by JCR error", e.getMessage());
e = new PersistenceException("Test message.", cause);
assertEquals("Test message caused by JCR error", e.getMessage());
e = new PersistenceException(cause.getMessage(), cause);
assertEquals("JCR error", e.getMessage());
}
}