Skip to content

Commit 232a3e5

Browse files
author
cube
committed
Improve error handling
1 parent ff93c47 commit 232a3e5

3 files changed

Lines changed: 21 additions & 5 deletions

File tree

src/main/java/com/chaudhuri/ooliteaddonscanner2/AddonsUtil.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,9 @@ public static void readShipModels(ExpansionCache cache, Expansion expansion) {
369369
if (expansion == null) {
370370
throw new IllegalArgumentException(EXCEPTION_EXPANSION_MUST_NOT_BE_NULL);
371371
}
372+
if (expansion.getDownloadUrl() == null) {
373+
throw new IllegalArgumentException("expansion must have download url");
374+
}
372375

373376
try {
374377
ZipInputStream zin = new ZipInputStream(new BufferedInputStream(cache.getPluginInputStream(expansion.getDownloadUrl())));

src/main/java/com/chaudhuri/ooliteaddonscanner2/ExpansionCache.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,15 +408,19 @@ public void update(String url) throws IOException, MalformedURLException, URISyn
408408
}
409409

410410
cacheMisses++;
411-
doDownload(u, localFile);
411+
try {
412+
doDownload(u, localFile);
413+
} catch (IOException e) {
414+
log.info("Could not download {} -> {}", u, localFile, e);
415+
}
412416
}
413417

414418
/**
415419
* Return the plugin's input stream.
416420
* If the file is not cached or too old it will be automatically downloaded.
417421
*
418422
* @param url the download url of the file
419-
* @return the inputstream (to the cached file on disk)
423+
* @return the inputstream (to the cached file on disk), or null if we just do not have it
420424
* @throws IOException something went wrong
421425
*/
422426
public InputStream getPluginInputStream(String url) throws IOException, MalformedURLException, URISyntaxException {
@@ -428,7 +432,11 @@ public InputStream getPluginInputStream(String url) throws IOException, Malforme
428432
update(url);
429433

430434
File cached = getCachedFile(url);
431-
return new FileInputStream(cached);
435+
if (cached.canRead()) {
436+
return new FileInputStream(cached);
437+
} else {
438+
return null;
439+
}
432440
}
433441

434442
/**

src/test/java/com/chaudhuri/ooliteaddonscanner2/AddonsUtilTest.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,8 +564,13 @@ public void testReadShipModels_ExpansionCache_Expansion3() throws IOException {
564564
Expansion expansion = new Expansion();
565565

566566
assertEquals(0, expansion.getWarnings().size());
567-
AddonsUtil.readShipModels(cache, expansion);
568-
assertEquals(1, expansion.getWarnings().size());
567+
try {
568+
AddonsUtil.readShipModels(cache, expansion);
569+
fail("expected exception");
570+
} catch (IllegalArgumentException e) {
571+
assertEquals("expansion must have download url", e.getMessage());
572+
log.debug("caught expected exception", e);
573+
}
569574
}
570575

571576
/**

0 commit comments

Comments
 (0)