From d8c45ca9c630b805c7db1438a669b224a18dafb9 Mon Sep 17 00:00:00 2001 From: jalikajalika5 <105954036+jalikajalika5@users.noreply.github.com> Date: Thu, 28 May 2026 18:31:50 +0700 Subject: [PATCH] fileserver: Improve 404 error detail and context --- modules/caddyhttp/fileserver/browse.go | 2 +- modules/caddyhttp/fileserver/staticfiles.go | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/modules/caddyhttp/fileserver/browse.go b/modules/caddyhttp/fileserver/browse.go index 3b97f2ff3..5f66e128c 100644 --- a/modules/caddyhttp/fileserver/browse.go +++ b/modules/caddyhttp/fileserver/browse.go @@ -121,7 +121,7 @@ func (fsrv *FileServer) serveBrowse(fileSystem fs.FS, root, dirPath string, w ht case errors.Is(err, fs.ErrPermission): return caddyhttp.Error(http.StatusForbidden, err) case errors.Is(err, fs.ErrNotExist): - return fsrv.notFound(w, r, next) + return fsrv.notFound(w, r, next, err) case err != nil: return caddyhttp.Error(http.StatusInternalServerError, err) } diff --git a/modules/caddyhttp/fileserver/staticfiles.go b/modules/caddyhttp/fileserver/staticfiles.go index 3ca7452e1..015ab1a5e 100644 --- a/modules/caddyhttp/fileserver/staticfiles.go +++ b/modules/caddyhttp/fileserver/staticfiles.go @@ -309,7 +309,7 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c if err != nil { err = fsrv.mapDirOpenError(fileSystem, err, filename) if errors.Is(err, fs.ErrNotExist) { - return fsrv.notFound(w, r, next) + return fsrv.notFound(w, r, next, err) } else if errors.Is(err, fs.ErrInvalid) { return caddyhttp.Error(http.StatusBadRequest, err) } else if errors.Is(err, fs.ErrPermission) { @@ -371,7 +371,7 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c if fsrv.Browse != nil && !fileHidden(filename, filesToHide) { return fsrv.serveBrowse(fileSystem, root, filename, w, r, next) } - return fsrv.notFound(w, r, next) + return fsrv.notFound(w, r, next, fmt.Errorf("directory without index file or browse enabled")) } // one last check to ensure the file isn't hidden (we might @@ -383,7 +383,7 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c zap.Strings("files_to_hide", filesToHide), ) } - return fsrv.notFound(w, r, next) + return fsrv.notFound(w, r, next, fmt.Errorf("file is hidden")) } // if URL canonicalization is enabled, we need to enforce trailing @@ -501,7 +501,7 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c if err != nil { if herr, ok := err.(caddyhttp.HandlerError); ok && herr.StatusCode == http.StatusNotFound { - return fsrv.notFound(w, r, next) + return fsrv.notFound(w, r, next, herr.Err) } return err // error is already structured } @@ -730,11 +730,11 @@ func fileHidden(filename string, hide []string) bool { // notFound returns a 404 error or, if pass-thru is enabled, // it calls the next handler in the chain. -func (fsrv *FileServer) notFound(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error { +func (fsrv *FileServer) notFound(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler, err error) error { if fsrv.PassThru { return next.ServeHTTP(w, r) } - return caddyhttp.Error(http.StatusNotFound, nil) + return caddyhttp.Error(http.StatusNotFound, err) } // Indicates whether a file's modification time is useful for validator