From 3391106135ff12e473e0d57b193e260bd9c7e92f Mon Sep 17 00:00:00 2001 From: Agadar Date: Tue, 29 Oct 2019 17:54:47 +0100 Subject: [PATCH] Added error log statements to all catches. --- .gitignore | 1 + .../agadar/nationstates/DefaultNationStatesImpl.java | 1 + .../com/github/agadar/nationstates/query/APIQuery.java | 6 ++++-- .../agadar/nationstates/query/AbstractQuery.java | 5 ++++- .../agadar/nationstates/query/DailyDumpQuery.java | 5 +++++ .../agadar/nationstates/query/NationDumpQuery.java | 4 ++++ .../agadar/nationstates/query/RegionDumpQuery.java | 10 +++++++--- .../agadar/nationstates/query/TelegramQuery.java | 3 +++ .../nationstates/ratelimiter/NormalRateLimiter.java | 4 ++++ .../nationstates/xmlconverter/XmlConverterImpl.java | 6 ++++++ .../ratelimiter/NormalRateLimiterTest.java | 1 - 11 files changed, 39 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 0bea57b..25e7ac2 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ /bin/ target\\nations.xml.gz target\\regions.xml.gz +.factorypath \ No newline at end of file diff --git a/src/main/java/com/github/agadar/nationstates/DefaultNationStatesImpl.java b/src/main/java/com/github/agadar/nationstates/DefaultNationStatesImpl.java index 27f8e56..ce72ad1 100644 --- a/src/main/java/com/github/agadar/nationstates/DefaultNationStatesImpl.java +++ b/src/main/java/com/github/agadar/nationstates/DefaultNationStatesImpl.java @@ -113,6 +113,7 @@ public DefaultNationStatesImpl(@NonNull String userAgent) { File jarFile = new File(codeSource.getLocation().toURI().getPath()); defaultDumpDirectory = jarFile.getParentFile().getPath(); } catch (URISyntaxException ex) { + log.error("An error occured while deriving the default dump directory", ex); throw new NationStatesAPIException(ex); } } diff --git a/src/main/java/com/github/agadar/nationstates/query/APIQuery.java b/src/main/java/com/github/agadar/nationstates/query/APIQuery.java index 8355c1a..e3d941b 100644 --- a/src/main/java/com/github/agadar/nationstates/query/APIQuery.java +++ b/src/main/java/com/github/agadar/nationstates/query/APIQuery.java @@ -3,6 +3,7 @@ import com.github.agadar.nationstates.xmlconverter.XmlConverter; import lombok.NonNull; +import lombok.extern.slf4j.Slf4j; import com.github.agadar.nationstates.exception.NationStatesAPIException; import com.github.agadar.nationstates.exception.NationStatesResourceNotFoundException; @@ -22,6 +23,7 @@ * @param the type the child class' execute()-function returns */ @SuppressWarnings("rawtypes") +@Slf4j public abstract class APIQuery extends AbstractQuery { /** @@ -111,8 +113,7 @@ public T execute(@NonNull Class type) { try { validateQueryParameters(); return makeRequest(buildURL().replace(' ', '_'), type); - } catch (Exception ex) { - throw ex; + } finally { getRateLimiter().unlock(); } @@ -221,6 +222,7 @@ protected final T makeRequest(String urlStr, Class type) { throw new NationStatesAPIException(response); } } catch (IOException ex) { + log.error("An error occured while making a request to the API", ex); throw new NationStatesAPIException(ex); } finally { // Always close the HttpURLConnection diff --git a/src/main/java/com/github/agadar/nationstates/query/AbstractQuery.java b/src/main/java/com/github/agadar/nationstates/query/AbstractQuery.java index 5d2018b..0030e27 100644 --- a/src/main/java/com/github/agadar/nationstates/query/AbstractQuery.java +++ b/src/main/java/com/github/agadar/nationstates/query/AbstractQuery.java @@ -4,6 +4,8 @@ import java.io.InputStream; import java.lang.reflect.ParameterizedType; +import lombok.extern.slf4j.Slf4j; + /** * Top parent class for all Queries to NationStates in general. * @@ -13,6 +15,7 @@ * @param the type the child class' execute()-function returns */ @SuppressWarnings("rawtypes") +@Slf4j public abstract class AbstractQuery { /** @@ -82,7 +85,7 @@ protected final static void closeInputStreamQuietly(InputStream stream) { try { stream.close(); } catch (IOException ex) { - // Ignore. + log.error("An error occured while silently closing the input stream", ex); } } } diff --git a/src/main/java/com/github/agadar/nationstates/query/DailyDumpQuery.java b/src/main/java/com/github/agadar/nationstates/query/DailyDumpQuery.java index 2765083..c4bd477 100644 --- a/src/main/java/com/github/agadar/nationstates/query/DailyDumpQuery.java +++ b/src/main/java/com/github/agadar/nationstates/query/DailyDumpQuery.java @@ -19,6 +19,7 @@ import com.github.agadar.nationstates.exception.NationStatesResourceNotFoundException; import lombok.NonNull; +import lombok.extern.slf4j.Slf4j; /** * Query for retrieving daily dumps from NationStates. @@ -29,6 +30,7 @@ * @param the type the child class' execute()-function returns */ @SuppressWarnings("rawtypes") +@Slf4j public abstract class DailyDumpQuery extends AbstractQuery { /** @@ -188,6 +190,7 @@ private Collection makeRequest(String urlStr) { throw new NationStatesAPIException(response); } } catch (IOException ex) { + log.error("An error occured while making a request to the API", ex); throw new NationStatesAPIException(ex); } finally { // Always close the HttpURLConnection @@ -232,6 +235,7 @@ private void download(String directory) { throw new NationStatesAPIException(response); } } catch (IOException ex) { + log.error("An error occured while downloading the dump file", ex); throw new NationStatesAPIException(ex); } finally { // Always close the HttpURLConnection @@ -254,6 +258,7 @@ private Collection readLocal(String directory) { closeInputStreamQuietly(stream); return obj; } catch (IOException ex) { + log.error("An error occured while reading the dump file", ex); throw new NationStatesAPIException(ex); } } diff --git a/src/main/java/com/github/agadar/nationstates/query/NationDumpQuery.java b/src/main/java/com/github/agadar/nationstates/query/NationDumpQuery.java index 8d8edde..bee3fd4 100644 --- a/src/main/java/com/github/agadar/nationstates/query/NationDumpQuery.java +++ b/src/main/java/com/github/agadar/nationstates/query/NationDumpQuery.java @@ -15,11 +15,14 @@ import com.github.agadar.nationstates.exception.NationStatesAPIException; import com.github.agadar.nationstates.xmlconverter.NationSaxHandler; +import lombok.extern.slf4j.Slf4j; + /** * Query for retrieving daily nation dumps from NationStates. * * @author Agadar (https://github.com/Agadar/) */ +@Slf4j public class NationDumpQuery extends DailyDumpQuery { public NationDumpQuery(String baseUrl, String userAgent, String defaultDirectory, DailyDumpMode mode, Predicate filter) { @@ -39,6 +42,7 @@ protected Collection translateResponse(GZIPInputStream stream) { saxParser.parse(stream, nationSaxHandler); return nationSaxHandler.filteredNations; } catch (SAXException | IOException | ParserConfigurationException ex) { + log.error("An error occured while parsing the dump file", ex); throw new NationStatesAPIException(ex); } } diff --git a/src/main/java/com/github/agadar/nationstates/query/RegionDumpQuery.java b/src/main/java/com/github/agadar/nationstates/query/RegionDumpQuery.java index ddc6176..e7a3dab 100644 --- a/src/main/java/com/github/agadar/nationstates/query/RegionDumpQuery.java +++ b/src/main/java/com/github/agadar/nationstates/query/RegionDumpQuery.java @@ -15,11 +15,14 @@ import com.github.agadar.nationstates.exception.NationStatesAPIException; import com.github.agadar.nationstates.xmlconverter.RegionSaxHandler; +import lombok.extern.slf4j.Slf4j; + /** * Query for retrieving daily region dumps from NationStates. * * @author Agadar (https://github.com/Agadar/) */ +@Slf4j public class RegionDumpQuery extends DailyDumpQuery { public RegionDumpQuery(String baseUrl, String userAgent, String defaultDirectory, DailyDumpMode mode, Predicate filter) { @@ -35,10 +38,11 @@ protected String getFileName() { protected Collection translateResponse(GZIPInputStream stream) { try { var saxParser = SAXParserFactory.newInstance().newSAXParser(); - var nationSaxHandler = new RegionSaxHandler(filter); - saxParser.parse(stream, nationSaxHandler); - return nationSaxHandler.filteredRegions; + var regionSaxHandler = new RegionSaxHandler(filter); + saxParser.parse(stream, regionSaxHandler); + return regionSaxHandler.filteredRegions; } catch (SAXException | IOException | ParserConfigurationException ex) { + log.error("An error occured while parsing the dump file", ex); throw new NationStatesAPIException(ex); } } diff --git a/src/main/java/com/github/agadar/nationstates/query/TelegramQuery.java b/src/main/java/com/github/agadar/nationstates/query/TelegramQuery.java index 6cfa4bc..9e85a64 100644 --- a/src/main/java/com/github/agadar/nationstates/query/TelegramQuery.java +++ b/src/main/java/com/github/agadar/nationstates/query/TelegramQuery.java @@ -10,12 +10,14 @@ import com.github.agadar.nationstates.xmlconverter.XmlConverter; import lombok.NonNull; +import lombok.extern.slf4j.Slf4j; /** * A query to the NationStates API's utility resource, sending (a) telegram(s). * * @author Agadar (https://github.com/Agadar/) */ +@Slf4j public class TelegramQuery extends APIQuery { /** @@ -133,6 +135,7 @@ public T execute(Class type) { try { makeRequest(url, type); } catch (Exception ex) { + log.error("An error occured while sending a telegram", ex); exception = ex; } finally { // Always unlock the rate limiter to prevent deadlock. diff --git a/src/main/java/com/github/agadar/nationstates/ratelimiter/NormalRateLimiter.java b/src/main/java/com/github/agadar/nationstates/ratelimiter/NormalRateLimiter.java index 040ca65..4f35a22 100644 --- a/src/main/java/com/github/agadar/nationstates/ratelimiter/NormalRateLimiter.java +++ b/src/main/java/com/github/agadar/nationstates/ratelimiter/NormalRateLimiter.java @@ -2,9 +2,12 @@ import java.util.concurrent.locks.ReentrantLock; +import lombok.extern.slf4j.Slf4j; + /** * @author Agadar (https://github.com/Agadar/) */ +@Slf4j public class NormalRateLimiter implements RateLimiter { /** @@ -65,6 +68,7 @@ public boolean lock() { Thread.sleep(sleepFor); } catch (InterruptedException ex) { // We were interrupted, so unlock to prevent a deadlock, then return false. + log.error("The sleeping thread was interrupted", ex); Thread.currentThread().interrupt(); lock.unlock(); return false; diff --git a/src/main/java/com/github/agadar/nationstates/xmlconverter/XmlConverterImpl.java b/src/main/java/com/github/agadar/nationstates/xmlconverter/XmlConverterImpl.java index 927d0af..3895e73 100644 --- a/src/main/java/com/github/agadar/nationstates/xmlconverter/XmlConverterImpl.java +++ b/src/main/java/com/github/agadar/nationstates/xmlconverter/XmlConverterImpl.java @@ -15,11 +15,14 @@ import com.github.agadar.nationstates.exception.NationStatesAPIException; +import lombok.extern.slf4j.Slf4j; + /** * Converts XML streams to properly annotated objects, and vice versa. * * @author Agadar (https://github.com/Agadar/) */ +@Slf4j public class XmlConverterImpl implements XmlConverter { /** @@ -50,6 +53,7 @@ public final void registerTypes(Class... types) { try { jaxbContext = JAXBContext.newInstance(JAXB_CONTEXT_CLASSES.toArray(new Class[numberOfClasses])); } catch (JAXBException ex) { + log.error("An error occured while (re)initialising the JAXB context", ex); throw new NationStatesAPIException(ex); } } @@ -71,6 +75,7 @@ public final T xmlToObject(InputStream xml, Class toType) { JAXBElement je1 = unmarshaller.unmarshal(xmlstream, toType); return je1.getValue(); } catch (JAXBException ex) { + log.error("An error occured while parsing a XML stream to an object", ex); throw new NationStatesAPIException(ex); } } @@ -90,6 +95,7 @@ public final ByteArrayOutputStream objectToXml(Object obj) { marshaller.marshal(obj, stream); return stream; } catch (JAXBException ex) { + log.error("An error occured while parsing an object to a XML stream", ex); throw new NationStatesAPIException(ex); } } diff --git a/src/test/java/com/github/agadar/nationstates/ratelimiter/NormalRateLimiterTest.java b/src/test/java/com/github/agadar/nationstates/ratelimiter/NormalRateLimiterTest.java index 9445f92..2b5b2f8 100644 --- a/src/test/java/com/github/agadar/nationstates/ratelimiter/NormalRateLimiterTest.java +++ b/src/test/java/com/github/agadar/nationstates/ratelimiter/NormalRateLimiterTest.java @@ -84,7 +84,6 @@ public void run() { Thread.sleep(1000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); - e.printStackTrace(); } rateLimiter.unlock(); }