diff --git a/go.mod b/go.mod index 8d72d34..806a5a1 100644 --- a/go.mod +++ b/go.mod @@ -5,9 +5,7 @@ go 1.16 require ( github.com/VictoriaMetrics/metrics v1.17.3 // indirect github.com/armon/go-metrics v0.3.9 // indirect - github.com/evanphx/json-patch/v5 v5.5.0 // indirect github.com/fatih/color v1.12.0 // indirect - github.com/gobwas/ws v1.1.0 // indirect github.com/hashicorp/consul/api v1.9.1 // indirect github.com/hashicorp/go-hclog v0.16.2 // indirect github.com/hashicorp/go-immutable-radix v1.3.1 // indirect @@ -21,9 +19,6 @@ require ( github.com/segmentio/kafka-go v0.4.17 github.com/silas/dag v0.0.0-20210626123444-3804bac2d6d4 // indirect github.com/stretchr/testify v1.7.0 - github.com/unistack-org/micro-api-handler-rpc/v3 v3.3.0 - github.com/unistack-org/micro-api-router-register/v3 v3.2.2 - github.com/unistack-org/micro-api-router-static/v3 v3.2.1 github.com/unistack-org/micro-broker-segmentio/v3 v3.4.3-0.20210804134048-7916dafb4dfe //github.com/unistack-org/micro-client-drpc/v3 v3.0.0-00010101000000-000000000000 github.com/unistack-org/micro-client-grpc/v3 v3.4.0 @@ -44,15 +39,16 @@ require ( github.com/unistack-org/micro-server-grpc/v3 v3.3.7 github.com/unistack-org/micro-server-http/v3 v3.4.2 github.com/unistack-org/micro-server-tcp/v3 v3.3.2 + github.com/unistack-org/micro-wrapper-recovery/v3 v3.0.0 github.com/unistack-org/micro-wrapper-trace-opentracing/v3 v3.3.0 github.com/unistack-org/micro/v3 v3.6.2 + github.com/zeebo/assert v1.3.0 golang.org/x/crypto v0.0.0-20210813211128-0a44fdfbc16e // indirect golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d // indirect golang.org/x/sys v0.0.0-20210816183151-1e6c022a8912 // indirect golang.org/x/text v0.3.7 // indirect google.golang.org/genproto v0.0.0-20210816143620-e15ff196659d google.golang.org/grpc v1.40.0 - google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.1.0 // indirect google.golang.org/protobuf v1.27.1 gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect storj.io/drpc v0.0.24 diff --git a/go.sum b/go.sum index aa9c892..2db566d 100644 --- a/go.sum +++ b/go.sum @@ -72,9 +72,6 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.m github.com/envoyproxy/go-control-plane v0.9.9-0.20210217033140-668b12f5399d/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= github.com/envoyproxy/go-control-plane v0.9.9-0.20210512163311-63b5d3c536b0/go.mod h1:hliV/p42l8fGbc6Y9bQ70uLwIvmJyVE5k4iMKlh8wCQ= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= -github.com/evanphx/json-patch/v5 v5.2.0/go.mod h1:G79N1coSVB93tBe7j6PhzjmR3/2VvlbKOFpnXhI9Bw4= -github.com/evanphx/json-patch/v5 v5.5.0 h1:bAmFiUJ+o0o2B4OiTFeE3MqCOtyo+jjPP9iZ0VRxYUc= -github.com/evanphx/json-patch/v5 v5.5.0/go.mod h1:G79N1coSVB93tBe7j6PhzjmR3/2VvlbKOFpnXhI9Bw4= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU= github.com/fatih/color v1.12.0 h1:mRhaKNwANqRgUBGKmnI5ZxEk7QXmjQeCcuYFMX2bfcc= @@ -97,13 +94,6 @@ github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/me github.com/go-test/deep v1.0.2-0.20181118220953-042da051cf31/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= github.com/go-test/deep v1.0.2 h1:onZX1rnHT3Wv6cqNgYyFOOlgVKJrksuCMCRvJStbMYw= github.com/go-test/deep v1.0.2/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= -github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU= -github.com/gobwas/httphead v0.1.0/go.mod h1:O/RXo79gxV8G+RqlR/otEwx4Q36zl9rqC5u12GKvMCM= -github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og= -github.com/gobwas/pool v0.2.1/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw= -github.com/gobwas/ws v1.0.4/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM= -github.com/gobwas/ws v1.1.0 h1:7RFti/xnNkMJnrK7D1yQ/iCIB5OrrY/54/H930kIbHA= -github.com/gobwas/ws v1.1.0/go.mod h1:nzvNcVha5eUziGrbxFCo6qFIojQHjJV5cLYIbezhfL0= github.com/godbus/dbus v0.0.0-20190422162347-ade71ed3457e/go.mod h1:bBOAhwG1umN6/6ZUMtDFBMQR8jRg9O75tm9K00oMsK4= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4= @@ -226,7 +216,6 @@ github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU= github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= -github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= github.com/jmespath/go-jmespath v0.3.0/go.mod h1:9QtRXoHjLGCJ5IBSaohpXITPlowMeeYCZ7fLUTSywik= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= @@ -306,7 +295,6 @@ github.com/opencontainers/runc v0.1.1/go.mod h1:qT5XzbpPznkRYVz/mWwUaVBUv2rmF59P github.com/opencontainers/runtime-spec v0.1.2-0.20190507144316-5b71a03e2700/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0= github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs= github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc= -github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c h1:rp5dCmg/yLR3mgFuSOe4oEnDDmGLROTvMragMUXpTQw= github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c/go.mod h1:X07ZCGwUbLaax7L0S3Tw4hpejzu63ZrrQiUe6W0hcy0= github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= github.com/pascaldekloe/goe v0.1.0 h1:cBOtyMzM9HTpWjXfbbunk26uA6nG3a8n06Wieeh0MwY= @@ -377,12 +365,6 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM= -github.com/unistack-org/micro-api-handler-rpc/v3 v3.3.0 h1:u+uobYW44rfHSQ8HfRs2kHzoG2SJcjDii/NRSnB3UXg= -github.com/unistack-org/micro-api-handler-rpc/v3 v3.3.0/go.mod h1:Y3EVOGcYCDMJCvoCbW2v/RRunGuvR9fT/pjRZeDcvFQ= -github.com/unistack-org/micro-api-router-register/v3 v3.2.2 h1:O8tIP+NFqyp/3F8vYnDMTati4etc2URxUSUH1VrEqOY= -github.com/unistack-org/micro-api-router-register/v3 v3.2.2/go.mod h1:MZeqvmekQB/avrTRajp86+s7GuHkQ8HAee+hZtY+dWw= -github.com/unistack-org/micro-api-router-static/v3 v3.2.1 h1:FMcEb8NYYX72OyQuMKHbtdEQV+eOFbmKyvwSY8cgan8= -github.com/unistack-org/micro-api-router-static/v3 v3.2.1/go.mod h1:vP8VMZrJtlimEQOgYnLJsjnaUMeULNMPBkHEK5DA7G0= github.com/unistack-org/micro-broker-segmentio/v3 v3.4.3-0.20210804134048-7916dafb4dfe h1:uhXdevTWfyH83V9X56obz467ddVO9VIl/qhCRrN6MWw= github.com/unistack-org/micro-broker-segmentio/v3 v3.4.3-0.20210804134048-7916dafb4dfe/go.mod h1:YlRoI+1nS7q5TyHLn+wBc7qqhUy4tFkAFJKtyyoVnmQ= github.com/unistack-org/micro-client-grpc/v3 v3.4.0 h1:53BD9KpD8zhKa+wz8gHnzxRkpnME6elbb6cj2NCRDtM= @@ -412,8 +394,6 @@ github.com/unistack-org/micro-config-vault/v3 v3.5.0/go.mod h1:GJKV/gu55BWq3eCNw github.com/unistack-org/micro-meter-victoriametrics/v3 v3.3.3 h1:eKe0IjMm5e02rkhALo1FcG6UxYKOsJ5HU1loGKAUyPs= github.com/unistack-org/micro-meter-victoriametrics/v3 v3.3.3/go.mod h1:cDz4YigqJ3e3/2A8ze5SdJA8Z7KJf/s0VlIA6w97EHw= github.com/unistack-org/micro-proto v0.0.5/go.mod h1:EuI7UlfGXmT1hy6WacULib9LbNgRnDYQvTCFoLgKM2I= -github.com/unistack-org/micro-proto v0.0.6 h1:Qd4mL46lzQz0qhwvkmkihetFRGzk49THf+7E3Bl+s0Y= -github.com/unistack-org/micro-proto v0.0.6/go.mod h1:EuI7UlfGXmT1hy6WacULib9LbNgRnDYQvTCFoLgKM2I= github.com/unistack-org/micro-proto v0.0.8 h1:g4UZGQGeYGI3CFJtjuEm47aouYPviG8SDhSifl0831w= github.com/unistack-org/micro-proto v0.0.8/go.mod h1:GYO53DWmeldRIo90cAdQx8bLr/WJMxW62W4ja74p1Ac= github.com/unistack-org/micro-router-register/v3 v3.2.2 h1:lYCymDHkJfhZWYQ4+Sb7Fu+NlqoysQCnpJytHGhdnws= @@ -424,11 +404,11 @@ github.com/unistack-org/micro-server-http/v3 v3.4.2 h1:PP+1zlqiOjY+4rpHDiyR+dp91 github.com/unistack-org/micro-server-http/v3 v3.4.2/go.mod h1:Uck3mzXwaCmf0TZnM/9rDrptkxtgg7go0p8h7wBGIxo= github.com/unistack-org/micro-server-tcp/v3 v3.3.2 h1:x5FjxOuI61Jy6OzgCWElCRCWP9NffGNWajz/WJPEzdQ= github.com/unistack-org/micro-server-tcp/v3 v3.3.2/go.mod h1:981+kGQT3Js2LJrNaI2AyP/NcbnL7GU8WTnlRxj07Ao= +github.com/unistack-org/micro-wrapper-recovery/v3 v3.0.0 h1:g3V56WCzzQE9SKxRSygFQ3gtjxRH6Hnl/FfyMxiPmW0= +github.com/unistack-org/micro-wrapper-recovery/v3 v3.0.0/go.mod h1:pclgUgRdNAPmsWiBlbcf0J7BQnrycGUV7UGjhOL6jVY= github.com/unistack-org/micro-wrapper-trace-opentracing/v3 v3.3.0 h1:2l//2Ej+CpmfHOdvQTnFjFM/a2MduA0YOFiL7tPs/Qw= github.com/unistack-org/micro-wrapper-trace-opentracing/v3 v3.3.0/go.mod h1:2NX5VQ2NV6GZVs3d5WypcBkuu/ybUuoMUnL5yWXEtIM= -github.com/unistack-org/micro/v3 v3.2.1/go.mod h1:J8XxJj4Pqa3Ee0a4biRRtut7UwTlfBq8QRe+s4PKGS0= github.com/unistack-org/micro/v3 v3.2.2/go.mod h1:J8XxJj4Pqa3Ee0a4biRRtut7UwTlfBq8QRe+s4PKGS0= -github.com/unistack-org/micro/v3 v3.3.13/go.mod h1:98hNcMXp/WyWJwLwCuwrhN1Jm7aCWaRNsMfRjK8Fq+Y= github.com/unistack-org/micro/v3 v3.3.17/go.mod h1:022EOEZZ789hZY3yB5ZSMXU6jLiadBgcNB/cpediV3c= github.com/unistack-org/micro/v3 v3.3.19/go.mod h1:LXmPfbJnJNvL0kQs8HfnkV3Wya2Wb+C7keVq++RCZnk= github.com/unistack-org/micro/v3 v3.4.7/go.mod h1:LXmPfbJnJNvL0kQs8HfnkV3Wya2Wb+C7keVq++RCZnk= @@ -542,7 +522,6 @@ golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20201207223542-d4d67f95c62d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -608,8 +587,6 @@ google.golang.org/grpc v1.37.1/go.mod h1:NREThFqKR1f3iQ6oBuvc5LadQuXVGo9rkm5ZGrQ google.golang.org/grpc v1.39.0/go.mod h1:PImNr+rS9TWYb2O4/emRugxiyHZ5JyHW5F+RPnDzfrE= google.golang.org/grpc v1.40.0 h1:AGJ0Ih4mHjSeibYkFGh1dD9KJ/eOtZ93I6hoHhukQ5Q= google.golang.org/grpc v1.40.0/go.mod h1:ogyxbiOoUXAkP+4+xa6PZSE9DZgIHtSpzjDTB9KAK34= -google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.1.0 h1:M1YKkFIboKNieVO5DLUEVzQfGwJD30Nv2jfUgzb5UcE= -google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.1.0/go.mod h1:6Kw0yEErY5E/yWrBtf03jp27GLLJujG4z/JK95pnjjw= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= diff --git a/wrapper/recovery/recovery_test.go b/wrapper/recovery/recovery_test.go new file mode 100644 index 0000000..01eb3ce --- /dev/null +++ b/wrapper/recovery/recovery_test.go @@ -0,0 +1,99 @@ +package recovery_test + +import ( + "context" + "fmt" + "testing" + + cli "github.com/unistack-org/micro-client-grpc/v3" + jsoncodec "github.com/unistack-org/micro-codec-json/v3" + rrouter "github.com/unistack-org/micro-router-register/v3" + srv "github.com/unistack-org/micro-server-grpc/v3" + recwrapper "github.com/unistack-org/micro-wrapper-recovery/v3" + "github.com/unistack-org/micro/v3/broker" + "github.com/unistack-org/micro/v3/client" + "github.com/unistack-org/micro/v3/errors" + "github.com/unistack-org/micro/v3/register" + "github.com/unistack-org/micro/v3/router" + "github.com/unistack-org/micro/v3/server" +) + +type Test interface { + Method(ctx context.Context, in *TestRequest, opts ...client.CallOption) (*TestResponse, error) +} + +type TestRequest struct { + IsPanic bool +} +type TestResponse struct{} + +type testHandler struct{} + +func (t *testHandler) Method(ctx context.Context, req *TestRequest, rsp *TestResponse) error { + if req.IsPanic { + panic("panic here") + } + return nil +} + +func TestRecovery(t *testing.T) { + // setup + reg := register.NewRegister() + brk := broker.NewBroker(broker.Register(reg)) + + name := "test" + id := "id-1234567890" + version := "1.2.3.4" + rt := rrouter.NewRouter(router.Register(reg)) + + c := cli.NewClient( + client.Codec("application/grpc+json", jsoncodec.NewCodec()), + client.Codec("application/json", jsoncodec.NewCodec()), + client.Router(rt), + ) + + rfn := func(ctx context.Context, req server.Request, rsp interface{}, err error) error { + return errors.BadRequest("id-1234567890", "handled panic: %v", err) + } + + s := srv.NewServer( + server.Codec("application/grpc+json", jsoncodec.NewCodec()), + server.Codec("application/json", jsoncodec.NewCodec()), + server.Name(name), + server.Version(version), + server.ID(id), + server.Register(reg), + server.Broker(brk), + server.WrapHandler( + recwrapper.NewHandlerWrapper(rfn), + ), + ) + + if err := s.Init(); err != nil { + t.Fatal(err) + } + type Test struct { + *testHandler + } + + if err := s.Handle(s.NewHandler(&Test{new(testHandler)})); err != nil { + t.Fatal(err) + } + + if err := s.Start(); err != nil { + t.Fatalf("Unexpected error starting server: %v", err) + } + defer func() { + _ = s.Stop() + }() + + req := c.NewRequest(name, "Test.Method", &TestRequest{IsPanic: true}, client.RequestContentType("application/json")) + rsp := TestResponse{} + + err := c.Call(context.TODO(), req, &rsp) + if err == nil { + t.Fatalf("panic happens, but handler not return err") + } + + fmt.Printf("%v\n", err) +}