Skip to content

Commit

Permalink
Print all known values when an invalid range is given.
Browse files Browse the repository at this point in the history
For easier root cause analysis when an invalid request is passed.

PiperOrigin-RevId: 613110312
  • Loading branch information
java-team-github-bot authored and google-java-format Team committed Mar 6, 2024
1 parent f20d393 commit 823d7c9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -573,9 +573,11 @@ Range<Integer> characterRangeToTokenRange(Range<Integer> characterRange)
if (characterRange.upperEndpoint() > text.length()) {
throw new FormatterException(
String.format(
"error: invalid length %d, offset + length (%d) is outside the file",
"error: invalid offset (%d) or length (%d); offset + length (%d) > file length (%d)",
characterRange.lowerEndpoint(),
characterRange.upperEndpoint() - characterRange.lowerEndpoint(),
characterRange.upperEndpoint()));
characterRange.upperEndpoint(),
text.length()));
}
// empty range stands for "format the line under the cursor"
Range<Integer> nonEmptyRange =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void testFormatAosp() throws Exception {

Path tmpdir = testFolder.newFolder().toPath();
Path path = tmpdir.resolve("A.java");
Files.write(path, input.getBytes(UTF_8));
Files.writeString(path, input);

StringWriter out = new StringWriter();
StringWriter err = new StringWriter();
Expand Down Expand Up @@ -116,7 +116,7 @@ public void testFormatLengthUpToEOF() throws Exception {

Path tmpdir = testFolder.newFolder().toPath();
Path path = tmpdir.resolve("Foo.java");
Files.write(path, input.getBytes(UTF_8));
Files.writeString(path, input);

StringWriter out = new StringWriter();
StringWriter err = new StringWriter();
Expand All @@ -133,7 +133,7 @@ public void testFormatLengthOutOfRange() throws Exception {

Path tmpdir = testFolder.newFolder().toPath();
Path path = tmpdir.resolve("Foo.java");
Files.write(path, input.getBytes(UTF_8));
Files.writeString(path, input);

StringWriter out = new StringWriter();
StringWriter err = new StringWriter();
Expand All @@ -142,7 +142,25 @@ public void testFormatLengthOutOfRange() throws Exception {
String[] args = {"--offset", "0", "--length", "9999", path.toString()};
assertThat(main.format(args)).isEqualTo(1);
assertThat(err.toString())
.contains("error: invalid length 9999, offset + length (9999) is outside the file");
.contains("error: invalid offset (0) or length (9999); offset + length (9999)");
}

@Test
public void testFormatOffsetOutOfRange() throws Exception {
String input = "class Foo{}\n";

Path tmpdir = testFolder.newFolder().toPath();
Path path = tmpdir.resolve("Foo.java");
Files.writeString(path, input);

StringWriter out = new StringWriter();
StringWriter err = new StringWriter();

Main main = new Main(new PrintWriter(out, true), new PrintWriter(err, true), System.in);
String[] args = {"--offset", "9998", "--length", "1", path.toString()};
assertThat(main.format(args)).isEqualTo(1);
assertThat(err.toString())
.contains("error: invalid offset (9998) or length (1); offset + length (9999)");
}

@Test
Expand Down Expand Up @@ -303,7 +321,7 @@ private void importOrdering(String sortArg, String outputResourceName)
String inputResourceName = "com/google/googlejavaformat/java/testimports/A.input";
String input = getResource(inputResourceName);
String expectedOutput = getResource(outputResourceName);
Files.write(path, input.getBytes(UTF_8));
Files.writeString(path, input);

StringWriter out = new StringWriter();
StringWriter err = new StringWriter();
Expand Down

0 comments on commit 823d7c9

Please sign in to comment.