-
Notifications
You must be signed in to change notification settings - Fork 22
SLING-12834 Expose the cause of PersistenceException in its getMessage() #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kwin
wants to merge
1
commit into
master
Choose a base branch
from
feature/expose-persistence-exception-cause
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/test/java/org/apache/sling/api/resource/PersistenceExceptionTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 thegetMessage-method there then?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.