Skip to content

fileserver: Improve 404 error detail and context#7777

Open
Jualhosting wants to merge 1 commit into
caddyserver:masterfrom
Jualhosting:fix-fileserver-errors
Open

fileserver: Improve 404 error detail and context#7777
Jualhosting wants to merge 1 commit into
caddyserver:masterfrom
Jualhosting:fix-fileserver-errors

Conversation

@Jualhosting
Copy link
Copy Markdown
Contributor

🚀 Improve error handling in file server middleware

Overview

This PR improves the error handling inside the fileserver middleware, specifically addressing the ambiguity of 404 Not Found errors.

Previously, when a file was not found, a directory lacked an index file, or a file was hidden, the notFound method would return a generic caddyhttp.Error(http.StatusNotFound, nil) without any context. This made debugging difficult for users trying to figure out exactly why a resource was returning a 404.

🛠️ What’s been fixed?

  • Updated notFound signature: The internal notFound function now accepts an err error argument.
  • Contextual error propagation: All calls to notFound in staticfiles.go and browse.go now pass specific errors to the response instead of nil.
  • Clearer Error Messages:
    • fmt.Errorf("directory without index file or browse enabled") when an index is missing.
    • fmt.Errorf("file is hidden") when a file matches the Hide paths configuration.
    • Passes through the underlying fs.ErrNotExist for missing files.

💡 Impact

These changes ensure that logs and error responses now contain actionable context when a 404 is triggered, significantly improving the debuggability of the file server without breaking the PassThru logic.


🤖 AI Disclosure: The logic updates, context propagation, and documentation in this Pull Request were developed with the assistance of AI, carefully reviewed and tested to ensure Caddy's high standards are met.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant