Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3c91b24896 | |||
| a89f1830a4 | |||
| 0f99bba64f | |||
|
|
cca6260434 | ||
| dd792ba674 | |||
| 4ce7122065 |
2
go.mod
2
go.mod
@@ -2,4 +2,4 @@ module go.unistack.org/micro-wrapper-recovery/v3
|
|||||||
|
|
||||||
go 1.16
|
go 1.16
|
||||||
|
|
||||||
require go.unistack.org/micro/v3 v3.9.13
|
require go.unistack.org/micro/v3 v3.9.14
|
||||||
|
|||||||
4
go.sum
4
go.sum
@@ -74,8 +74,8 @@ github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQ
|
|||||||
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
|
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
|
||||||
go.unistack.org/micro-proto/v3 v3.3.1 h1:nQ0MtWvP2G3QrpOgawVOPhpZZYkq6umTGDqs8FxJYIo=
|
go.unistack.org/micro-proto/v3 v3.3.1 h1:nQ0MtWvP2G3QrpOgawVOPhpZZYkq6umTGDqs8FxJYIo=
|
||||||
go.unistack.org/micro-proto/v3 v3.3.1/go.mod h1:cwRyv8uInM2I7EbU7O8Fx2Ls3N90Uw9UCCcq4olOdfE=
|
go.unistack.org/micro-proto/v3 v3.3.1/go.mod h1:cwRyv8uInM2I7EbU7O8Fx2Ls3N90Uw9UCCcq4olOdfE=
|
||||||
go.unistack.org/micro/v3 v3.9.13 h1:x1/H4rDgvz2JWXx6UvFiWDyRLxg2Kpp6V1kjzpLlyvo=
|
go.unistack.org/micro/v3 v3.9.14 h1:KTH3mrAMALFLAzH0fLJpaVmKjEa0VlHMRBJBUCKD91g=
|
||||||
go.unistack.org/micro/v3 v3.9.13/go.mod h1:gI4RkJKHLPW7KV6h4+ZBOZD997MRvFRXMPQIHpozikI=
|
go.unistack.org/micro/v3 v3.9.14/go.mod h1:gI4RkJKHLPW7KV6h4+ZBOZD997MRvFRXMPQIHpozikI=
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||||
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||||
|
|||||||
72
wrapper.go
72
wrapper.go
@@ -4,22 +4,60 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"go.unistack.org/micro/v3/errors"
|
||||||
"go.unistack.org/micro/v3/server"
|
"go.unistack.org/micro/v3/server"
|
||||||
)
|
)
|
||||||
|
|
||||||
type wrapper struct {
|
var (
|
||||||
serverHandlerFunc func(context.Context, server.Request, interface{}, error) error
|
_ server.HandlerWrapper = NewServerHandlerWrapper()
|
||||||
serverSubscriberFunc func(context.Context, server.Message, error) error
|
_ server.SubscriberWrapper = NewServerSubscriberWrapper()
|
||||||
/*
|
)
|
||||||
clientCallFunc func(context.Context, string, client.Request, interface{}, client.CallOptions, error) error
|
|
||||||
clientClient func(client.Client, error) error
|
func NewOptions(opts ...Option) Options {
|
||||||
*/
|
options := Options{
|
||||||
|
ServerHandlerFn: DefaultServerHandlerFn,
|
||||||
|
ServerSubscriberFn: DefaultServerSubscriberFn,
|
||||||
|
}
|
||||||
|
for _, o := range opts {
|
||||||
|
o(&options)
|
||||||
|
}
|
||||||
|
return options
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHandlerWrapper(fn func(context.Context, server.Request, interface{}, error) error) server.HandlerWrapper {
|
type Options struct {
|
||||||
handler := &wrapper{
|
ServerHandlerFn func(context.Context, server.Request, interface{}, error) error
|
||||||
serverHandlerFunc: fn,
|
ServerSubscriberFn func(context.Context, server.Message, error) error
|
||||||
|
}
|
||||||
|
|
||||||
|
type Option func(*Options)
|
||||||
|
|
||||||
|
func ServerHandlerFn(fn func(context.Context, server.Request, interface{}, error) error) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.ServerHandlerFn = fn
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ServerSubscriberFn(fn func(context.Context, server.Message, error) error) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.ServerSubscriberFn = fn
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
DefaultServerHandlerFn = func(ctx context.Context, req server.Request, rsp interface{}, err error) error {
|
||||||
|
return errors.BadRequest("", "%v", err)
|
||||||
|
}
|
||||||
|
DefaultServerSubscriberFn = func(ctx context.Context, req server.Message, err error) error {
|
||||||
|
return errors.BadRequest("", "%v", err)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
type wrapper struct {
|
||||||
|
opts Options
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewServerHandlerWrapper(opts ...Option) server.HandlerWrapper {
|
||||||
|
handler := &wrapper{opts: NewOptions(opts...)}
|
||||||
return handler.HandlerFunc
|
return handler.HandlerFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,9 +69,9 @@ func (w *wrapper) HandlerFunc(fn server.HandlerFunc) server.HandlerFunc {
|
|||||||
case nil:
|
case nil:
|
||||||
return
|
return
|
||||||
case error:
|
case error:
|
||||||
err = w.serverHandlerFunc(ctx, req, rsp, verr)
|
err = w.opts.ServerHandlerFn(ctx, req, rsp, verr)
|
||||||
default:
|
default:
|
||||||
err = w.serverHandlerFunc(ctx, req, rsp, fmt.Errorf("%v", r))
|
err = w.opts.ServerHandlerFn(ctx, req, rsp, fmt.Errorf("%v", r))
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
err = fn(ctx, req, rsp)
|
err = fn(ctx, req, rsp)
|
||||||
@@ -41,10 +79,8 @@ func (w *wrapper) HandlerFunc(fn server.HandlerFunc) server.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSubscriberWrapper(fn func(context.Context, server.Message, error) error) server.SubscriberWrapper {
|
func NewServerSubscriberWrapper(opts ...Option) server.SubscriberWrapper {
|
||||||
handler := &wrapper{
|
handler := &wrapper{opts: NewOptions(opts...)}
|
||||||
serverSubscriberFunc: fn,
|
|
||||||
}
|
|
||||||
return handler.SubscriberFunc
|
return handler.SubscriberFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,9 +92,9 @@ func (w *wrapper) SubscriberFunc(fn server.SubscriberFunc) server.SubscriberFunc
|
|||||||
case nil:
|
case nil:
|
||||||
return
|
return
|
||||||
case error:
|
case error:
|
||||||
err = w.serverSubscriberFunc(ctx, msg, verr)
|
err = w.opts.ServerSubscriberFn(ctx, msg, verr)
|
||||||
default:
|
default:
|
||||||
err = w.serverSubscriberFunc(ctx, msg, fmt.Errorf("%v", r))
|
err = w.opts.ServerSubscriberFn(ctx, msg, fmt.Errorf("%v", r))
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
err = fn(ctx, msg)
|
err = fn(ctx, msg)
|
||||||
|
|||||||
Reference in New Issue
Block a user