Skip to content

Commit

Permalink
Added error log statements to all catches.
Browse files Browse the repository at this point in the history
  • Loading branch information
Agadar committed Oct 29, 2019
1 parent 180e2b6 commit 3391106
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
/bin/
target\\nations.xml.gz
target\\regions.xml.gz
.factorypath
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,6 +23,7 @@
* @param <R> the type the child class' execute()-function returns
*/
@SuppressWarnings("rawtypes")
@Slf4j
public abstract class APIQuery<Q extends APIQuery, R> extends AbstractQuery<Q, R> {

/**
Expand Down Expand Up @@ -111,8 +113,7 @@ public <T> T execute(@NonNull Class<T> type) {
try {
validateQueryParameters();
return makeRequest(buildURL().replace(' ', '_'), type);
} catch (Exception ex) {
throw ex;

} finally {
getRateLimiter().unlock();
}
Expand Down Expand Up @@ -221,6 +222,7 @@ protected final <T> T makeRequest(String urlStr, Class<T> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -13,6 +15,7 @@
* @param <R> the type the child class' execute()-function returns
*/
@SuppressWarnings("rawtypes")
@Slf4j
public abstract class AbstractQuery<Q extends AbstractQuery, R> {

/**
Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -29,6 +30,7 @@
* @param <R> the type the child class' execute()-function returns
*/
@SuppressWarnings("rawtypes")
@Slf4j
public abstract class DailyDumpQuery<Q extends DailyDumpQuery, R> extends AbstractQuery<Q, R> {

/**
Expand Down Expand Up @@ -188,6 +190,7 @@ private Collection<R> 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
Expand Down Expand Up @@ -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
Expand All @@ -254,6 +258,7 @@ private Collection<R> 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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<NationDumpQuery, Nation> {

public NationDumpQuery(String baseUrl, String userAgent, String defaultDirectory, DailyDumpMode mode, Predicate<Nation> filter) {
Expand All @@ -39,6 +42,7 @@ protected Collection<Nation> 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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<RegionDumpQuery, Region> {

public RegionDumpQuery(String baseUrl, String userAgent, String defaultDirectory, DailyDumpMode mode, Predicate<Region> filter) {
Expand All @@ -35,10 +38,11 @@ protected String getFileName() {
protected Collection<Region> 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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TelegramQuery, Void> {

/**
Expand Down Expand Up @@ -133,6 +135,7 @@ public <T> T execute(Class<T> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

/**
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

/**
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -71,6 +75,7 @@ public final <T> T xmlToObject(InputStream xml, Class<T> toType) {
JAXBElement<T> 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);
}
}
Expand All @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ public void run() {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
e.printStackTrace();
}
rateLimiter.unlock();
}
Expand Down

0 comments on commit 3391106

Please sign in to comment.