Skip to content

Conversation

@umputun
Copy link
Member

@umputun umputun commented Jan 19, 2026

Adds ErrorHandlerFunc type to customize error responses on auth failures. Related to #264

What changed:

  • new ErrorHandlerFunc type in middleware package
  • ErrorHandler field in Authenticator struct and Opts
  • all middleware (Auth, AdminOnly, RBAC) now use errResponse which calls custom handler if set
  • default behavior unchanged (returns "Unauthorized" 401 or "Access denied" 403)

Usage example:

options := auth.Opts{
    ErrorHandler: middleware.ErrorHandlerFunc(func(w http.ResponseWriter, r *http.Request, code int, err error) {
        if strings.HasPrefix(r.URL.Path, "/api/") {
            w.Header().Set("Content-Type", "application/json")
            w.WriteHeader(code)
            json.NewEncoder(w).Encode(map[string]string{"error": "authentication required"})
            return
        }
        http.Redirect(w, r, "/login", http.StatusFound)
    }),
}

@fbuedding pls take a look and let me know if this covers the functionality you had in mind

Add ErrorHandler option to customize error responses when authentication
fails. The handler receives the HTTP status code (401/403) and the error,
allowing custom HTML pages, redirects, or JSON responses.

Related to #264
- remove duplicate default error handler logic from NewService
  (errResponse already handles the default case)
- add ErrorHandler documentation to README.md
- add progress-*.txt to .gitignore
Copilot AI review requested due to automatic review settings January 19, 2026 21:19
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds customizable error handling for authentication failures in the middleware package, addressing issue #264. The implementation allows users to customize how authentication errors are handled (e.g., returning JSON responses for API routes or redirecting to login pages for web routes) while maintaining backward compatibility with the default behavior.

Changes:

  • Added ErrorHandlerFunc type to allow custom error response handling on authentication failures
  • Extended Authenticator struct and Opts to include optional ErrorHandler field
  • Refactored error responses in Auth, AdminOnly, and RBAC middlewares to use a centralized errResponse method

Reviewed changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
v2/middleware/auth.go Added ErrorHandlerFunc type, ErrorHandler field to Authenticator, and errResponse helper method; updated Auth, AdminOnly, and RBAC to use custom error handler
v2/auth.go Added ErrorHandler field to Opts and wired it to Authenticator in NewService
v2/middleware/auth_test.go Added comprehensive test coverage for custom error handlers in Auth, AdminOnly, and RBAC middlewares
README.md Added documentation and usage example for the new ErrorHandlerFunc feature
.gitignore Added entries for ralph progress logs (unrelated to this PR)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@coveralls
Copy link

coveralls commented Jan 19, 2026

Pull Request Test Coverage Report for Build 21151713439

Details

  • 14 of 16 (87.5%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.05%) to 83.628%

Changes Missing Coverage Covered Lines Changed/Added Lines %
v2/middleware/auth.go 13 15 86.67%
Totals Coverage Status
Change from base Build 20442464979: 0.05%
Covered Lines: 2697
Relevant Lines: 3225

💛 - Coveralls

@fbuedding
Copy link

This is exactly what I had in mind :-) Thank you!

@umputun
Copy link
Member Author

umputun commented Jan 19, 2026

oops, just noticed in your original ticket: "If you're open to it, I could try to implement these
changes myself and create a pull request."

sorry, missed it completely and jumped on this one ;)

@umputun umputun merged commit 5ff800f into master Jan 19, 2026
13 checks passed
@umputun umputun deleted the feature/custom-error-handler branch January 19, 2026 21:32
@fbuedding
Copy link

oops, just noticed in your original ticket: "If you're open to it, I could try to implement these changes myself and create a pull request."

sorry, missed it completely and jumped on this one ;)

No worries! I'm always eager to contribute :D. When I saw that function literal, I thought it would be great to make it customizable and didn't want to 'demand' a change that I could implement myself.

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.

4 participants