From b24a7b24cdd076cd2eea0cb622bbcf8d0fa6e17a Mon Sep 17 00:00:00 2001 From: Sergey Gladkovskiy Date: Wed, 28 Jul 2021 14:56:30 +0300 Subject: [PATCH] Some refactoring, linting, tests --- .gitignore | 6 +- Makefile | 38 ++++++++----- README.md | 12 ++-- gin.go | 11 ++-- gin_test.go | 46 +++++++++------ go.mod | 14 ++--- go.sum | 126 +++++++++++++++++++++++++++++++++++------- grpc.go | 26 ++++++--- grpc_internal_test.go | 71 ++++++++++++++++++++++++ grpc_test.go | 19 ++++--- validator.go | 42 +++++++++----- 11 files changed, 314 insertions(+), 97 deletions(-) create mode 100644 grpc_internal_test.go diff --git a/.gitignore b/.gitignore index b5225a5..53c28e2 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,8 @@ vendor .golangci.yml c.out -coverage.html \ No newline at end of file +coverage.html +tests.mk +linter.mk +.golangci*.yml +bin \ No newline at end of file diff --git a/Makefile b/Makefile index 9eb750d..80c6cbe 100644 --- a/Makefile +++ b/Makefile @@ -2,21 +2,33 @@ deps: go mod vendor .PHONY: deps -get_lint_config: - @[ -f ./.golangci.yml ] && echo ".golangci.yml exists" || ( echo "getting .golangci.yml" && curl -O https://raw.githubusercontent.com/microparts/docker-golang/master/lint/.golangci.yml ) -.PHONY: get_lint_config +# ---- +## LINTER stuff start + +linter_include_check: + @[ -f linter.mk ] && echo "linter.mk include exists" || (echo "getting linter.mk from github.com" && curl -sO https://raw.githubusercontent.com/spacetab-io/makefiles/master/golang/linter.mk) -lint: get_lint_config - golangci-lint run .PHONY: lint +lint: linter_include_check + @make -f linter.mk go_lint + +## LINTER stuff end +# ---- + +# ---- +## TESTS stuff start + +tests_include_check: + @[ -f tests.mk ] && echo "tests.mk include exists" || (echo "getting tests.mk from github.com" && curl -sO https://raw.githubusercontent.com/spacetab-io/makefiles/master/golang/tests.mk) -test-unit: - go test $$(go list ./...) --race --cover -count=1 -timeout 1s -coverprofile=c.out -v -.PHONY: test-unit +tests: tests_include_check + @make -f tests.mk go_tests +.PHONY: tests -coverage-html: - go tool cover -html=c.out -o coverage.html -.PHONE: coverage-html +tests_html: tests_include_check + @make -f tests.mk go_tests_html + @open coverage.html +.PHONY: tests -test: deps test-unit coverage-html -.PHONY: test \ No newline at end of file +## TESTS stuff end +# ---- \ No newline at end of file diff --git a/README.md b/README.md index 93c67be..9f3ecd7 100644 --- a/README.md +++ b/README.md @@ -10,15 +10,15 @@ package main import ( "errors" "net/http" - + "github.com/gin-gonic/gin" - "github.com/microparts/errors-go-gin" + "github.com/spacetab-io/errors-gin-go" ) func main() { r := gin.New() - r.GET("/", func(c*gin.Context) {c.JSON(http.StatusOK,`{"status":"ok"}`)}) + r.GET("/", func(c *gin.Context) { c.JSON(http.StatusOK, `{"status":"ok"}`) }) r.GET("/err", func(c *gin.Context) { ginerrors.Response(c, errors.New("error")) }) _ = r.Run(":8080") } @@ -26,8 +26,8 @@ func main() { ## Linter -Lint code with [golangci-lint](https://github.com/golangci/golangci-lint) and -[custom config](https://github.com/microparts/docker-golang/blob/master/lint/.golangci.yml) for it: +Lint code with [golangci-lint](https://github.com/golangci/golangci-lint) and +[custom config](https://github.com/spacetab-io/linter-go/blob/master/.golangci.yml) for it: make lint @@ -35,4 +35,4 @@ Lint code with [golangci-lint](https://github.com/golangci/golangci-lint) and Test code with race checking and generation coverage profile: - make test + make tests diff --git a/gin.go b/gin.go index fd1dd14..e937fbe 100644 --- a/gin.go +++ b/gin.go @@ -6,25 +6,24 @@ import ( "strings" "github.com/gin-gonic/gin" + "github.com/go-playground/validator/v10" + errs "github.com/spacetab-io/errors-go" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - "gopkg.in/go-playground/validator.v9" - - errs "github.com/microparts/errors-go" ) const ( validationErrorMessage = "validation error" ) -//Response makes common error response +// Response makes common error response. func Response(c *gin.Context, err interface{}) { errCode, data := MakeResponse(err, getLang(c)) resp := errs.Response{Error: *data} c.AbortWithStatusJSON(errCode, resp) } -//MakeResponse makes ErrorObject based on error type +// MakeResponse makes ErrorObject based on error type. func MakeResponse(err interface{}, lang langName) (int, *errs.ErrorObject) { errObj := &errs.ErrorObject{} errCode := http.StatusBadRequest @@ -39,9 +38,11 @@ func MakeResponse(err interface{}, lang langName) (int, *errs.ErrorObject) { case []error: errCode = http.StatusInternalServerError msgs := make([]string, 0) + for _, e := range err.([]error) { msgs = append(msgs, e.Error()) } + errObj.Message = strings.Join(msgs, "; ") case validator.ValidationErrors: errCode = http.StatusUnprocessableEntity diff --git a/gin_test.go b/gin_test.go index 8cd4910..8877762 100644 --- a/gin_test.go +++ b/gin_test.go @@ -1,18 +1,18 @@ -package ginerrors +package ginerrors_test import ( + "context" "database/sql" "errors" - "fmt" "net/http" "net/http/httptest" "testing" "github.com/gin-gonic/gin" + "github.com/go-playground/validator/v10" + ginerrors "github.com/microparts/errors-go-gin" + errs "github.com/spacetab-io/errors-go" "github.com/stretchr/testify/assert" - "gopkg.in/go-playground/validator.v9" - - errs "github.com/microparts/errors-go" ) func TestMakeResponse(t *testing.T) { @@ -24,25 +24,32 @@ func TestMakeResponse(t *testing.T) { } errType := errs.ErrorTypeError + // nolint: goerr113 cases := []testCase{ {name: "common error", err: errors.New("common err"), httpCode: http.StatusBadRequest, errObject: &errs.ErrorObject{Message: "common err", Type: &errType}}, {name: "validation error", err: makeValidationError(), httpCode: http.StatusUnprocessableEntity, errObject: &errs.ErrorObject{Message: "validation error", Validation: map[errs.FieldName][]errs.ValidationError{"String": {"Ошибка валидации для свойства `String` с правилом `%!s(MISSING)`"}}, Type: &errType}}, - {name: "mux err no method allowed", err: ErrNoMethod, httpCode: http.StatusMethodNotAllowed, errObject: &errs.ErrorObject{Message: ErrNoMethod.Error(), Type: &errType}}, - {name: "mux err route not found", err: ErrNotFound, httpCode: http.StatusNotFound, errObject: &errs.ErrorObject{Message: ErrNotFound.Error(), Type: &errType}}, + {name: "mux err no method allowed", err: ginerrors.ErrNoMethod, httpCode: http.StatusMethodNotAllowed, errObject: &errs.ErrorObject{Message: ginerrors.ErrNoMethod.Error(), Type: &errType}}, + {name: "mux err route not found", err: ginerrors.ErrNotFound, httpCode: http.StatusNotFound, errObject: &errs.ErrorObject{Message: ginerrors.ErrNotFound.Error(), Type: &errType}}, {name: "errors slice", err: []error{errors.New("common err 1"), errors.New("common err 2")}, httpCode: http.StatusInternalServerError, errObject: &errs.ErrorObject{Message: "common err 1; common err 2", Type: &errType}}, {name: "map of errors", err: map[string]error{"common_err": errors.New("common err")}, httpCode: http.StatusBadRequest, errObject: &errs.ErrorObject{Message: map[string]string{"common_err": "common err"}, Type: &errType}}, - {name: "record not found", err: ErrRecordNotFound, httpCode: http.StatusNotFound, errObject: &errs.ErrorObject{Message: ErrRecordNotFound.Error(), Type: &errType}}, - {name: "sql error no rows", err: sql.ErrNoRows, httpCode: http.StatusNotFound, errObject: &errs.ErrorObject{Message: ErrRecordNotFound.Error(), Type: &errType}}, + {name: "record not found", err: ginerrors.ErrRecordNotFound, httpCode: http.StatusNotFound, errObject: &errs.ErrorObject{Message: ginerrors.ErrRecordNotFound.Error(), Type: &errType}}, + {name: "sql error no rows", err: sql.ErrNoRows, httpCode: http.StatusNotFound, errObject: &errs.ErrorObject{Message: ginerrors.ErrRecordNotFound.Error(), Type: &errType}}, {name: "sql error conn done", err: sql.ErrConnDone, httpCode: http.StatusInternalServerError, errObject: &errs.ErrorObject{Message: sql.ErrConnDone.Error(), Type: &errType}}, {name: "sql error tx done", err: sql.ErrTxDone, httpCode: http.StatusInternalServerError, errObject: &errs.ErrorObject{Message: sql.ErrTxDone.Error(), Type: &errType}}, } + + t.Parallel() + for _, testCase := range cases { + testCase := testCase t.Run(testCase.name, func(t *testing.T) { - errCode, errObject := MakeResponse(testCase.err, "ru") + t.Parallel() + + errCode, errObject := ginerrors.MakeResponse(testCase.err, "ru") assert.Equal(t, testCase.errObject, errObject, testCase.name) assert.Equal(t, testCase.httpCode, errCode, testCase.name) }) @@ -52,23 +59,28 @@ func TestMakeResponse(t *testing.T) { func setupRouter() *gin.Engine { r := gin.New() - r.NoRoute(func(c *gin.Context) { Response(c, ErrNotFound) }) + r.NoRoute(func(c *gin.Context) { ginerrors.Response(c, ginerrors.ErrNotFound) }) r.GET("/ping", func(c *gin.Context) { c.String(200, "pong") }) + return r } func TestResponse(t *testing.T) { + t.Parallel() + router := setupRouter() t.Run("not found", func(t *testing.T) { + t.Parallel() + w := httptest.NewRecorder() - req, _ := http.NewRequest("GET", "/pong", nil) + req, _ := http.NewRequestWithContext(context.Background(), "GET", "/pong", nil) router.ServeHTTP(w, req) assert.Equal(t, http.StatusNotFound, w.Code) - assert.Equal(t, "{\"error\":{\"message\":\"route not found\",\"type\":\"error\"}}\n", w.Body.String()) + assert.Equal(t, `{"error":{"message":"route not found","type":"error"}}`, w.Body.String()) }) } @@ -84,16 +96,16 @@ func makeValidationError() error { s := MyStruct{String: "awesome"} - err := validate.Struct(s) - if err != nil { - fmt.Printf("Err(s):\n%+v\n", err) + if err := validate.Struct(s); err != nil { + return errs.Wrap("makeValidationError", "validate.Struct", err) } s.String = "not awesome" + return validate.Struct(s) } -// ValidateMyVal implements validator.Func +// ValidateMyVal implements validator.Func. func ValidateMyVal(fl validator.FieldLevel) bool { return fl.Field().String() == "awesome" } diff --git a/go.mod b/go.mod index 73e3fa1..0645596 100644 --- a/go.mod +++ b/go.mod @@ -1,12 +1,12 @@ module github.com/microparts/errors-go-gin -go 1.12 +go 1.16 require ( - github.com/gin-gonic/gin v1.6.3 - github.com/microparts/errors-go v1.1.1 - github.com/stretchr/testify v1.4.0 - google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55 - google.golang.org/grpc v1.29.1 - gopkg.in/go-playground/validator.v9 v9.30.0 + github.com/gin-gonic/gin v1.7.2 + github.com/go-playground/validator/v10 v10.8.0 + github.com/spacetab-io/errors-go v1.3.0 + github.com/stretchr/testify v1.7.0 + google.golang.org/genproto v0.0.0-20210726200206-e7812ac95cc0 + google.golang.org/grpc v1.39.0 ) diff --git a/go.sum b/go.sum index cb76ce2..42b436f 100644 --- a/go.sum +++ b/go.sum @@ -1,43 +1,68 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= +github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= +github.com/cncf/xds/go v0.0.0-20210312221358-fbca930ec8ed/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= +github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/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/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= -github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14= -github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M= +github.com/gin-gonic/gin v1.7.2 h1:Tg03T9yM2xa8j6I3Z3oqLaQRSmKvxPd6g/2HJ6zICFA= +github.com/gin-gonic/gin v1.7.2/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY= github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A= github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q= github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no= github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= -github.com/go-playground/validator/v10 v10.2.0 h1:KgJ0snyC2R9VXYN2rneOtQcw5aHQB1Vv0sFl1UcHBOY= -github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI= +github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4= +github.com/go-playground/validator/v10 v10.8.0 h1:1kAa0fCrnpv+QYdkdcRzrRM7AyYs5o8+jZdJCz9xj6k= +github.com/go-playground/validator/v10 v10.8.0/go.mod h1:9JhgTzTaE31GZDpH/HSvHiRJrJ3iKAgqqH0Bl/Ocjdk= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I= github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= -github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ= +github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= +github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= +github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= +github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= +github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= +github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns= github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= -github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= +github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w= +github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY= github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= -github.com/microparts/errors-go v1.1.1 h1:2SHCwg7vhRz9B1FFA2oG5D3W8r7/fcPYug0m2acl9Cw= -github.com/microparts/errors-go v1.1.1/go.mod h1:ZhPeujudtuK3iE9S3rvrL5hrC8MdLyH/02lWcfgFqPA= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg= @@ -45,58 +70,119 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lN github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= +github.com/spacetab-io/errors-go v1.3.0 h1:7e9IcR5T8VUfLJxsx/S5BWYCLiaaQBdho8dihAphAb8= +github.com/spacetab-io/errors-go v1.3.0/go.mod h1:TJyCmPgzstb4mD7NI2QK9gTGbtzIR8FyoyL9obXqGa8= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= +github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= +go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 h1:/UOmuWzQfxxo9UtlXMwuQU8CMgg1eZXqTRwkSQJWKOI= +golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/lint v0.0.0-20210508222113-6edffad5e616/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= +golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= +golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 h1:4nGaVu0QrbjT/AK2PRLuQfQuh6DJve+pELhqTdAj3x0= +golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= +golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= -google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55 h1:gSJIx1SDwno+2ElGhA4+qG2zF97qiUzTM+rQ0klBOcE= google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= +google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= +google.golang.org/genproto v0.0.0-20210726200206-e7812ac95cc0 h1:VpRFBmFg/ol+rqJnkKLPjVebPNFbSxuj17B7bH1xMc8= +google.golang.org/genproto v0.0.0-20210726200206-e7812ac95cc0/go.mod h1:ob2IJxKrgPT52GcgX759i1sleT07tiKowYBGbczaW48= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= -google.golang.org/grpc v1.29.1 h1:EC2SB8S04d2r73uptxphDSUG+kTKVgjRPF+N3xpxRB4= -google.golang.org/grpc v1.29.1/go.mod h1:itym6AZVZYACWQqET3MqgPpjcuV5QH3BxFS3IjizoKk= +google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= +google.golang.org/grpc v1.33.1/go.mod h1:fr5YgcSWrqhRRxogOsw7RzIpsmvOZ6IcH4kBYTpR3n0= +google.golang.org/grpc v1.36.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU= +google.golang.org/grpc v1.39.0 h1:Klz8I9kdtkIN6EpHHUOMLCYhTn/2WAe5a0s1hcBkdTI= +google.golang.org/grpc v1.39.0/go.mod h1:PImNr+rS9TWYb2O4/emRugxiyHZ5JyHW5F+RPnDzfrE= +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= +google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ= +google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM= -gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE= -gopkg.in/go-playground/validator.v9 v9.30.0 h1:Wk0Z37oBmKj9/n+tPyBHZmeL19LaCoK3Qq48VwYENss= -gopkg.in/go-playground/validator.v9 v9.30.0/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/grpc.go b/grpc.go index b6accc4..2595a84 100644 --- a/grpc.go +++ b/grpc.go @@ -1,13 +1,13 @@ package ginerrors import ( - "errors" + "fmt" "net/http" + "github.com/go-playground/validator/v10" "google.golang.org/genproto/googleapis/rpc/errdetails" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - "gopkg.in/go-playground/validator.v9" ) type GRPCValidationError struct { @@ -31,7 +31,7 @@ var ( ) func UnwrapRPCError(st *status.Status) interface{} { - switch st.Code() { + switch st.Code() { //nolint: exhaustive case codes.NotFound: return ErrRecordNotFound case codes.Unavailable: @@ -44,7 +44,7 @@ func UnwrapRPCError(st *status.Status) interface{} { return GRPCValidationError{Violations: violations} default: - return errors.New(st.Message()) + return fmt.Errorf("%w: %s", st.Err(), st.Message()) } } @@ -60,9 +60,12 @@ func WrapErrorWithStatus(err error, lang string) error { st := status.New(code, msg) if code == codes.InvalidArgument { + //nolint: errorlint if errs, ok := err.(validator.ValidationErrors); ok { var e error - br := &errdetails.BadRequest{} + + br := errdetails.BadRequest{} + for _, ee := range errs { field := getFieldName(ee.Namespace(), ee.Field()) msg := getErrMessage(validationRule(ee.ActualTag()), field, ee.Param(), l) @@ -74,7 +77,7 @@ func WrapErrorWithStatus(err error, lang string) error { br.FieldViolations = append(br.FieldViolations, violation) } - st, e = st.WithDetails(br) + st, e = st.WithDetails(&br) if e != nil { return err } @@ -84,9 +87,17 @@ func WrapErrorWithStatus(err error, lang string) error { return st.Err() } -// getGRPCCode returns grpc error code by first value and its existence by second +// getGRPCCode returns grpc error code by first value and its existence by second. func getGRPCCode(httpCode int) (codes.Code, bool) { c, ok := httpToGRPCCodes[httpCode] + + return c, ok +} + +// getHTTPCode returns http error code by first value and its existence by second. +func getHTTPCode(grpcCode codes.Code) (int, bool) { + c, ok := grpcToHTTPCodes[grpcCode] + return c, ok } @@ -96,6 +107,7 @@ func getViolations(details []interface{}) []*errdetails.BadRequest_FieldViolatio } violations := make([]*errdetails.BadRequest_FieldViolation, 0) + for _, detail := range details { if d, ok := detail.(*errdetails.BadRequest); ok { violations = append(violations, d.FieldViolations...) diff --git a/grpc_internal_test.go b/grpc_internal_test.go new file mode 100644 index 0000000..81b7df0 --- /dev/null +++ b/grpc_internal_test.go @@ -0,0 +1,71 @@ +package ginerrors + +import ( + "net/http" + "testing" + + "github.com/stretchr/testify/assert" + "google.golang.org/grpc/codes" +) + +func Test_getHTTPCode(t *testing.T) { + type tc struct { + name string + exp int + in codes.Code + exists bool + } + + tcs := []tc{ + { + name: "not found", + in: codes.NotFound, + exp: http.StatusNotFound, + exists: true, + }, + } + + t.Parallel() + + for _, tc := range tcs { + tc := tc + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + out, exist := getHTTPCode(tc.in) + assert.Equal(t, tc.exp, out) + assert.Equal(t, tc.exists, exist) + }) + } +} + +func Test_getGRPCCode(t *testing.T) { + type tc struct { + name string + in int + exp codes.Code + exists bool + } + + tcs := []tc{ + { + name: "not found", + in: http.StatusNotFound, + exp: codes.NotFound, + exists: true, + }, + } + + t.Parallel() + + for _, tc := range tcs { + tc := tc + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + out, exist := getGRPCCode(tc.in) + assert.Equal(t, tc.exp, out) + assert.Equal(t, tc.exists, exist) + }) + } +} diff --git a/grpc_test.go b/grpc_test.go index 7fb667e..ab7b5f7 100644 --- a/grpc_test.go +++ b/grpc_test.go @@ -1,16 +1,15 @@ -package ginerrors +package ginerrors_test import ( "database/sql" - "errors" "testing" + ginerrors "github.com/microparts/errors-go-gin" "github.com/stretchr/testify/assert" ) const ( - unknownErrMessage = "rpc error: code = InvalidArgument desc = foo" - unknownErrValue = "foo" + unknownErrMessage = "rpc error: code = InvalidArgument desc = unknown error value" recordNotFountMessage = "rpc error: code = NotFound desc = record not found" unavailableMethodMessage = "rpc error: code = Unavailable desc = method not allowed" ) @@ -24,7 +23,7 @@ func TestWrapErrWithRPC(t *testing.T) { }{ { caseName: "unknown err", - err: errors.New(unknownErrValue), + err: ginerrors.ErrUnknownErrVal, lang: "en", expected: unknownErrMessage, }, @@ -36,14 +35,20 @@ func TestWrapErrWithRPC(t *testing.T) { }, { caseName: "unavailable method", - err: ErrNoMethod, + err: ginerrors.ErrNoMethod, lang: "en", expected: unavailableMethodMessage, }, } + + t.Parallel() + for _, cc := range cases { + cc := cc t.Run(cc.caseName, func(t *testing.T) { - err := WrapErrorWithStatus(cc.err, cc.lang) + t.Parallel() + + err := ginerrors.WrapErrorWithStatus(cc.err, cc.lang) assert.Error(t, err) assert.Equal(t, cc.expected, err.Error()) }) diff --git a/validator.go b/validator.go index e66e9c3..b6cd304 100644 --- a/validator.go +++ b/validator.go @@ -6,16 +6,17 @@ import ( "strings" "github.com/gin-gonic/gin" + "github.com/go-playground/validator/v10" + errs "github.com/spacetab-io/errors-go" "google.golang.org/genproto/googleapis/rpc/errdetails" - "gopkg.in/go-playground/validator.v9" - - errs "github.com/microparts/errors-go" ) -type langName string -type validationRule string -type errorPattern string -type validationErrors map[validationRule]errorPattern +type ( + langName string + validationRule string + errorPattern string + validationErrors map[validationRule]errorPattern +) func (ve errorPattern) string() string { return string(ve) @@ -46,6 +47,7 @@ var ( ErrNoMethod = errors.New("method not allowed") ErrServerError = errors.New("internal server error") ErrRecordNotFound = errors.New("record not found") + ErrUnknownErrVal = errors.New("unknown error value") ) func getLang(c *gin.Context) langName { @@ -57,32 +59,44 @@ func getLang(c *gin.Context) langName { return langName(lang) } -// validationErrors Формирование массива ошибок +// validationErrors Формирование массива ошибок. func makeErrorsSlice(err error, lang langName) map[errs.FieldName][]errs.ValidationError { ve := make(map[errs.FieldName][]errs.ValidationError) - for _, e := range err.(validator.ValidationErrors) { + + //nolint: errorlint + verrs, ok := err.(validator.ValidationErrors) + if !ok { + return nil + } + + for _, e := range verrs { field := getFieldName(e.Namespace(), e.Field()) if _, ok := ve[field]; !ok { ve[field] = make([]errs.ValidationError, 0) } + ve[field] = append( ve[field], getErrMessage(validationRule(e.ActualTag()), field, e.Param(), lang), ) } + return ve } func makeErrorsSliceFromViolations(violations []*errdetails.BadRequest_FieldViolation) map[errs.FieldName][]errs.ValidationError { ve := make(map[errs.FieldName][]errs.ValidationError) + for _, v := range violations { if v == nil { continue } + field := errs.FieldName(v.Field) if _, ok := ve[field]; !ok { ve[field] = make([]errs.ValidationError, 0) } + e := errs.ValidationError(v.Description) ve[field] = append(ve[field], e) } @@ -91,12 +105,12 @@ func makeErrorsSliceFromViolations(violations []*errdetails.BadRequest_FieldViol } func getFieldName(namespace string, field string) errs.FieldName { - namespace = strings.Replace(namespace, "]", "", -1) - namespace = strings.Replace(namespace, "[", ".", -1) + namespace = strings.ReplaceAll(namespace, "]", "") + namespace = strings.ReplaceAll(namespace, "[", ".") namespaceSlice := strings.Split(namespace, ".") fieldName := field - if len(namespaceSlice) > 2 { + if len(namespaceSlice) > 2 { //nolint: gomnd fieldName = strings.Join([]string{strings.Join(namespaceSlice[1:len(namespaceSlice)-1], "."), field}, ".") } @@ -105,8 +119,8 @@ func getFieldName(namespace string, field string) errs.FieldName { func getErrMessage(errorType validationRule, field errs.FieldName, param string, lang langName) errs.ValidationError { errKey := errorType - _, ok := CommonValidationErrors[lang][errorType] - if !ok { + + if _, ok := CommonValidationErrors[lang][errorType]; !ok { errKey = "ek" }