all repos — elgit @ b2a43e5d4fca579e569449a9f84283bca7c62a4d

fork of legit: web frontend for git, written in go

improve error logging

Alin
commit

b2a43e5d4fca579e569449a9f84283bca7c62a4d

parent

15f548a674ff07cc9822ce334c4e38d1081bd957

2 files changed, 13 insertions(+), 7 deletions(-)

changed files
M routes/handler.goroutes/handler.go
@@ -21,6 +21,7 @@
var ( ErrMethodNotAllowed = fmt.Errorf("that's not possible") ErrNotFound = fmt.Errorf("nothing like that here") + ErrRepoDenied = fmt.Errorf("nothing like that here") ) func Handlers(c *config.Config, staticFiles fs.FS) *atreugo.Atreugo {
@@ -122,9 +123,7 @@ name, _ := rc.UserValue("name").(string)
repoName := path.Join(category, name) if d.isNotAllowed(repoName) { - log.Printf("access not allowed: %s", repoName) - - return ErrNotFound + return fmt.Errorf("repo %s: %w", repoName, ErrRepoDenied) } rc.SetUserValue("repoName", repoName)
M routes/routes.goroutes/routes.go
@@ -2,6 +2,7 @@ package routes
import ( "compress/gzip" + "errors" "fmt" "log" "path/filepath"
@@ -311,14 +312,20 @@ return templates.RefsPage(pageData, branches, tags).Render(rc)
} func (d *deps) NotFound(rc *atreugo.RequestCtx) error { - log.Printf("Not found: %s", rc.Request.RequestURI()) - - return ErrNotFound + return fmt.Errorf("%s: %w", string(rc.Request.RequestURI()), ErrNotFound) } func (d *deps) Error(rc *atreugo.RequestCtx, cause error, statusCode int) { + message := cause.Error() + log.Printf("Error: %v", cause) + if errors.Is(cause, ErrNotFound) || errors.Is(cause, ErrRepoDenied) { + statusCode = fasthttp.StatusNotFound + cause = errors.Unwrap(cause) + message = cause.Error() + } + var err error if rc.Request.Header.HasAcceptEncoding("text/html") { rc.SetContentType("text/html; charset=utf-8")
@@ -326,7 +333,7 @@
err = templates.ErrorPage(templates.PageData{ Error: &templates.Error{ Code: statusCode, - Message: cause.Error(), + Message: message, }, }).Render(rc) } else {