diff options
author | Gabriel A. Giovanini <mail@gabrielgio.me> | 2024-06-08 00:01:44 +0200 |
---|---|---|
committer | Gabriel A. Giovanini <mail@gabrielgio.me> | 2024-06-08 00:01:44 +0200 |
commit | 6079b1d963f34ada5c4b25363f2319901e283936 (patch) | |
tree | f7f93616eb3dacfcebee486fe7542ec3adfb3950 /pkg/handler/git | |
parent | e9098e00fb6339b759df5b0df2e086cef8a7ce83 (diff) | |
download | cerrado-6079b1d963f34ada5c4b25363f2319901e283936.tar.gz cerrado-6079b1d963f34ada5c4b25363f2319901e283936.tar.bz2 cerrado-6079b1d963f34ada5c4b25363f2319901e283936.zip |
feat: Add error handling
Diffstat (limited to 'pkg/handler/git')
-rw-r--r-- | pkg/handler/git/handler.go | 61 |
1 files changed, 27 insertions, 34 deletions
diff --git a/pkg/handler/git/handler.go b/pkg/handler/git/handler.go index 7bdf372..d952fef 100644 --- a/pkg/handler/git/handler.go +++ b/pkg/handler/git/handler.go @@ -3,7 +3,6 @@ package git import ( "bytes" "io" - "log/slog" "net/http" "os" "path/filepath" @@ -50,23 +49,20 @@ func NewGitHandler(gitService gitService, confRepo configurationRepository) *Git } } -func (g *GitHandler) List(w http.ResponseWriter, _ *http.Request) { +func (g *GitHandler) List(w http.ResponseWriter, _ *http.Request) error { repos, err := g.gitService.ListRepositories() if err != nil { - slog.Error("Error listing repo", "error", err) - return + return err } f, err := os.Open(g.readmePath) if err != nil { - slog.Error("Error loading readme file", "error", err) - return + return err } bs, err := io.ReadAll(f) if err != nil { - slog.Error("Error reading readme file bytes", "error", err) - return + return err } extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock @@ -84,15 +80,15 @@ func (g *GitHandler) List(w http.ResponseWriter, _ *http.Request) { About: bs, } templates.WritePageTemplate(w, gitList) + return nil } -func (g *GitHandler) Summary(w http.ResponseWriter, r *http.Request) { +func (g *GitHandler) Summary(w http.ResponseWriter, r *http.Request) error { ext.SetHTML(w) name := r.PathValue("name") ref, err := g.gitService.GetHead(name) if err != nil { - slog.Error("Error loading head", "error", err) - return + return err } gitList := &templates.GitItemPage{ @@ -101,15 +97,15 @@ func (g *GitHandler) Summary(w http.ResponseWriter, r *http.Request) { GitItemBase: &templates.GitItemSummaryPage{}, } templates.WritePageTemplate(w, gitList) + return nil } -func (g *GitHandler) About(w http.ResponseWriter, r *http.Request) { +func (g *GitHandler) About(w http.ResponseWriter, r *http.Request) error { ext.SetHTML(w) name := r.PathValue("name") ref, err := g.gitService.GetHead(name) if err != nil { - slog.Error("Error loading head", "error", err) - return + return err } gitList := &templates.GitItemPage{ Name: name, @@ -117,28 +113,26 @@ func (g *GitHandler) About(w http.ResponseWriter, r *http.Request) { GitItemBase: &templates.GitItemAboutPage{}, } templates.WritePageTemplate(w, gitList) + return nil } -func (g *GitHandler) Refs(w http.ResponseWriter, r *http.Request) { +func (g *GitHandler) Refs(w http.ResponseWriter, r *http.Request) error { ext.SetHTML(w) name := r.PathValue("name") tags, err := g.gitService.ListTags(name) if err != nil { - slog.Error("Error loading tags", "error", err) - return + return err } branches, err := g.gitService.ListBranches(name) if err != nil { - slog.Error("Error loading branches", "error", err) - return + return err } ref, err := g.gitService.GetHead(name) if err != nil { - slog.Error("Error loading head", "error", err) - return + return err } gitList := &templates.GitItemPage{ @@ -150,9 +144,10 @@ func (g *GitHandler) Refs(w http.ResponseWriter, r *http.Request) { }, } templates.WritePageTemplate(w, gitList) + return nil } -func (g *GitHandler) Tree(w http.ResponseWriter, r *http.Request) { +func (g *GitHandler) Tree(w http.ResponseWriter, r *http.Request) error { ext.SetHTML(w) name := r.PathValue("name") ref := r.PathValue("ref") @@ -160,8 +155,7 @@ func (g *GitHandler) Tree(w http.ResponseWriter, r *http.Request) { tree, err := g.gitService.GetTree(name, ref, rest) if err != nil { - slog.Error("Error loading tree", "error", err) - return + return err } gitList := &templates.GitItemPage{ @@ -175,9 +169,10 @@ func (g *GitHandler) Tree(w http.ResponseWriter, r *http.Request) { }, } templates.WritePageTemplate(w, gitList) + return nil } -func (g *GitHandler) Blob(w http.ResponseWriter, r *http.Request) { +func (g *GitHandler) Blob(w http.ResponseWriter, r *http.Request) error { ext.SetHTML(w) name := r.PathValue("name") ref := r.PathValue("ref") @@ -185,8 +180,7 @@ func (g *GitHandler) Blob(w http.ResponseWriter, r *http.Request) { file, err := g.gitService.GetFileContent(name, ref, rest) if err != nil { - slog.Error("Error loading blob", "error", err) - return + return err } filename := filepath.Base(rest) @@ -197,15 +191,13 @@ func (g *GitHandler) Blob(w http.ResponseWriter, r *http.Request) { ) iterator, err := lexer.Tokenise(nil, file) 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 } gitList := &templates.GitItemPage{ @@ -217,17 +209,17 @@ func (g *GitHandler) Blob(w http.ResponseWriter, r *http.Request) { }, } templates.WritePageTemplate(w, gitList) + return nil } -func (g *GitHandler) Log(w http.ResponseWriter, r *http.Request) { +func (g *GitHandler) Log(w http.ResponseWriter, r *http.Request) error { ext.SetHTML(w) name := r.PathValue("name") ref := r.PathValue("ref") commits, err := g.gitService.ListCommits(name, ref) if err != nil { - slog.Error("Error loading commits", "error", err) - return + return err } gitList := &templates.GitItemPage{ @@ -238,6 +230,7 @@ func (g *GitHandler) Log(w http.ResponseWriter, r *http.Request) { }, } templates.WritePageTemplate(w, gitList) + return nil } func GetLexers(filename string) chroma.Lexer { |