From 6079b1d963f34ada5c4b25363f2319901e283936 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Sat, 8 Jun 2024 00:01:44 +0200 Subject: feat: Add error handling --- pkg/handler/config/handler.go | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) (limited to 'pkg/handler/config') diff --git a/pkg/handler/config/handler.go b/pkg/handler/config/handler.go index 30f4283..c43b54d 100644 --- a/pkg/handler/config/handler.go +++ b/pkg/handler/config/handler.go @@ -3,7 +3,6 @@ package config import ( "bytes" "encoding/json" - "log/slog" "net/http" "github.com/alecthomas/chroma/v2/formatters/html" @@ -11,6 +10,7 @@ import ( "github.com/alecthomas/chroma/v2/styles" "git.gabrielgio.me/cerrado/pkg/config" + "git.gabrielgio.me/cerrado/pkg/ext" "git.gabrielgio.me/cerrado/templates" ) @@ -21,8 +21,8 @@ type ( } ) -func ConfigFile(configRepo configurationRepository) func(http.ResponseWriter, *http.Request) { - return func(w http.ResponseWriter, _ *http.Request) { +func ConfigFile(configRepo configurationRepository) ext.ErrorRequestHandler { + return func(w http.ResponseWriter, _ *http.Request) error { config := struct { RootReadme string @@ -34,8 +34,7 @@ func ConfigFile(configRepo configurationRepository) func(http.ResponseWriter, *h b, err := json.MarshalIndent(config, "", " ") if err != nil { - slog.Error("Error parsing json", "error", err) - return + return err } lexer := lexers.Get("json") @@ -45,15 +44,13 @@ func ConfigFile(configRepo configurationRepository) func(http.ResponseWriter, *h ) iterator, err := lexer.Tokenise(nil, string(b)) if err != nil { - slog.Error("Error tokenise", "error", err) - return + return err } var code bytes.Buffer err = formatter.Format(&code, style, iterator) if err != nil { - slog.Error("Error format", "error", err) - return + return err } hello := &templates.ConfigPage{ @@ -61,5 +58,6 @@ func ConfigFile(configRepo configurationRepository) func(http.ResponseWriter, *h } templates.WritePageTemplate(w, hello) + return nil } } -- cgit v1.2.3