@@ -5,45 +5,41 @@ import Map from "map"
55import Option from "option"
66import File from "sys/file"
77import String from "string"
8+ import Result from "result"
89import Mediatype from "./lib/mediatype"
910import Stringutil from "./lib/stringutil"
1011
12+ // Utility wrapper around a Result.expect that ignores the return value
13+ // so we don't need to worry about things returning non-Void types
14+ let validateResult = (msg, res) => {
15+ ignore(Result.expect(msg, res))
16+ }
17+
1118let internalError = () => {
12- // Is stdout correct here?
13- let result = File.fdWrite(File.stdout, "Status: 500\n\nInternal Server Error")
14- match (result) {
15- Err(err) => {
16- // TODO: What do you do on a write error?
17- void
18- },
19- Ok(_nBytes) => void,
20- }
19+ validateResult(
20+ "Unexpected error when writing Internal Server Error response",
21+ File.fdWrite(File.stdout, "Status: 500\n\nInternal Server Error"),
22+ )
2123}
2224
2325let notFound = () => {
24- let result = File.fdWrite(File.stdout, "Status: 404\n\nNot Found")
25- match (result) {
26- Err(err) => {
27- // TODO: What do you do on a write error?
28- void
29- },
30- Ok(_nBytes) => void,
31- }
26+ validateResult(
27+ "Unexpected error when writing Not Found response",
28+ File.fdWrite(File.stdout, "Status: 404\n\nNot Found"),
29+ )
3230}
3331
3432// Pipe output to STDOUT
3533let rec pipe = (in, out) => {
3634 let res = File.fdRead(in, 1024)
3735 match (res) {
38- Err(err) => {
39- // TODO: What to do on a read error?
40- void
41- },
36+ Err(err) => Err(err),
4237 Ok((d, len)) => {
43- // TODO: What should happen if this Result is an error
44- File.fdWrite(out, d)
38+ let res = File.fdWrite(out, d)
4539 if (len > 0) {
4640 pipe(in, out)
41+ } else {
42+ res
4743 }
4844 },
4945 }
@@ -52,26 +48,30 @@ let rec pipe = (in, out) => {
5248let serve = abs_path => {
5349 // Trim the leading /
5450 let path = String.slice(1, String.length(abs_path), abs_path)
55- // TODO: What should happen if any of these Results are an error
56- File.fdWrite(File.stderr, "Fileserver: Loading file ")
57- File.fdWrite(File.stderr, path)
58- File.fdWrite(File.stderr, "\n")
51+ // Explicitly ignoring any Ok or Err that happens on this log
52+ // The `ignore` can be removed if you don't want to be explicit about this behavior
53+ ignore(File.fdWrite(File.stderr, "Fileserver: Loading file " ++ path ++ "\n"))
5954
6055 // Open file
6156 let result = File.pathOpen(File.pwdfd, [], path, [], [File.FdRead], [], [])
6257
6358 match (result) {
6459 Err(_err) => notFound(),
6560 Ok(input) => {
66- // TODO: What should happen if any of these Results are an error
67- File.fdWrite(File.stdout, "Content-Type: ")
68- File.fdWrite(File.stdout, Mediatype.guess(path))
69- File.fdWrite(File.stdout, "\n\n")
61+ validateResult(
62+ "Unexpected error when writing Content-Type",
63+ File.fdWrite(
64+ File.stdout,
65+ "Content-Type: " ++ Mediatype.guess(path) ++ "\n\n",
66+ ),
67+ )
7068
71- pipe(input, File.stdout)
72- // TODO: What should happen if this Result is an error
73- File.fdClose(input)
74- void
69+ validateResult(
70+ "Unexpected error when streaming file body",
71+ pipe(input, File.stdout),
72+ )
73+ // This validation may be able to be removed if it doesn't matter if the fdClose fails
74+ validateResult("Unexpected error when closing file", File.fdClose(input))
7575 },
7676 }
7777}
0 commit comments