How can I combine Go middleware pattern with error returning request handlers?

I like this pattern of HandlerFuncs returning errors too, it's much neater and you just write your error handler once. Just think of your middleware separately from the handlers it contains, you don't need the middleware to pass errors. The middleware is like a chain which executes each one in turn, and then the very last middleware is one which is aware of your handler signature, and deals with the error appropriately.

So in it's simplest form, keep the middleware you have exactly the same, but at the end insert one which is of this form (and doesn't execute another middleware but a special HandlerFunc):

// Use this special type for your handler funcs
type MyHandlerFunc func(w http.ResponseWriter, r *http.Request) error


// Pattern for endpoint on middleware chain, not takes a diff signature.
func errorHandler(h MyHandlerFunc) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
       // Execute the final handler, and deal with errors
        err := h(w, r)
        if err != nil {
            // Deal with error here, show user error template, log etc
        }
    })
}

...

Then wrap your function like this:

moreMiddleware(myMiddleWare(errorHandler(myhandleFuncReturningError)))

That means this special error middleware can only ever wrap your special function signature, and come at the end of the chain, but that's fine. Also I'd consider wrapping this behaviour in your own mux to make it a little simpler and avoid passing error handlers around, and let you build a chain of middleware more easily without having ugly wrapping in your route setup.

I think if you're using a router library, it needs explicit support for this pattern to work probably. You can see an example of this in action in a modified form in this router, which uses exactly the signatures you're after, but handles building a middleware chain and executing it without manual wrapping:

https://github.com/fragmenta/mux/blob/master/mux.go


The most flexible solution would be like this:

First define a type that matches your handler signature and implement ServeHTTP to satisfy the http.Handler interface. By doing so, ServeHTTP will be able to call the handler function and process the error if it fails. Something like:

type httpHandlerWithError func(http.ResponseWriter, *http.Request) error

func (fn httpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    if err := fn(w, r); err != nil {
         http.Error(w, err.Message, err.StatusCode)
    }
}

Now create the middleware as usual. The middleware should create a function which returns an error if it fails or calls the next in the chain on success. Then convert the function to the defined type something like:

func AuthMiddleware(next http.Handler) http.Handler {

    // create handler which returns error
    fn := func(w http.ResponseWriter, r *http.Request) error {

        //a custom error value
        unauthorizedError := &httpError{Code: http.StatusUnauthorized, Message: http.StatusText(http.StatusUnauthorized)}

        auth := r.Header.Get("authorization")
        creds := credentialsFromHeader(auth)

        if creds != nil {
            return unauthorizedError
        }

        user, err := db.ReadUser(creds.username)
        if err != nil {
            return &httpError{Code: http.StatusInternalServerError, Message: http.StatusText(http.StatusInternalServerError)}
        }

        err = checkPassword(creds.password+user.Salt, user.Hash)
        if err != nil {
            return unauthorizedError
        }

        ctx := r.Context()
        userCtx := UserToCtx(ctx, user)

        // we got here so there was no error
        next.ServeHTTP(w, r.WithContext(userCtx))
        return nil
    }

    // convert function
    return httpHandlerWithError(fn)
}

Now you can use the middleware as you would use any regular middleware.