2024-04-23 11:21:57 +03:00
|
|
|
package recovery
|
2021-08-17 10:02:21 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2022-11-28 12:29:53 +03:00
|
|
|
"go.unistack.org/micro/v3/errors"
|
2021-10-26 23:45:34 +03:00
|
|
|
"go.unistack.org/micro/v3/server"
|
2021-08-17 10:02:21 +03:00
|
|
|
)
|
|
|
|
|
2022-11-28 12:29:53 +03:00
|
|
|
func NewOptions(opts ...Option) Options {
|
|
|
|
options := Options{
|
|
|
|
ServerHandlerFn: DefaultServerHandlerFn,
|
|
|
|
ServerSubscriberFn: DefaultServerSubscriberFn,
|
|
|
|
}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
|
|
|
type Options struct {
|
|
|
|
ServerHandlerFn func(context.Context, server.Request, interface{}, error) error
|
|
|
|
ServerSubscriberFn func(context.Context, server.Message, error) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type Option func(*Options)
|
|
|
|
|
2024-04-23 11:21:57 +03:00
|
|
|
func ServerHandlerFunc(fn func(context.Context, server.Request, interface{}, error) error) Option {
|
2022-11-28 12:29:53 +03:00
|
|
|
return func(o *Options) {
|
|
|
|
o.ServerHandlerFn = fn
|
|
|
|
}
|
2021-08-17 10:02:21 +03:00
|
|
|
}
|
|
|
|
|
2024-04-23 11:21:57 +03:00
|
|
|
func ServerSubscriberFunc(fn func(context.Context, server.Message, error) error) Option {
|
2022-11-28 12:29:53 +03:00
|
|
|
return func(o *Options) {
|
|
|
|
o.ServerSubscriberFn = fn
|
2021-08-17 10:02:21 +03:00
|
|
|
}
|
2022-11-28 12:29:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2024-04-23 11:21:57 +03:00
|
|
|
var Hook = NewHook()
|
|
|
|
|
|
|
|
type hook struct {
|
2022-11-28 12:29:53 +03:00
|
|
|
opts Options
|
|
|
|
}
|
|
|
|
|
2024-04-23 11:21:57 +03:00
|
|
|
func NewHook(opts ...Option) *hook {
|
|
|
|
return &hook{opts: NewOptions(opts...)}
|
2021-08-17 10:02:21 +03:00
|
|
|
}
|
|
|
|
|
2024-04-23 11:21:57 +03:00
|
|
|
func (w *hook) ServerHandler(next server.FuncHandler) server.FuncHandler {
|
2021-08-17 10:02:21 +03:00
|
|
|
return func(ctx context.Context, req server.Request, rsp interface{}) (err error) {
|
|
|
|
defer func() {
|
|
|
|
r := recover()
|
|
|
|
switch verr := r.(type) {
|
|
|
|
case nil:
|
|
|
|
return
|
|
|
|
case error:
|
2022-11-28 12:29:53 +03:00
|
|
|
err = w.opts.ServerHandlerFn(ctx, req, rsp, verr)
|
2021-08-17 10:02:21 +03:00
|
|
|
default:
|
2022-11-28 12:29:53 +03:00
|
|
|
err = w.opts.ServerHandlerFn(ctx, req, rsp, fmt.Errorf("%v", r))
|
2021-08-17 10:02:21 +03:00
|
|
|
}
|
|
|
|
}()
|
2024-04-23 11:21:57 +03:00
|
|
|
err = next(ctx, req, rsp)
|
2021-08-17 10:02:21 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-23 11:21:57 +03:00
|
|
|
func (w *hook) ServerSubscriber(next server.FuncSubHandler) server.FuncSubHandler {
|
2021-08-17 10:02:21 +03:00
|
|
|
return func(ctx context.Context, msg server.Message) (err error) {
|
|
|
|
defer func() {
|
|
|
|
r := recover()
|
|
|
|
switch verr := r.(type) {
|
|
|
|
case nil:
|
|
|
|
return
|
|
|
|
case error:
|
2022-11-28 12:29:53 +03:00
|
|
|
err = w.opts.ServerSubscriberFn(ctx, msg, verr)
|
2021-08-17 10:02:21 +03:00
|
|
|
default:
|
2022-11-28 12:29:53 +03:00
|
|
|
err = w.opts.ServerSubscriberFn(ctx, msg, fmt.Errorf("%v", r))
|
2021-08-17 10:02:21 +03:00
|
|
|
}
|
|
|
|
}()
|
2024-04-23 11:21:57 +03:00
|
|
|
err = next(ctx, msg)
|
2021-08-17 10:02:21 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|