Skip to content

Commit 840e9da

Browse files
committed
fix: improved handling of search API errors
1 parent 5a481f5 commit 840e9da

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/main/java/org/codejive/jpm/Main.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,14 @@ public Integer call() throws Exception {
169169
if (max == null) {
170170
max = (Integer) 20;
171171
}
172-
String[] artifactNames = search(artifactPattern);
173-
if (artifactNames.length > 0) {
174-
Arrays.stream(artifactNames).forEach(System.out::println);
172+
try {
173+
String[] artifactNames = search(artifactPattern);
174+
if (artifactNames.length > 0) {
175+
Arrays.stream(artifactNames).forEach(System.out::println);
176+
}
177+
} catch (UncheckedIOException ex) {
178+
System.err.println(ex.getCause().getMessage());
179+
return 1;
175180
}
176181
}
177182
return (Integer) 0;

src/main/java/org/codejive/jpm/util/SearchUtils.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ private static SearchResult select(String query, int start, int count) throws IO
8484
try (CloseableHttpClient httpClient = HttpClients.custom().setUserAgent(agent).build()) {
8585
HttpGet request = new HttpGet(searchUrl);
8686
try (CloseableHttpResponse response = httpClient.execute(request)) {
87+
if (response.getStatusLine().getStatusCode() != 200) {
88+
throw new IOException(
89+
"Search failed: Maven Central Search API returned an error: "
90+
+ response.getStatusLine().getStatusCode()
91+
+ " "
92+
+ response.getStatusLine().getReasonPhrase());
93+
}
8794
DumperOptions dopts = new DumperOptions();
8895
Constructor cons = new Constructor(MvnSearchResult.class, new LoaderOptions());
8996
Representer representer = new Representer(dopts);
@@ -93,7 +100,8 @@ private static SearchResult select(String query, int start, int count) throws IO
93100
InputStreamReader rdr = new InputStreamReader(ins);
94101
MvnSearchResult result = yaml.load(rdr);
95102
if (result.responseHeader.status != 0) {
96-
throw new IOException("Search failed");
103+
throw new IOException(
104+
"Search failed: Maven Central Search API returned a response that could not be understood");
97105
}
98106
List<DefaultArtifact> artifacts =
99107
result.response.docs.stream()

0 commit comments

Comments
 (0)