Skip to content

Commit

Permalink
Added more error handling to holiday function
Browse files Browse the repository at this point in the history
  • Loading branch information
frossm committed Dec 1, 2023
1 parent ef4aa92 commit 51a10b3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>org.fross</groupId>
<artifactId>cal</artifactId>
<version>2.5.1</version>
<version>2.5.2</version>
<packaging>jar</packaging>

<name>cal</name>
Expand Down
2 changes: 1 addition & 1 deletion snap/snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: fcal
version: '2.5.1'
version: '2.5.2'
summary: Command line calendar display
description: |
fCal is a command line calendar utility. It will display a
Expand Down
37 changes: 24 additions & 13 deletions src/main/java/org/fross/cal/Holidays.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,24 @@ public static TreeMap<String, String> getHolidays(int year) {
// Build the ISO3 to ISO2 Country Map
buildCountryCodeMap();

// Debug
Output.debugPrintln("Locale Information: ");
Output.debugPrintln(" - ISO3 Code: " + locale.getISO3Country());
Output.debugPrintln(" - ISO2 Code: " + countryMap.get(locale.getISO3Country()));
Output.debugPrintln(" - Name: " + locale.getDisplayCountry());

// Build the URL and fetch the data
// https://date.nager.at/api/v3/publicholidays/2023/US
// TESTING: URL = URL + year + "/" + "MX";
URL = URL + year + "/" + countryMap.get(locale.getISO3Country());
try {
// Debug
Output.debugPrintln("Locale Information: ");
Output.debugPrintln(" - ISO3 Code: " + locale.getISO3Country());
Output.debugPrintln(" - ISO2 Code: " + countryMap.get(locale.getISO3Country()));
Output.debugPrintln(" - Name: " + locale.getDisplayCountry());

// Build the URL and fetch the data
// https://date.nager.at/api/v3/publicholidays/2023/US
URL = URL + year + "/" + countryMap.get(locale.getISO3Country());

} catch (Exception Ex) {
// Couldn't retrieve the holidays - turn off holiday display
Holidays.setDisplayHolidays(false);
Output.printColorln(Ansi.Color.RED, "It doesn't look like the following country is supported: '" + locale.getDisplayCountry() + "'\n");
return null;
}

Output.debugPrintln("URL to use: " + URL);

// Pull the JSON holidays from the website
Expand All @@ -77,7 +85,7 @@ public static TreeMap<String, String> getHolidays(int year) {
} catch (Exception ex) {
// Couldn't retrieve the holidays - turn off holiday display
Holidays.setDisplayHolidays(false);
Output.printColorln(Ansi.Color.RED, "Unable to retrieve holiday information from the internet\n");
Output.printColorln(Ansi.Color.RED, "Unable to retrieve holidays for " + locale.getDisplayCountry() + "\n");
return null;
}

Expand All @@ -94,9 +102,12 @@ public static TreeMap<String, String> getHolidays(int year) {
for (int holidayEntry = 0; holidayEntry < gsonMap.length; holidayEntry++) {
holidays.put(gsonMap[holidayEntry].get("date").toString(), gsonMap[holidayEntry].get("localName").toString());
}

} catch (Exception ex) {
System.out.println(ex.getMessage());
System.out.println(ex.getStackTrace());
// Couldn't retrieve the holidays - turn off holiday display
Holidays.setDisplayHolidays(false);
Output.printColorln(Ansi.Color.RED, "Unable to process holidays for " + locale.getDisplayCountry() + "\n");
return null;
}

return holidays;
Expand Down

0 comments on commit 51a10b3

Please sign in to comment.