Skip to content

Commit d9da73c

Browse files
committed
fix: more explicit error handling
1 parent 6e26874 commit d9da73c

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

main.go

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ type LibationsPageData struct {
5252

5353
// parseTemplates is used to parse templates from the embedded FS, and ensure that the
5454
// 'StringJoin' function is available to the templates.
55-
func parseTemplates() *template.Template {
55+
func parseTemplates() (*template.Template, error) {
5656
// Create an fs.FS from the embedded filesystem
5757
files, err := fs.Sub(templateFS, "templates")
5858
if err != nil {
@@ -62,8 +62,13 @@ func parseTemplates() *template.Template {
6262

6363
funcMap := template.FuncMap{"StringsJoin": strings.Join}
6464
tmpl := template.New("").Funcs(funcMap)
65-
tmpl, _ = tmpl.ParseFS(files, "*.html", "icons/*.svg")
66-
return tmpl
65+
66+
tmpl, err = tmpl.ParseFS(files, "*.html", "icons/*.svg")
67+
if err != nil {
68+
return nil, err
69+
}
70+
71+
return tmpl, nil
6772
}
6873

6974
// parseRecipes attempts to read and parse recipes either from a path specified at the CLI,
@@ -103,7 +108,11 @@ func libationsMux(drinks []Drink, files fs.FS) *http.ServeMux {
103108
// Serve static files from our embedded filesystem using http.Fileserver
104109
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(files))))
105110

106-
templates := parseTemplates()
111+
templates, err := parseTemplates()
112+
if err != nil {
113+
slog.Error("failed to parse templates from filesystem", "error", err.Error())
114+
os.Exit(1)
115+
}
107116

108117
// Render the templates with the drinks/time data.
109118
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
@@ -196,32 +205,36 @@ func main() {
196205
// Create an fs.FS from the embedded filesystem
197206
files, err := fs.Sub(staticFS, "static")
198207
if err != nil {
199-
slog.Error(err.Error())
208+
slog.Error("failed to access static files in embedded filesystem", "error", err.Error())
200209
os.Exit(1)
201210
}
202211

203212
drinks, err := parseRecipes(*recipesFile)
204213
if err != nil {
205-
slog.Error(err.Error())
214+
slog.Error("failed to parse recipes", "error", err.Error())
206215
os.Exit(1)
207216
}
208217

209218
var listener *net.Listener
210219
if *local {
211220
listener, err = localListener(*addr)
221+
if err != nil {
222+
slog.Error("failed to create local listener", "error", err.Error())
223+
os.Exit(1)
224+
}
212225
} else {
213226
listener, err = tailscaleListener(*hostname, *tsnetLogs)
214-
}
215-
216-
if err != nil {
217-
slog.Error(err.Error())
218-
os.Exit(1)
227+
if err != nil {
228+
slog.Error("failed to create tailscale listener", "error", err.Error())
229+
os.Exit(1)
230+
}
219231
}
220232

221233
mux := libationsMux(drinks, files)
222234

223235
slog.Info(fmt.Sprintf("starting listener on %s", *addr))
224236
if err = http.Serve(*listener, mux); err != nil {
225-
slog.Error(err.Error())
237+
slog.Error("failed to start http server", "error", err.Error())
238+
os.Exit(1)
226239
}
227240
}

0 commit comments

Comments
 (0)