Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BusinessErrorMiddleware ¶ added in v0.2.0
BusinessErrorMiddleware checks if a returned error is a business error and wraps it in a failer response if it is. Deprecated: Use FailerMiddleware instead.
func Chain ¶ added in v0.2.0
Chain composes a single middleware from a list. Compared to endpoint.Chain, this function accepts a variadic list.
Example ¶
annotate := func(pos string) endpoint.Middleware { return func(e endpoint.Endpoint) endpoint.Endpoint { return func(ctx context.Context, req interface{}) (response interface{}, err error) { fmt.Println(pos + " pre") response, err = e(ctx, req) fmt.Println(pos + " post") return } } } e := endpoint.Chain( annotate("first"), annotate("second"), annotate("third"), )( func(context.Context, interface{}) (interface{}, error) { fmt.Println("endpoint") return nil, nil }, ) if _, err := e(ctx, req); err != nil { panic(err) }
Output: first pre second pre third pre endpoint third post second post first post
func FailerMiddleware ¶ added in v0.4.0
func FailerMiddleware(matcher ErrorMatcher) endpoint.Middleware
FailerMiddleware checks if a returned error matches a predicate and wraps it in a failer response if it does.
Types ¶
type ErrorMatcher ¶ added in v0.4.0
type ErrorMatcher interface { // MatchError evaluates the predicate for an error. MatchError(err error) bool }
ErrorMatcher is a predicate for errors. It can be used in middleware to decide whether to take action or not.
type ErrorMatcherFunc ¶ added in v0.4.0
ErrorMatcherFunc turns a plain function into an ErrorMatcher if it's definition matches the interface.
func (ErrorMatcherFunc) MatchError ¶ added in v0.4.0
func (fn ErrorMatcherFunc) MatchError(err error) bool
MatchError calls the underlying function to evaluate the predicate.
type Factory ¶
type Factory interface { // NewEndpoint returns an endpoint wrapped with preconfigured middleware. // It also accepts an operation name for per operation middleware (eg. logging or tracing middleware). NewEndpoint(name string, e endpoint.Endpoint) endpoint.Endpoint }
Factory returns an endpoint wrapped with preconfigured middleware.
func NewFactory ¶
func NewFactory(middlewareFactories ...MiddlewareFactory) Factory
NewFactory returns a new Factory.
type MiddlewareFactory ¶
type MiddlewareFactory func(name string) endpoint.Middleware
MiddlewareFactory creates a middleware per operation.
func Middleware ¶
func Middleware(middleware endpoint.Middleware) MiddlewareFactory
Middleware wraps singleton middleware and wraps them in a MiddlewareFactory.