From 013ac22fa788e5996941740e6e492958590e65ba Mon Sep 17 00:00:00 2001 From: BLCK <123077751+BLCK-B@users.noreply.github.com> Date: Tue, 28 May 2024 18:16:46 +0200 Subject: [PATCH] manager error refactor --- .../Scraping/ScraperGenericException.java | 5 ++ .../Scraping/ScraperManager.java | 53 +++++++------------ .../Scraping/ScraperTimeoutException.java | 5 ++ .../Scraping/ScraperManagerTest.java | 2 +- 4 files changed, 30 insertions(+), 35 deletions(-) diff --git a/src/main/java/com/blck/MusicReleaseTracker/Scraping/ScraperGenericException.java b/src/main/java/com/blck/MusicReleaseTracker/Scraping/ScraperGenericException.java index e84e680..3ecd8d9 100644 --- a/src/main/java/com/blck/MusicReleaseTracker/Scraping/ScraperGenericException.java +++ b/src/main/java/com/blck/MusicReleaseTracker/Scraping/ScraperGenericException.java @@ -6,4 +6,9 @@ public ScraperGenericException(String url) { super(url); } + @Override + public String toString() { + return "generic exception"; + } + } diff --git a/src/main/java/com/blck/MusicReleaseTracker/Scraping/ScraperManager.java b/src/main/java/com/blck/MusicReleaseTracker/Scraping/ScraperManager.java index 850acf3..ebcfe66 100644 --- a/src/main/java/com/blck/MusicReleaseTracker/Scraping/ScraperManager.java +++ b/src/main/java/com/blck/MusicReleaseTracker/Scraping/ScraperManager.java @@ -40,42 +40,12 @@ public int loadWithScrapers() { public int scrapeNext() { double startTime = System.currentTimeMillis(); Scraper scraper = scrapers.getFirst(); - for (int i = 0; i <= 2; i++) { + for (int i = 0; i <= 2; ++i) { try { scraper.scrape(jsoupTimeout); - break; // exception = will not break - } - catch (ScraperTimeoutException e) { - switch (i) { - case 0 -> log.error(e, ErrorLogging.Severity.INFO, scraper + " time out"); - case 1 -> log.error(e, ErrorLogging.Severity.INFO, scraper + " second time out"); - default -> { - log.error(e, ErrorLogging.Severity.INFO, "final time out, disabling source " + scraper); - // remove all scrapers of a faulty source - scrapers.removeIf(s -> s.getClass().equals(scraper.getClass())); - } - } - try { - Thread.sleep(minDelay); - } catch (InterruptedException ex) { - throw new RuntimeException(ex); - } - } - catch (Exception e) { - switch (i) { - case 0 -> log.error(e, ErrorLogging.Severity.WARNING, scraper + " error of scraper"); - case 1 -> log.error(e, ErrorLogging.Severity.WARNING, scraper + " second error of scraper"); - default -> { - log.error(e, ErrorLogging.Severity.WARNING, "final error of scraper, disabling source " + scraper); - // remove all scrapers of a faulty source - scrapers.removeIf(s -> s.getClass().equals(scraper.getClass())); - } - } - try { - Thread.sleep(minDelay); - } catch (InterruptedException ex) { - throw new RuntimeException(ex); - } + break; + } catch (Exception e) { + scrapeErrorLaunder(i, scraper, e); } if (scrapers.isEmpty()) return 0; @@ -94,6 +64,21 @@ public int scrapeNext() { return scrapers.size(); } + private void scrapeErrorLaunder(int i, Scraper scraper, Exception e) { + if (e instanceof ScraperTimeoutException) + log.error(e, ErrorLogging.Severity.INFO, scraper + " scraper threw " + e + " " + i + " times"); + else + log.error(e, ErrorLogging.Severity.WARNING, scraper + " scraper threw " + e + " " + i + " times"); + // remove scrapers of a faulty source + if (i == 2) + scrapers.removeIf(s -> s.getClass().equals(scraper.getClass())); + try { + Thread.sleep(minDelay); + } catch (InterruptedException ex) { + throw new RuntimeException(ex); + } + } + public long delays(String source) { long waitTime = (long) (minDelay - sourceTimes.get(source)); if (waitTime > 0) { diff --git a/src/main/java/com/blck/MusicReleaseTracker/Scraping/ScraperTimeoutException.java b/src/main/java/com/blck/MusicReleaseTracker/Scraping/ScraperTimeoutException.java index 4564b6a..a2b5c5a 100644 --- a/src/main/java/com/blck/MusicReleaseTracker/Scraping/ScraperTimeoutException.java +++ b/src/main/java/com/blck/MusicReleaseTracker/Scraping/ScraperTimeoutException.java @@ -6,4 +6,9 @@ public ScraperTimeoutException(String url) { super(url); } + @Override + public String toString() { + return "time out exception"; + } + } \ No newline at end of file diff --git a/src/test/java/com/blck/MusicReleaseTracker/Scraping/ScraperManagerTest.java b/src/test/java/com/blck/MusicReleaseTracker/Scraping/ScraperManagerTest.java index 320672b..9ded374 100644 --- a/src/test/java/com/blck/MusicReleaseTracker/Scraping/ScraperManagerTest.java +++ b/src/test/java/com/blck/MusicReleaseTracker/Scraping/ScraperManagerTest.java @@ -103,7 +103,7 @@ void erroringSourceCausesWarningLogAndIsRemoved() throws ScraperTimeoutException assertEquals(4, scraperManager.loadWithScrapers()); assertEquals(1, scraperManager.scrapeNext()); assertEquals(0, scraperManager.scrapeNext()); - verify(log, times(6)).error(any(), eq(ErrorLogging.Severity.WARNING), contains("error")); + verify(log, times(6)).error(any(), eq(ErrorLogging.Severity.WARNING), contains("generic")); } @Test