update for latest micro
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
08a832569a
commit
08389ffa3a
7
go.mod
7
go.mod
@ -2,4 +2,9 @@ module go.unistack.org/micro-wrapper-recovery/v3
|
||||
|
||||
go 1.16
|
||||
|
||||
require go.unistack.org/micro/v3 v3.10.42
|
||||
require (
|
||||
go.unistack.org/micro/v3 v3.10.66
|
||||
golang.org/x/sys v0.19.0 // indirect
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20240415180920-8c6c420018be // indirect
|
||||
google.golang.org/grpc v1.63.2 // indirect
|
||||
)
|
||||
|
100
wrapper.go
100
wrapper.go
@ -1,4 +1,4 @@
|
||||
package recovery // import "go.unistack.org/micro-wrapper-recovery/v3"
|
||||
package recovery
|
||||
|
||||
import (
|
||||
"context"
|
||||
@ -8,11 +8,6 @@ import (
|
||||
"go.unistack.org/micro/v3/server"
|
||||
)
|
||||
|
||||
var (
|
||||
_ server.HandlerWrapper = NewServerHandlerWrapper()
|
||||
_ server.SubscriberWrapper = NewServerSubscriberWrapper()
|
||||
)
|
||||
|
||||
func NewOptions(opts ...Option) Options {
|
||||
options := Options{
|
||||
ServerHandlerFn: DefaultServerHandlerFn,
|
||||
@ -31,13 +26,13 @@ type Options struct {
|
||||
|
||||
type Option func(*Options)
|
||||
|
||||
func ServerHandlerFn(fn func(context.Context, server.Request, interface{}, error) error) Option {
|
||||
func ServerHandlerFunc(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 {
|
||||
func ServerSubscriberFunc(fn func(context.Context, server.Message, error) error) Option {
|
||||
return func(o *Options) {
|
||||
o.ServerSubscriberFn = fn
|
||||
}
|
||||
@ -52,16 +47,17 @@ var (
|
||||
}
|
||||
)
|
||||
|
||||
type wrapper struct {
|
||||
var Hook = NewHook()
|
||||
|
||||
type hook struct {
|
||||
opts Options
|
||||
}
|
||||
|
||||
func NewServerHandlerWrapper(opts ...Option) server.HandlerWrapper {
|
||||
handler := &wrapper{opts: NewOptions(opts...)}
|
||||
return handler.HandlerFunc
|
||||
func NewHook(opts ...Option) *hook {
|
||||
return &hook{opts: NewOptions(opts...)}
|
||||
}
|
||||
|
||||
func (w *wrapper) HandlerFunc(fn server.HandlerFunc) server.HandlerFunc {
|
||||
func (w *hook) ServerHandler(next server.FuncHandler) server.FuncHandler {
|
||||
return func(ctx context.Context, req server.Request, rsp interface{}) (err error) {
|
||||
defer func() {
|
||||
r := recover()
|
||||
@ -74,17 +70,12 @@ func (w *wrapper) HandlerFunc(fn server.HandlerFunc) server.HandlerFunc {
|
||||
err = w.opts.ServerHandlerFn(ctx, req, rsp, fmt.Errorf("%v", r))
|
||||
}
|
||||
}()
|
||||
err = fn(ctx, req, rsp)
|
||||
err = next(ctx, req, rsp)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
func NewServerSubscriberWrapper(opts ...Option) server.SubscriberWrapper {
|
||||
handler := &wrapper{opts: NewOptions(opts...)}
|
||||
return handler.SubscriberFunc
|
||||
}
|
||||
|
||||
func (w *wrapper) SubscriberFunc(fn server.SubscriberFunc) server.SubscriberFunc {
|
||||
func (w *hook) ServerSubscriber(next server.FuncSubHandler) server.FuncSubHandler {
|
||||
return func(ctx context.Context, msg server.Message) (err error) {
|
||||
defer func() {
|
||||
r := recover()
|
||||
@ -97,74 +88,7 @@ func (w *wrapper) SubscriberFunc(fn server.SubscriberFunc) server.SubscriberFunc
|
||||
err = w.opts.ServerSubscriberFn(ctx, msg, fmt.Errorf("%v", r))
|
||||
}
|
||||
}()
|
||||
err = fn(ctx, msg)
|
||||
err = next(ctx, msg)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
func NewClientWrapper() client.Wrapper {
|
||||
return func(c client.Client) client.Client {
|
||||
handler := &wrapper{
|
||||
clientClient: c,
|
||||
}
|
||||
return handler
|
||||
}
|
||||
}
|
||||
|
||||
func NewCallWrapper() client.CallWrapper {
|
||||
return func(fn client.CallFunc) client.CallFunc {
|
||||
handler := &wrapper{
|
||||
clientCallFunc: fn,
|
||||
}
|
||||
return handler.CallFunc
|
||||
}
|
||||
}
|
||||
|
||||
func (w *wrapper) CallFunc(ctx context.Context, addr string, req client.Request, rsp interface{}, opts client.CallOptions) (err error) {
|
||||
defer func() {
|
||||
r := recover()
|
||||
switch verr := r.(type) {
|
||||
case nil:
|
||||
return
|
||||
case error:
|
||||
err = w.clientCallFunc(ctx, addr, req, rsp, opts, verr)
|
||||
default:
|
||||
err = w.clientCallFunc(ctx, addr, req, rsp, opts, fmt.Errorf("%v", r))
|
||||
}
|
||||
}()
|
||||
err = w.CallFunc(ctx, addr, req, rsp, opts)
|
||||
return err
|
||||
}
|
||||
|
||||
func (w *wrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
|
||||
defer func() {
|
||||
r := recover()
|
||||
switch verr := r.(type) {
|
||||
case nil:
|
||||
return
|
||||
case error:
|
||||
err = w.clientClient.Call(ctx, addr, req, rsp, opts, verr)
|
||||
default:
|
||||
err = w.clientClient.Call(ctx, addr, req, rsp, opts, fmt.Errorf("%v", r))
|
||||
}
|
||||
}()
|
||||
err = w.clientClient.Call(ctx, req, rsp, opts...)
|
||||
return err
|
||||
}
|
||||
|
||||
func (w *wrapper) Stream(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Stream, error) {
|
||||
|
||||
stream, err := w.Client.Stream(ctx, req, opts...)
|
||||
|
||||
return stream, err
|
||||
}
|
||||
|
||||
func (w *wrapper) Publish(ctx context.Context, p client.Message, opts ...client.PublishOption) error {
|
||||
|
||||
err := w.Client.Publish(ctx, p, opts...)
|
||||
|
||||
|
||||
return err
|
||||
}
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user