Skip to content

Commit 5b35397

Browse files
committed
Improve error handling
1 parent f395549 commit 5b35397

File tree

6 files changed

+338
-270
lines changed

6 files changed

+338
-270
lines changed

src/com/joelchristophel/sourceradio/FileUtilities.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,26 @@ private FileUtilities() {
2323
* @param includeBlankLines
2424
* - indicates whether or not to include blank lines in list
2525
* @return the lines of the specified file or an empty list if the file was not found
26+
* @throws IOException
2627
*/
27-
static String[] getLines(String path, boolean includeBlankLines) {
28+
static String[] getLines(String path, boolean includeBlankLines) throws IOException {
2829
List<String> lines = new ArrayList<String>();
2930
File file = new File(path);
3031
if (file.exists()) {
31-
try (BufferedReader reader = new BufferedReader(new FileReader(new File(path)));) {
32-
String line = null;
33-
while ((line = reader.readLine()) != null) {
34-
if (!line.trim().isEmpty() || includeBlankLines) {
35-
lines.add(line);
36-
}
32+
BufferedReader reader = new BufferedReader(new FileReader(new File(path)));
33+
String line = null;
34+
while ((line = reader.readLine()) != null) {
35+
if (!line.trim().isEmpty() || includeBlankLines) {
36+
lines.add(line);
3737
}
38-
} catch (IOException e) {
39-
e.printStackTrace();
4038
}
39+
reader.close();
40+
4141
}
4242
return lines.toArray(new String[] {});
4343
}
4444

45-
static boolean fileHasLine(String path, String line, boolean commentAware) {
45+
static boolean fileHasLine(String path, String line, boolean commentAware) throws IOException {
4646
String[] lines = getLines(path, true);
4747
boolean hasLine = false;
4848
for (String fileLine : lines) {
@@ -61,8 +61,9 @@ static boolean fileHasLine(String path, String line, boolean commentAware) {
6161
* - the path to a file
6262
* @param line
6363
* - the line to append
64+
* @throws IOException
6465
*/
65-
static void appendLine(String path, String line) {
66+
static void appendLine(String path, String line) throws IOException {
6667
trimFile(path);
6768
line = line.startsWith(System.lineSeparator()) ? line : System.lineSeparator() + line;
6869
try {
@@ -101,8 +102,9 @@ public void run() {
101102
*
102103
* @param path
103104
* - the path to a file
105+
* @throws IOException
104106
*/
105-
static void trimFile(String path) {
107+
static void trimFile(String path) throws IOException {
106108
String[] lines = getLines(path, true);
107109
int endingBlankLines = 0;
108110
for (int i = lines.length - 1; i >= 0; i--) {
@@ -140,9 +142,10 @@ static void trimFile(String path) {
140142
* - the line to remove
141143
* @param commentAware
142144
* - indicates whether or not this method is aware that comments exist
145+
* @throws IOException
143146
* @returns <code>true</code> if the file contained the line to begin with; <code>false</code> otherwise
144147
*/
145-
static boolean removeLine(String path, String line, boolean commentAware) {
148+
static boolean removeLine(String path, String line, boolean commentAware) throws IOException {
146149
boolean hasLine = fileHasLine(path, line, commentAware);
147150
if (hasLine) {
148151
String[] lines = getLines(path, true);

src/com/joelchristophel/sourceradio/Game.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.joelchristophel.sourceradio;
22

33
import java.io.File;
4-
import java.io.FileNotFoundException;
4+
import java.io.IOException;
55

66
enum Game {
77
TEAM_FORTRESS_2("Team Fortress 2", 440, "steamapps\\common\\Team Fortress 2\\tf\\", "hl2.exe", false, true,
@@ -12,7 +12,8 @@ enum Game {
1212
StringResources.get("counterTerroristPattern"), " @ .+$" },
1313
new String[] { "CS:GO", "CSGO", "Counterstrike", "Counter-Strike" }),
1414
LEFT_4_DEAD_2("Left 4 Dead 2", 550, "steamapps\\common\\Left 4 Dead 2\\left4dead2\\", "left4dead2.exe", false,
15-
false, new String[] { StringResources.get("survivorPattern") }, new String[] { "L4D2", "Left for Dead 2" });
15+
false, new String[] { StringResources.get("survivorPattern") },
16+
new String[] { "L4D2", "LFD2", "Left for Dead 2" });
1617
private static Properties properties = Properties.getInstance();
1718
private static Game currentGame;
1819
private String friendlyName;
@@ -89,16 +90,23 @@ String getExeName() {
8990
return exeName;
9091
}
9192

92-
String getCfgPath() throws FileNotFoundException {
93-
String steamId3 = properties.getOwner().getSteamId3();
94-
String cfgPath = null;
95-
if (cfgInUserdata) {
96-
cfgPath = getSteamPath() + "userdata" + File.separator + steamId3 + File.separator + currentGame.id
97-
+ File.separator + "local" + File.separator + "cfg" + File.separator;
98-
} else {
99-
cfgPath = currentGame.getPath() + "cfg" + File.separator;
93+
String getCfgPath() throws IOException {
94+
try {
95+
String cfgPath = null;
96+
String steamId3 = properties.getOwner().getSteamId3();
97+
if (cfgInUserdata) {
98+
cfgPath = getSteamPath() + "userdata" + File.separator + steamId3 + File.separator + currentGame.id
99+
+ File.separator + "local" + File.separator + "cfg" + File.separator;
100+
} else {
101+
cfgPath = currentGame.getPath() + "cfg" + File.separator;
102+
}
103+
if (!new File(cfgPath).exists()) {
104+
throw new IOException("Error: The game's expected cfg directory doesn't exist: " + cfgPath + ".");
105+
}
106+
return cfgPath;
107+
} catch (IOException e) {
108+
throw new IOException("Error: Failed to find the path to the game's cfg directory.", e);
100109
}
101-
return cfgPath;
102110
}
103111

104112
boolean canChangeLog() {

src/com/joelchristophel/sourceradio/Playlist.java

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
package com.joelchristophel.sourceradio;
22

3-
import java.io.BufferedReader;
43
import java.io.Closeable;
54
import java.io.File;
65
import java.io.FileNotFoundException;
7-
import java.io.InputStreamReader;
8-
import java.net.HttpURLConnection;
9-
import java.net.URL;
6+
import java.io.IOException;
107
import java.net.UnknownHostException;
118
import java.sql.Types;
129
import java.text.Normalizer;
@@ -64,7 +61,7 @@ public static void main(String[] args) {
6461
System.out.println();
6562
String version = Playlist.class.getPackage().getImplementationVersion();
6663
version = version == null ? "" : version;
67-
System.out.println("**** SourceRadio" + (version.isEmpty() ? "" : " v" + version) + " ****");
64+
System.out.println("**** SourceRadio" + (version.isEmpty() ? "" : " v" + version) + " ****");
6865
System.out.println();
6966
List<String> argsList = args == null ? new ArrayList<String>() : new ArrayList<String>(Arrays.asList(args));
7067
if (argsList.contains("-d")) { // Restore default properties
@@ -121,7 +118,7 @@ public static void main(String[] args) {
121118
Song.downloadYoutubedl("libraries");
122119
System.out.println("Listening for commands...");
123120
playlist.start();
124-
} catch (FileNotFoundException e) { // If Steam or game directories don't exist
121+
} catch (IOException e) {
125122
System.out.println(e.getMessage());
126123
}
127124
}
@@ -131,9 +128,9 @@ public static void main(String[] args) {
131128
* This method is to be used to obtain a {@link Playlist} instance.
132129
*
133130
* @return a <code>Playlist</code> instance
134-
* @throws FileNotFoundException
131+
* @throws IOException
135132
*/
136-
public synchronized static Playlist getInstance(Game game) throws FileNotFoundException {
133+
public synchronized static Playlist getInstance(Game game) throws IOException {
137134
Playlist playlist = null;
138135
if (instance == null) {
139136
playlist = (instance = new Playlist(game));
@@ -148,11 +145,11 @@ public synchronized static Playlist getInstance(Game game) throws FileNotFoundEx
148145
/**
149146
* Constructs a {@link Playlist}.
150147
*
151-
* @throws FileNotFoundException
148+
* @throws IOException
152149
*
153150
* @see #getInstance
154151
*/
155-
private Playlist(Game game) throws FileNotFoundException {
152+
private Playlist(Game game) throws IOException {
156153
super();
157154
this.game = game;
158155
initialize();
@@ -161,9 +158,9 @@ private Playlist(Game game) throws FileNotFoundException {
161158
/**
162159
* Starts the database, initializes instance variables, and writes key binds.
163160
*
164-
* @throws FileNotFoundException
161+
* @throws IOException
165162
*/
166-
private void initialize() throws FileNotFoundException {
163+
private void initialize() throws IOException {
167164
Game.setCurrentGame(game);
168165
logReader = LogReader.getInstance();
169166
database = DatabaseManager.getInstance();
@@ -619,11 +616,7 @@ public void close() {
619616
*/
620617
private void setCurrentSong(Song song) {
621618
currentSong = song;
622-
try {
623-
scriptWriter.updateCurrentSongScript(song);
624-
} catch (FileNotFoundException e) {
625-
System.err.println(e.getMessage());
626-
}
619+
scriptWriter.updateCurrentSongScript(song);
627620
}
628621

629622
/**
@@ -787,7 +780,6 @@ private boolean removeAdmin(final Player adminToRemove, Player requester, boolea
787780
success = admins.remove(adminToRemove);
788781
if (toBeWritten) {
789782
Runnable removeAdmin = new Runnable() {
790-
791783
@Override
792784
public void run() {
793785
properties.removeAdmin(adminToRemove);

0 commit comments

Comments
 (0)