github.com/avenga/couper@v1.12.2/handler/middleware/error.go (about)

     1  package middleware
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  
     7  	"github.com/avenga/couper/config/request"
     8  )
     9  
    10  type condition func(req *http.Request) error
    11  
    12  func NewErrorHandler(condition condition, eh http.Handler) Next {
    13  	return func(handler http.Handler) *NextHandler {
    14  		return NewHandler(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
    15  			if err := condition(req); err != nil {
    16  				*req = *req.WithContext(context.WithValue(req.Context(), request.Error, err))
    17  				eh.ServeHTTP(rw, req)
    18  				return
    19  			}
    20  			handler.ServeHTTP(rw, req)
    21  		}), handler)
    22  	}
    23  }