Skip to content

Commit ab5c3b1

Browse files
committed
SLING-12834 Expose the cause of PersistenceException in its getMessage()
This is beneficial for cases where the stacktrace is not available (e.g. in UI using PersistenceException.getMessage() or PersistenceException.toString()) like SlingPostServlets HtmlResponse.
1 parent 5767e54 commit ab5c3b1

2 files changed

Lines changed: 73 additions & 0 deletions

File tree

src/main/java/org/apache/sling/api/resource/PersistenceException.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.apache.sling.api.resource;
2020

2121
import java.io.IOException;
22+
import java.util.Set;
2223

2324
/**
2425
* This exception will be thrown during the commit to persist
@@ -29,6 +30,8 @@ public class PersistenceException extends IOException {
2930

3031
private static final long serialVersionUID = 2454225989618227698L;
3132

33+
private static final Set<Character> PUNCTUATION = Set.of('.', ':', '!');
34+
3235
/** Optional resource path. */
3336
private final String resourcePath;
3437

@@ -91,4 +94,34 @@ public String getResourcePath() {
9194
public String getPropertyName() {
9295
return this.propertyName;
9396
}
97+
98+
/**
99+
* Returns a message for this exception.
100+
* If the cause is not null, it will be appended to the message.
101+
* @return The message for this exception
102+
*/
103+
@Override
104+
public String getMessage() {
105+
StringBuilder sb = new StringBuilder();
106+
if (this.getCause() != null) {
107+
sb.append(stripTailingPunctuation(super.getMessage()))
108+
.append(" caused by ")
109+
.append(this.getCause().getMessage());
110+
} else {
111+
sb.append(super.getMessage());
112+
}
113+
114+
return sb.toString();
115+
}
116+
117+
private static String stripTailingPunctuation(String str) {
118+
if (str == null || str.isEmpty()) {
119+
return str;
120+
}
121+
int len = str.length();
122+
if (len > 0 && PUNCTUATION.contains(str.charAt(len - 1))) {
123+
return str.substring(0, len - 1);
124+
}
125+
return str;
126+
}
94127
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.sling.api.resource;
20+
21+
import javax.jcr.RepositoryException;
22+
23+
import org.junit.Test;
24+
25+
import static org.junit.Assert.assertEquals;
26+
27+
public class PersistenceExceptionTest {
28+
29+
@Test
30+
public void testGetMessage() {
31+
PersistenceException e = new PersistenceException("Test message");
32+
assertEquals("Test message", e.getMessage());
33+
e = new PersistenceException("Test message", new RepositoryException("JCR error"));
34+
assertEquals("Test message caused by JCR error", e.getMessage());
35+
e = new PersistenceException("Test message!", new RepositoryException("JCR error"));
36+
assertEquals("Test message caused by JCR error", e.getMessage());
37+
e = new PersistenceException("Test message.", new RepositoryException("JCR error"));
38+
assertEquals("Test message caused by JCR error", e.getMessage());
39+
}
40+
}

0 commit comments

Comments
 (0)