refactor(errors)!: remove grpc from errors (#20402)

This commit is contained in:
Marko
2024-05-21 16:47:20 +00:00
committed by GitHub
parent a2dd2a0923
commit b17ccd8fdc
6 changed files with 9 additions and 81 deletions
+4
View File
@@ -31,6 +31,10 @@ Ref: https://keepachangelog.com/en/1.0.0/
## [Unreleased]
### API Breaking
* [#20402](https://github.com/cosmos/cosmos-sdk/pull/20402) Remove Grpc error codes from the error package. This is done in order to keep the dependency graph of errors minimal
## [v1.0.1](https://github.com/cosmos/cosmos-sdk/releases/tag/errors%2Fv1.0.1)
### Improvements
+1 -38
View File
@@ -5,16 +5,12 @@ import (
"reflect"
"github.com/pkg/errors"
grpccodes "google.golang.org/grpc/codes"
grpcstatus "google.golang.org/grpc/status"
)
// UndefinedCodespace when we explicitly declare no codespace
const UndefinedCodespace = "undefined"
var (
// errInternal should never be exposed, but we reserve this code for non-specified errors
errInternal = Register(UndefinedCodespace, 1, "internal")
// ErrStopIterating is used to break out of an iteration
ErrStopIterating = Register(UndefinedCodespace, 2, "stop iterating")
@@ -32,18 +28,11 @@ var (
//
// Use this function only during a program startup phase.
func Register(codespace string, code uint32, description string) *Error {
return RegisterWithGRPCCode(codespace, code, grpccodes.Unknown, description)
}
// RegisterWithGRPCCode is a version of Register that associates a gRPC error
// code with a registered error.
func RegisterWithGRPCCode(codespace string, code uint32, grpcCode grpccodes.Code, description string) *Error {
// TODO - uniqueness is (codespace, code) combo
if e := getUsed(codespace, code); e != nil {
panic(fmt.Sprintf("error with code %d is already registered: %q", code, e.desc))
}
err := &Error{codespace: codespace, code: code, desc: description, grpcCode: grpcCode}
err := &Error{codespace: codespace, code: code, desc: description}
setUsed(err)
return err
@@ -94,7 +83,6 @@ type Error struct {
codespace string
code uint32
desc string
grpcCode grpccodes.Code
}
// New is an alias for Register.
@@ -154,10 +142,6 @@ func (e *Error) Wrap(desc string) error { return Wrap(e, desc) }
// It's a handy function to call Wrapf with sdk errors.
func (e *Error) Wrapf(desc string, args ...interface{}) error { return Wrapf(e, desc, args...) }
func (e *Error) GRPCStatus() *grpcstatus.Status {
return grpcstatus.Newf(e.grpcCode, "codespace %s code %d: %s", e.codespace, e.code, e.desc)
}
func isNilErr(err error) bool {
// Reflect usage is necessary to correctly compare with
// a nil implementation of an error.
@@ -246,27 +230,6 @@ func (e *wrappedError) Unwrap() error {
return e.parent
}
// GRPCStatus gets the gRPC status from the wrapped error or returns an unknown gRPC status.
func (e *wrappedError) GRPCStatus() *grpcstatus.Status {
w := e.Cause()
for {
if hasStatus, ok := w.(interface {
GRPCStatus() *grpcstatus.Status
}); ok {
status := hasStatus.GRPCStatus()
return grpcstatus.New(status.Code(), fmt.Sprintf("%s: %s", status.Message(), e.msg))
}
x, ok := w.(causer)
if ok {
w = x.Cause()
}
if x == nil {
return grpcstatus.New(grpccodes.Unknown, e.msg)
}
}
}
// Recover captures a panic and stop its propagation. If panic happens it is
// transformed into a ErrPanic instance and assigned to given error. Call this
// function using defer in order to work as expected.
-22
View File
@@ -7,8 +7,6 @@ import (
"github.com/pkg/errors"
"github.com/stretchr/testify/suite"
"google.golang.org/grpc/codes"
grpcstatus "google.golang.org/grpc/status"
)
type errorsTestSuite struct {
@@ -209,23 +207,6 @@ func (s *errorsTestSuite) TestABCIError() {
s.Require().Equal("custom: unknown", ABCIError("unknown", 1, "custom").Error())
}
func (s *errorsTestSuite) TestGRPCStatus() {
s.Require().Equal(codes.Unknown, grpcstatus.Code(errInternal))
s.Require().Equal(codes.NotFound, grpcstatus.Code(ErrNotFound))
status, ok := grpcstatus.FromError(ErrNotFound)
s.Require().True(ok)
s.Require().Equal("codespace testtesttest code 38: not found", status.Message())
// test wrapping
s.Require().Equal(codes.Unimplemented, grpcstatus.Code(ErrNotSupported.Wrap("test")))
s.Require().Equal(codes.FailedPrecondition, grpcstatus.Code(ErrConflict.Wrapf("test %s", "foo")))
status, ok = grpcstatus.FromError(ErrNotFound.Wrap("test"))
s.Require().True(ok)
s.Require().Equal("codespace testtesttest code 38: not found: test", status.Message())
}
const testCodespace = "testtesttest"
var (
@@ -254,8 +235,5 @@ var (
ErrUnknownExtensionOptions = Register(testCodespace, 31, "unknown extension options")
ErrPackAny = Register(testCodespace, 33, "failed packing protobuf message to Any")
ErrLogic = Register(testCodespace, 35, "internal logic error")
ErrConflict = RegisterWithGRPCCode(testCodespace, 36, codes.FailedPrecondition, "conflict")
ErrNotSupported = RegisterWithGRPCCode(testCodespace, 37, codes.Unimplemented, "feature not supported")
ErrNotFound = RegisterWithGRPCCode(testCodespace, 38, codes.NotFound, "not found")
ErrIO = Register(testCodespace, 39, "Internal IO error")
)
-5
View File
@@ -5,7 +5,6 @@ go 1.20
require (
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.9.0
google.golang.org/grpc v1.64.0
)
require (
@@ -13,10 +12,6 @@ require (
github.com/kr/pretty v0.3.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.8.1 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240513163218-0867130af1f8 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
-12
View File
@@ -1,7 +1,6 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
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/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
@@ -20,17 +19,6 @@ github.com/rogpeppe/go-internal v1.8.1 h1:geMPLpDpQOgVyCg5z5GoRwLHepNdb71NXb67XF
github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240513163218-0867130af1f8 h1:mxSlqyb8ZAHsYDCfiXN1EDdNTdvjUJSLY+OnAUtYNYA=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240513163218-0867130af1f8/go.mod h1:I7Y+G38R2bu5j1aLzfFmQfTcU/WnFuqDwLZAbvKTKpM=
google.golang.org/grpc v1.64.0 h1:KH3VH9y/MgNQg1dE7b3XfVK0GsPSIzJwdF617gUSbvY=
google.golang.org/grpc v1.64.0/go.mod h1:oxjF8E3FBnjp+/gVFYdWacaLDx9na1aqy9oovLpxQYg=
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=