Skip to content

Commit 45ea1f0

Browse files
AlexTereshenkovcopybara-github
authored andcommitted
Prevent UrlRewriterParseException from being wrapped in RuntimeException (bazelbuild#28305)
When attempting to run a Bazel command, Bazel server will shutdown and exit with an unexpected error when the [downloader config](https://bazel.build/reference/command-line-reference#common_options-flag--downloader_config) is invalid: ``` $ echo "#test\nfoo" > bazel_downloader.cfg $ cat bazel_downloader.cfg #test foo $ bazel-dev build //... --downloader_config=bazel_downloader.cfg < exits immediately with the Bazel server killed > $ echo $? 37 ``` `37` corresponds to `Unhandled Exception / Internal Bazel Error` as per the [docs](https://bazel.build/run/scripts#exit-codes). This PR introduces changes that will let catch and rethrow `UrlRewriterParseException` before `Closer.rethrow()` to prevent it from being wrapped in `RuntimeException`. ``` $ bazel-dev-fixed query //... --downloader_config=bazel_downloader.cfg ... ERROR: Failed to parse downloader config at bazel_downloader.cfg:2: Unable to parse: foo $ echo $? 2 ``` Closes bazelbuild#28305. PiperOrigin-RevId: 891501161 Change-Id: I3cbfe2e2cdcad14c6b5e8210e2e1b8e999eba0ae
1 parent 1527433 commit 45ea1f0

2 files changed

Lines changed: 5 additions & 1 deletion

File tree

src/main/java/com/google/devtools/build/lib/bazel/repository/downloader/UrlRewriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public static UrlRewriter getDownloaderUrlRewriter(
125125
return new UrlRewriter(
126126
configPaths.stream().map(PathFragment::getPathString).toList(), readers);
127127
} catch (Throwable e) {
128-
throw closer.rethrow(e);
128+
throw closer.rethrow(e, UrlRewriterParseException.class);
129129
} finally {
130130
closer.close();
131131
}

src/test/java/com/google/devtools/build/lib/bazel/repository/downloader/UrlRewriterTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,15 @@ public void rewritingUrlsIsAppliedBeforeAllowing() throws Exception {
261261
@Test
262262
public void parseError() throws Exception {
263263
String config = "#comment\nhello";
264+
assertThrows(
265+
UrlRewriterParseException.class,
266+
() -> testUrlRewriter("/some/file", new StringReader(config)));
264267
try {
265268
new UrlRewriterConfig("/some/file", new StringReader(config));
266269
fail();
267270
} catch (UrlRewriterParseException e) {
268271
assertThat(e.getLocation()).isEqualTo(Location.fromFileLineColumn("/some/file", 2, 0));
272+
assertThat(e.getMessage()).contains("Unable to parse: hello");
269273
}
270274
}
271275

0 commit comments

Comments
 (0)