Skip to content

Commit 482a49f

Browse files
authored
Improve size limiting string message (#122427)
SizeLimitingStringWriter is used to limit the output size of mustache scripts. When the size is limited, the resulting exception lacks detail needed to identify the problem. In particular, the actual size that would result is not given. Additionally, the excerpt lacks any of the new string being added. This commit tweaks the exception to include both of these details.
1 parent f8aa047 commit 482a49f

File tree

4 files changed

+48
-14
lines changed

4 files changed

+48
-14
lines changed

docs/changelog/122427.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 122427
2+
summary: Improve size limiting string message
3+
area: Infra/Core
4+
type: enhancement
5+
issues: []

modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/MustacheScriptEngineTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ public void testResultSizeLimit() throws IOException {
423423
ex.getCause().getCause(),
424424
allOf(
425425
instanceOf(SizeLimitingStringWriter.SizeLimitExceededException.class),
426-
transformedMatch(Throwable::getMessage, endsWith("has exceeded the size limit [1024]"))
426+
transformedMatch(Throwable::getMessage, endsWith("has size [1030] which exceeds the size limit [1024]"))
427427
)
428428
);
429429
}

server/src/main/java/org/elasticsearch/common/text/SizeLimitingStringWriter.java

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,39 +30,59 @@ public SizeLimitingStringWriter(int sizeLimit) {
3030
this.sizeLimit = sizeLimit;
3131
}
3232

33-
private void checkSizeLimit(int additionalChars) {
34-
int bufLen = getBuffer().length();
35-
if (bufLen + additionalChars > sizeLimit) {
36-
throw new SizeLimitExceededException(
37-
Strings.format("String [%s...] has exceeded the size limit [%s]", getBuffer().substring(0, Math.min(bufLen, 20)), sizeLimit)
38-
);
33+
private int limitSize(int additionalChars) {
34+
int neededSize = getBuffer().length() + additionalChars;
35+
if (neededSize > sizeLimit) {
36+
return additionalChars - (neededSize - sizeLimit);
3937
}
38+
return additionalChars;
39+
}
40+
41+
private void throwSizeLimitExceeded(int limitedChars, int requestedChars) {
42+
assert limitedChars < requestedChars;
43+
int bufLen = getBuffer().length();
44+
int foundSize = bufLen - limitedChars + requestedChars; // reconstitute original
45+
String selection = getBuffer().substring(0, Math.min(bufLen, 20));
46+
throw new SizeLimitExceededException(
47+
Strings.format("String [%s...] has size [%d] which exceeds the size limit [%d]", selection, foundSize, sizeLimit)
48+
);
4049
}
4150

4251
@Override
4352
public void write(int c) {
44-
checkSizeLimit(1);
53+
if (limitSize(1) != 1) {
54+
throwSizeLimitExceeded(0, 1);
55+
}
4556
super.write(c);
4657
}
4758

4859
// write(char[]) delegates to write(char[], int, int)
4960

5061
@Override
5162
public void write(char[] cbuf, int off, int len) {
52-
checkSizeLimit(len);
53-
super.write(cbuf, off, len);
63+
int limitedLen = limitSize(len);
64+
if (limitedLen > 0) {
65+
super.write(cbuf, off, limitedLen);
66+
}
67+
if (limitedLen != len) {
68+
throwSizeLimitExceeded(limitedLen, len);
69+
}
5470
}
5571

5672
@Override
5773
public void write(String str) {
58-
checkSizeLimit(str.length());
59-
super.write(str);
74+
this.write(str, 0, str.length());
6075
}
6176

6277
@Override
6378
public void write(String str, int off, int len) {
64-
checkSizeLimit(len);
65-
super.write(str, off, len);
79+
int limitedLen = limitSize(len);
80+
if (limitedLen > 0) {
81+
super.write(str, off, limitedLen);
82+
}
83+
if (limitedLen != len) {
84+
throwSizeLimitExceeded(limitedLen, len);
85+
}
6686
}
6787

6888
// append(...) delegates to write(...) methods

server/src/test/java/org/elasticsearch/common/text/SizeLimitingStringWriterTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
import org.elasticsearch.test.ESTestCase;
1313

14+
import static org.hamcrest.Matchers.equalTo;
15+
1416
public class SizeLimitingStringWriterTests extends ESTestCase {
1517
public void testSizeIsLimited() {
1618
SizeLimitingStringWriter writer = new SizeLimitingStringWriter(10);
@@ -26,4 +28,11 @@ public void testSizeIsLimited() {
2628
expectThrows(SizeLimitingStringWriter.SizeLimitExceededException.class, () -> writer.append("a"));
2729
expectThrows(SizeLimitingStringWriter.SizeLimitExceededException.class, () -> writer.append("a", 0, 1));
2830
}
31+
32+
public void testLimitMessage() {
33+
SizeLimitingStringWriter writer = new SizeLimitingStringWriter(3);
34+
35+
var e = expectThrows(SizeLimitingStringWriter.SizeLimitExceededException.class, () -> writer.write("abcdefgh"));
36+
assertThat(e.getMessage(), equalTo("String [abc...] has size [8] which exceeds the size limit [3]"));
37+
}
2938
}

0 commit comments

Comments
 (0)