Skip to content

Commit 8052c60

Browse files
committed
Improve error handling
1 parent 7826fec commit 8052c60

File tree

7 files changed

+26
-28
lines changed

7 files changed

+26
-28
lines changed

src/main/java/httpserver/Logger.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,21 @@ public class Logger {
99
private final FileOperator fileOperator;
1010
private final Path logPath;
1111

12-
public Logger(Path logPath, FileOperator fileOperator) {
12+
public Logger(Path logPath, FileOperator fileOperator) throws IOException {
1313
this.fileOperator = fileOperator;
1414
this.logPath = logPath;
1515
createFileIfDoesntExist(logPath);
1616
}
1717

18-
public void log(String logString) {
18+
public void log(String logString) throws IOException {
1919
byte[] logBytes = (logString + "\r\n").getBytes();
20-
try {
21-
fileOperator.appendToFile(logPath, logBytes);
22-
} catch (IOException e) {
23-
e.printStackTrace();
24-
}
20+
fileOperator.appendToFile(logPath, logBytes);
2521
}
2622

2723
private void createFileIfDoesntExist(Path path) {
2824
try {
2925
fileOperator.createFileAtPath(path);
30-
} catch (IOException e) {}
26+
} catch (IOException e) { }
3127
}
3228

3329
public byte[] readLog() throws IOException {

src/main/java/httpserver/SocketHandler.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,18 @@ public void run() {
4343

4444
responseWriter.write(response);
4545

46+
closeStream();
47+
}
48+
49+
private void closeStream() {
4650
try {
4751
inputStream.close();
4852
} catch (IOException e) {
49-
appConfig.getLogger().log(e.toString());
53+
try {
54+
appConfig.getLogger().log(e.toString());
55+
} catch (IOException e1) {
56+
e1.printStackTrace();
57+
}
5058
}
5159
}
5260
}

src/main/java/httpserver/file/PathExaminer.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,15 @@ public boolean isFile(Path path) {
3434
return exists(path) && !isDirectory(path);
3535
}
3636

37-
public byte[] fileContents(Path file) {
38-
try {
39-
return readAllBytes(file);
40-
} catch (IOException e) {
41-
return new byte[0];
42-
}
37+
public byte[] fileContents(Path file) throws IOException {
38+
return readAllBytes(file);
4339
}
4440

45-
public Path[] directoryContents(Path dir) {
41+
public Path[] directoryContents(Path dir) throws IOException {
4642
List<Path> result = new ArrayList<>();
47-
try (DirectoryStream<Path> stream = newDirectoryStream(dir)) {
48-
for (Path entry: stream) {
49-
result.add(entry);
50-
}
51-
} catch (IOException e) {
52-
System.out.println("Couldn't retrieve directory contents for directory at " + dir.toString());
43+
DirectoryStream<Path> stream = newDirectoryStream(dir);
44+
for (Path entry: stream) {
45+
result.add(entry);
5346
}
5447
return result.toArray(new Path[0]);
5548
}

src/main/java/httpserver/request/UrlDecoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public String decode(String urlString) {
77
try {
88
return java.net.URLDecoder.decode(urlString, "UTF-8");
99
} catch (UnsupportedEncodingException e) {
10-
return urlString;
10+
throw new RuntimeException(e);
1111
}
1212
}
1313
}

src/main/java/httpserver/responder/GetResponder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public Response respond(AppConfig appConfig, Request request) throws IOException
5656
return responseForFile(fullPath, request);
5757
}
5858

59-
private Response responseForDir(Path root, Path path) {
59+
private Response responseForDir(Path root, Path path) throws IOException {
6060
Path[] paths = pathExaminer.directoryContents(path);
6161
String result = htmlLinksForContents(root, paths);
6262
return new OkResponse(result.getBytes());
@@ -70,7 +70,7 @@ private String htmlLinksForContents(Path root, Path[] paths) {
7070
return stringBuilder.toString();
7171
}
7272

73-
private Response responseForFile(Path path, Request request) {
73+
private Response responseForFile(Path path, Request request) throws IOException {
7474
byte[] payload = pathExaminer.fileContents(path);
7575

7676
Response response;

src/main/java/httpserver/responder/PatchResponder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private boolean noIfMatchHeader(Request request) {
4949
return !request.hasHeader("If-Match");
5050
}
5151

52-
private boolean matchingHash(Path fullPath, Request request) {
52+
private boolean matchingHash(Path fullPath, Request request) throws IOException {
5353
byte[] fileContents = pathExaminer.fileContents(fullPath);
5454
String ifMatchHash = request.getHeaderValue("If-Match");
5555
return hasher.matches(fileContents, ifMatchHash);

src/test/java/httpserver/SocketHandlerTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class SocketHandlerTest {
3535

3636
public SocketHandlerTest() throws IOException {
3737
root = tempDir();
38-
logger = new Logger(root, new FileOperator());
38+
logger = new Logger(root.resolve("logs"), new FileOperator());
3939
appConfig = new AppConfig(root, logger);
4040
relativePath1 = root.relativize(tempFileOptions(root, "aaa", "temp"));
4141
relativePath2 = root.relativize(tempFileOptions(root, "bbb", "temp"));
@@ -117,7 +117,8 @@ public void writesContentTypeForGif() throws Exception {
117117
@Test
118118
public void writesRequestedDirContentsAsHtmlToOutputStreamForGET() throws Exception {
119119
String expectedBody = "<div><a href=\"/" + relativePath1 + "\">/" + relativePath1 + "</a></div>" +
120-
"<div><a href=\"/" + relativePath2 + "\">/" + relativePath2 + "</a></div>";
120+
"<div><a href=\"/" + relativePath2 + "\">/" + relativePath2 + "</a></div>" +
121+
"<div><a href=\"/logs\">/logs</a></div>";
121122

122123
byte[] request = ("GET / HTTP/1.1\r\nHost: 127.0.0.1:5000\r\n\r\n").getBytes();
123124

0 commit comments

Comments
 (0)