Compare commits
106 Commits
Author | SHA1 | Date | |
---|---|---|---|
08389ffa3a | |||
08a832569a | |||
|
81c7d9cc9a | ||
4ba456d7a7 | |||
|
6f18745792 | ||
3e7795a981 | |||
|
382cd1db32 | ||
129b680834 | |||
|
62f3faecaa | ||
b469fa26bd | |||
|
5127f912dc | ||
eda442b1ab | |||
|
d5cc390c17 | ||
e348a6e9fb | |||
|
5d349e196d | ||
80e8f68a9f | |||
|
9926bd992b | ||
cbe4fb2f02 | |||
|
9854bf456a | ||
d40008c676 | |||
|
da6221e3b7 | ||
ac4aab65fb | |||
|
65b5fd8342 | ||
763b119289 | |||
|
619d4ebb99 | ||
39b8f0a01d | |||
|
ec40a36254 | ||
0fabb2c777 | |||
|
39ed1ba5b2 | ||
5cb43f196b | |||
|
99b6a44134 | ||
ee5f59121b | |||
|
5ecfb46d14 | ||
9c76e6a498 | |||
|
57a0f75e22 | ||
e376188d06 | |||
|
7d8fa279fa | ||
c6b165fc27 | |||
|
cefd04c64c | ||
c716595b1c | |||
|
5b78e842e4 | ||
69ac0c5bc0 | |||
|
c78998d57c | ||
53310928ad | |||
|
d2252ccede | ||
38e78fbde9 | |||
|
6748f177a3 | ||
971d9a46a3 | |||
|
ee9a96f2f6 | ||
737ff8146a | |||
|
e70b60ddee | ||
dc391902e9 | |||
|
c3f4cf564c | ||
9859c80c5b | |||
|
130aa5fe61 | ||
|
269e8208df | ||
ce5de2809c | |||
|
d881112f30 | ||
|
6616a1c448 | ||
982d1013b0 | |||
|
7b5e5505d0 | ||
648bd8bd73 | |||
|
f839ee7e79 | ||
93c4f593a3 | |||
|
2fe3bafe88 | ||
3b50129b45 | |||
|
1ce984d8da | ||
3da2958c4b | |||
|
c5968ca6a2 | ||
7f631a4f3d | |||
|
0a9274c9a3 | ||
3c91b24896 | |||
a89f1830a4 | |||
0f99bba64f | |||
|
cca6260434 | ||
dd792ba674 | |||
4ce7122065 | |||
|
2b27d340bd | ||
7991010515 | |||
|
e468c43226 | ||
|
e1f64501a1 | ||
|
4233548704 | ||
|
b570f87400 | ||
|
54ae8cd36f | ||
2c56091b65 | |||
|
40c08a46d7 | ||
ac021a800f | |||
|
a02a5109df | ||
|
0a4fe04db2 | ||
d7df6a34f9 | |||
|
310b1e1e9f | ||
|
54245a08ab | ||
|
8f7ad0ded7 | ||
19a8caf8b4 | |||
|
e8ff017087 | ||
6f9f665399 | |||
|
c340ca6f7a | ||
82609130b5 | |||
|
e37b3e87b0 | ||
ee4292fb55 | |||
|
5996bfe584 | ||
fe7b259e60 | |||
0408c6825f | |||
|
8f2e679ea3 | ||
|
53de1e50ea | ||
372c979f27 |
2
.github/workflows/autoapprove.yml
vendored
2
.github/workflows/autoapprove.yml
vendored
@ -13,7 +13,7 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: approve
|
- name: approve
|
||||||
uses: hmarr/auto-approve-action@v2
|
uses: hmarr/auto-approve-action@v3
|
||||||
if: github.actor == 'vtolstov' || github.actor == 'dependabot[bot]'
|
if: github.actor == 'vtolstov' || github.actor == 'dependabot[bot]'
|
||||||
id: approve
|
id: approve
|
||||||
with:
|
with:
|
||||||
|
8
.github/workflows/build.yml
vendored
8
.github/workflows/build.yml
vendored
@ -10,13 +10,13 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: setup
|
- name: setup
|
||||||
uses: actions/setup-go@v2
|
uses: actions/setup-go@v3
|
||||||
with:
|
with:
|
||||||
go-version: 1.16
|
go-version: 1.17
|
||||||
- name: checkout
|
- name: checkout
|
||||||
uses: actions/checkout@v3
|
uses: actions/checkout@v3
|
||||||
- name: cache
|
- name: cache
|
||||||
uses: actions/cache@v2
|
uses: actions/cache@v3
|
||||||
with:
|
with:
|
||||||
path: ~/go/pkg/mod
|
path: ~/go/pkg/mod
|
||||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||||
@ -34,7 +34,7 @@ jobs:
|
|||||||
- name: checkout
|
- name: checkout
|
||||||
uses: actions/checkout@v3
|
uses: actions/checkout@v3
|
||||||
- name: lint
|
- name: lint
|
||||||
uses: golangci/golangci-lint-action@v3.1.0
|
uses: golangci/golangci-lint-action@v3.4.0
|
||||||
continue-on-error: true
|
continue-on-error: true
|
||||||
with:
|
with:
|
||||||
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
||||||
|
10
.github/workflows/codeql-analysis.yml
vendored
10
.github/workflows/codeql-analysis.yml
vendored
@ -45,12 +45,12 @@ jobs:
|
|||||||
- name: checkout
|
- name: checkout
|
||||||
uses: actions/checkout@v3
|
uses: actions/checkout@v3
|
||||||
- name: setup
|
- name: setup
|
||||||
uses: actions/setup-go@v2
|
uses: actions/setup-go@v3
|
||||||
with:
|
with:
|
||||||
go-version: 1.16
|
go-version: 1.17
|
||||||
# Initializes the CodeQL tools for scanning.
|
# Initializes the CodeQL tools for scanning.
|
||||||
- name: init
|
- name: init
|
||||||
uses: github/codeql-action/init@v1
|
uses: github/codeql-action/init@v2
|
||||||
with:
|
with:
|
||||||
languages: ${{ matrix.language }}
|
languages: ${{ matrix.language }}
|
||||||
# If you wish to specify custom queries, you can do so here or in a config file.
|
# If you wish to specify custom queries, you can do so here or in a config file.
|
||||||
@ -61,7 +61,7 @@ jobs:
|
|||||||
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
||||||
# If this step fails, then you should remove it and run the build manually (see below)
|
# If this step fails, then you should remove it and run the build manually (see below)
|
||||||
- name: autobuild
|
- name: autobuild
|
||||||
uses: github/codeql-action/autobuild@v1
|
uses: github/codeql-action/autobuild@v2
|
||||||
|
|
||||||
# ℹ️ Command-line programs to run using the OS shell.
|
# ℹ️ Command-line programs to run using the OS shell.
|
||||||
# 📚 https://git.io/JvXDl
|
# 📚 https://git.io/JvXDl
|
||||||
@ -75,4 +75,4 @@ jobs:
|
|||||||
# make release
|
# make release
|
||||||
|
|
||||||
- name: analyze
|
- name: analyze
|
||||||
uses: github/codeql-action/analyze@v1
|
uses: github/codeql-action/analyze@v2
|
||||||
|
2
.github/workflows/dependabot-automerge.yml
vendored
2
.github/workflows/dependabot-automerge.yml
vendored
@ -15,7 +15,7 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
- name: metadata
|
- name: metadata
|
||||||
id: metadata
|
id: metadata
|
||||||
uses: dependabot/fetch-metadata@v1.3.0
|
uses: dependabot/fetch-metadata@v1.3.6
|
||||||
with:
|
with:
|
||||||
github-token: "${{ secrets.TOKEN }}"
|
github-token: "${{ secrets.TOKEN }}"
|
||||||
- name: merge
|
- name: merge
|
||||||
|
8
.github/workflows/pr.yml
vendored
8
.github/workflows/pr.yml
vendored
@ -10,13 +10,13 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: setup
|
- name: setup
|
||||||
uses: actions/setup-go@v2
|
uses: actions/setup-go@v3
|
||||||
with:
|
with:
|
||||||
go-version: 1.16
|
go-version: 1.17
|
||||||
- name: checkout
|
- name: checkout
|
||||||
uses: actions/checkout@v3
|
uses: actions/checkout@v3
|
||||||
- name: cache
|
- name: cache
|
||||||
uses: actions/cache@v2
|
uses: actions/cache@v3
|
||||||
with:
|
with:
|
||||||
path: ~/go/pkg/mod
|
path: ~/go/pkg/mod
|
||||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||||
@ -34,7 +34,7 @@ jobs:
|
|||||||
- name: checkout
|
- name: checkout
|
||||||
uses: actions/checkout@v3
|
uses: actions/checkout@v3
|
||||||
- name: lint
|
- name: lint
|
||||||
uses: golangci/golangci-lint-action@v3.1.0
|
uses: golangci/golangci-lint-action@v3.4.0
|
||||||
continue-on-error: true
|
continue-on-error: true
|
||||||
with:
|
with:
|
||||||
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
||||||
|
7
go.mod
7
go.mod
@ -2,4 +2,9 @@ module go.unistack.org/micro-wrapper-recovery/v3
|
|||||||
|
|
||||||
go 1.16
|
go 1.16
|
||||||
|
|
||||||
require go.unistack.org/micro/v3 v3.8.21
|
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
|
||||||
|
)
|
||||||
|
150
wrapper.go
150
wrapper.go
@ -1,29 +1,63 @@
|
|||||||
package recovery // import "go.unistack.org/micro-wrapper-recovery/v3"
|
package recovery
|
||||||
|
|
||||||
import (
|
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 {
|
func NewOptions(opts ...Option) Options {
|
||||||
serverHandlerFunc func(context.Context, server.Request, interface{}, error) error
|
options := Options{
|
||||||
serverSubscriberFunc func(context.Context, server.Message, error) error
|
ServerHandlerFn: DefaultServerHandlerFn,
|
||||||
/*
|
ServerSubscriberFn: DefaultServerSubscriberFn,
|
||||||
clientCallFunc func(context.Context, string, client.Request, interface{}, client.CallOptions, error) error
|
}
|
||||||
clientClient func(client.Client, error) error
|
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
|
||||||
}
|
|
||||||
return handler.HandlerFunc
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *wrapper) HandlerFunc(fn server.HandlerFunc) server.HandlerFunc {
|
type Option func(*Options)
|
||||||
|
|
||||||
|
func ServerHandlerFunc(fn func(context.Context, server.Request, interface{}, error) error) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.ServerHandlerFn = fn
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ServerSubscriberFunc(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)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
var Hook = NewHook()
|
||||||
|
|
||||||
|
type hook struct {
|
||||||
|
opts Options
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHook(opts ...Option) *hook {
|
||||||
|
return &hook{opts: NewOptions(opts...)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *hook) ServerHandler(next server.FuncHandler) server.FuncHandler {
|
||||||
return func(ctx context.Context, req server.Request, rsp interface{}) (err error) {
|
return func(ctx context.Context, req server.Request, rsp interface{}) (err error) {
|
||||||
defer func() {
|
defer func() {
|
||||||
r := recover()
|
r := recover()
|
||||||
@ -31,24 +65,17 @@ 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 = next(ctx, req, rsp)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSubscriberWrapper(fn func(context.Context, server.Message, error) error) server.SubscriberWrapper {
|
func (w *hook) ServerSubscriber(next server.FuncSubHandler) server.FuncSubHandler {
|
||||||
handler := &wrapper{
|
|
||||||
serverSubscriberFunc: fn,
|
|
||||||
}
|
|
||||||
return handler.SubscriberFunc
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w *wrapper) SubscriberFunc(fn server.SubscriberFunc) server.SubscriberFunc {
|
|
||||||
return func(ctx context.Context, msg server.Message) (err error) {
|
return func(ctx context.Context, msg server.Message) (err error) {
|
||||||
defer func() {
|
defer func() {
|
||||||
r := recover()
|
r := recover()
|
||||||
@ -56,79 +83,12 @@ 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 = next(ctx, msg)
|
||||||
return err
|
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…
Reference in New Issue
Block a user