Skip to content

Commit b17ccd8

Browse files
authored
refactor(errors)!: remove grpc from errors (#20402)
1 parent a2dd2a0 commit b17ccd8

6 files changed

Lines changed: 9 additions & 81 deletions

File tree

errors/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ Ref: https://keepachangelog.com/en/1.0.0/
3131

3232
## [Unreleased]
3333

34+
### API Breaking
35+
36+
* [#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
37+
3438
## [v1.0.1](https://github.com/cosmos/cosmos-sdk/releases/tag/errors%2Fv1.0.1)
3539

3640
### Improvements

errors/errors.go

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,12 @@ import (
55
"reflect"
66

77
"github.com/pkg/errors"
8-
grpccodes "google.golang.org/grpc/codes"
9-
grpcstatus "google.golang.org/grpc/status"
108
)
119

1210
// UndefinedCodespace when we explicitly declare no codespace
1311
const UndefinedCodespace = "undefined"
1412

1513
var (
16-
// errInternal should never be exposed, but we reserve this code for non-specified errors
17-
errInternal = Register(UndefinedCodespace, 1, "internal")
1814

1915
// ErrStopIterating is used to break out of an iteration
2016
ErrStopIterating = Register(UndefinedCodespace, 2, "stop iterating")
@@ -32,18 +28,11 @@ var (
3228
//
3329
// Use this function only during a program startup phase.
3430
func Register(codespace string, code uint32, description string) *Error {
35-
return RegisterWithGRPCCode(codespace, code, grpccodes.Unknown, description)
36-
}
37-
38-
// RegisterWithGRPCCode is a version of Register that associates a gRPC error
39-
// code with a registered error.
40-
func RegisterWithGRPCCode(codespace string, code uint32, grpcCode grpccodes.Code, description string) *Error {
41-
// TODO - uniqueness is (codespace, code) combo
4231
if e := getUsed(codespace, code); e != nil {
4332
panic(fmt.Sprintf("error with code %d is already registered: %q", code, e.desc))
4433
}
4534

46-
err := &Error{codespace: codespace, code: code, desc: description, grpcCode: grpcCode}
35+
err := &Error{codespace: codespace, code: code, desc: description}
4736
setUsed(err)
4837

4938
return err
@@ -94,7 +83,6 @@ type Error struct {
9483
codespace string
9584
code uint32
9685
desc string
97-
grpcCode grpccodes.Code
9886
}
9987

10088
// New is an alias for Register.
@@ -154,10 +142,6 @@ func (e *Error) Wrap(desc string) error { return Wrap(e, desc) }
154142
// It's a handy function to call Wrapf with sdk errors.
155143
func (e *Error) Wrapf(desc string, args ...interface{}) error { return Wrapf(e, desc, args...) }
156144

157-
func (e *Error) GRPCStatus() *grpcstatus.Status {
158-
return grpcstatus.Newf(e.grpcCode, "codespace %s code %d: %s", e.codespace, e.code, e.desc)
159-
}
160-
161145
func isNilErr(err error) bool {
162146
// Reflect usage is necessary to correctly compare with
163147
// a nil implementation of an error.
@@ -246,27 +230,6 @@ func (e *wrappedError) Unwrap() error {
246230
return e.parent
247231
}
248232

249-
// GRPCStatus gets the gRPC status from the wrapped error or returns an unknown gRPC status.
250-
func (e *wrappedError) GRPCStatus() *grpcstatus.Status {
251-
w := e.Cause()
252-
for {
253-
if hasStatus, ok := w.(interface {
254-
GRPCStatus() *grpcstatus.Status
255-
}); ok {
256-
status := hasStatus.GRPCStatus()
257-
return grpcstatus.New(status.Code(), fmt.Sprintf("%s: %s", status.Message(), e.msg))
258-
}
259-
260-
x, ok := w.(causer)
261-
if ok {
262-
w = x.Cause()
263-
}
264-
if x == nil {
265-
return grpcstatus.New(grpccodes.Unknown, e.msg)
266-
}
267-
}
268-
}
269-
270233
// Recover captures a panic and stop its propagation. If panic happens it is
271234
// transformed into a ErrPanic instance and assigned to given error. Call this
272235
// function using defer in order to work as expected.

errors/errors_test.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77

88
"github.com/pkg/errors"
99
"github.com/stretchr/testify/suite"
10-
"google.golang.org/grpc/codes"
11-
grpcstatus "google.golang.org/grpc/status"
1210
)
1311

1412
type errorsTestSuite struct {
@@ -209,23 +207,6 @@ func (s *errorsTestSuite) TestABCIError() {
209207
s.Require().Equal("custom: unknown", ABCIError("unknown", 1, "custom").Error())
210208
}
211209

212-
func (s *errorsTestSuite) TestGRPCStatus() {
213-
s.Require().Equal(codes.Unknown, grpcstatus.Code(errInternal))
214-
s.Require().Equal(codes.NotFound, grpcstatus.Code(ErrNotFound))
215-
216-
status, ok := grpcstatus.FromError(ErrNotFound)
217-
s.Require().True(ok)
218-
s.Require().Equal("codespace testtesttest code 38: not found", status.Message())
219-
220-
// test wrapping
221-
s.Require().Equal(codes.Unimplemented, grpcstatus.Code(ErrNotSupported.Wrap("test")))
222-
s.Require().Equal(codes.FailedPrecondition, grpcstatus.Code(ErrConflict.Wrapf("test %s", "foo")))
223-
224-
status, ok = grpcstatus.FromError(ErrNotFound.Wrap("test"))
225-
s.Require().True(ok)
226-
s.Require().Equal("codespace testtesttest code 38: not found: test", status.Message())
227-
}
228-
229210
const testCodespace = "testtesttest"
230211

231212
var (
@@ -254,8 +235,5 @@ var (
254235
ErrUnknownExtensionOptions = Register(testCodespace, 31, "unknown extension options")
255236
ErrPackAny = Register(testCodespace, 33, "failed packing protobuf message to Any")
256237
ErrLogic = Register(testCodespace, 35, "internal logic error")
257-
ErrConflict = RegisterWithGRPCCode(testCodespace, 36, codes.FailedPrecondition, "conflict")
258-
ErrNotSupported = RegisterWithGRPCCode(testCodespace, 37, codes.Unimplemented, "feature not supported")
259-
ErrNotFound = RegisterWithGRPCCode(testCodespace, 38, codes.NotFound, "not found")
260238
ErrIO = Register(testCodespace, 39, "Internal IO error")
261239
)

errors/go.mod

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,13 @@ go 1.20
55
require (
66
github.com/pkg/errors v0.9.1
77
github.com/stretchr/testify v1.9.0
8-
google.golang.org/grpc v1.64.0
98
)
109

1110
require (
1211
github.com/davecgh/go-spew v1.1.1 // indirect
1312
github.com/kr/pretty v0.3.0 // indirect
1413
github.com/pmezard/go-difflib v1.0.0 // indirect
1514
github.com/rogpeppe/go-internal v1.8.1 // indirect
16-
golang.org/x/net v0.25.0 // indirect
17-
golang.org/x/sys v0.20.0 // indirect
18-
google.golang.org/genproto/googleapis/rpc v0.0.0-20240513163218-0867130af1f8 // indirect
19-
google.golang.org/protobuf v1.34.1 // indirect
2015
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
2116
gopkg.in/yaml.v3 v3.0.1 // indirect
2217
)

errors/go.sum

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
22
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
33
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4-
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
54
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
65
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
76
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
@@ -20,17 +19,6 @@ github.com/rogpeppe/go-internal v1.8.1 h1:geMPLpDpQOgVyCg5z5GoRwLHepNdb71NXb67XF
2019
github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o=
2120
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
2221
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
23-
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
24-
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
25-
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
26-
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
27-
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
28-
google.golang.org/genproto/googleapis/rpc v0.0.0-20240513163218-0867130af1f8 h1:mxSlqyb8ZAHsYDCfiXN1EDdNTdvjUJSLY+OnAUtYNYA=
29-
google.golang.org/genproto/googleapis/rpc v0.0.0-20240513163218-0867130af1f8/go.mod h1:I7Y+G38R2bu5j1aLzfFmQfTcU/WnFuqDwLZAbvKTKpM=
30-
google.golang.org/grpc v1.64.0 h1:KH3VH9y/MgNQg1dE7b3XfVK0GsPSIzJwdF617gUSbvY=
31-
google.golang.org/grpc v1.64.0/go.mod h1:oxjF8E3FBnjp+/gVFYdWacaLDx9na1aqy9oovLpxQYg=
32-
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
33-
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
3422
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3523
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3624
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=

simapp/gomod2nix.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ schema = 3
171171
version = "v1.6.2"
172172
hash = "sha256-X7aNKLKZ7pJBG/wdP+TWuQnlNLNdbUDd+kC5kF4uBtU="
173173
[mod."github.com/fatih/color"]
174-
version = "v1.16.0"
175-
hash = "sha256-Aq/SM28aPJVzvapllQ64R/DM4aZ5CHPewcm/AUJPyJQ="
174+
version = "v1.17.0"
175+
hash = "sha256-QsKMy3MsvjbYNcA9jP8w6c3wpmWDZ0079bybAEzmXR0="
176176
[mod."github.com/felixge/httpsnoop"]
177177
version = "v1.0.4"
178178
hash = "sha256-c1JKoRSndwwOyOxq9ddCe+8qn7mG9uRq2o/822x5O/c="
@@ -282,8 +282,8 @@ schema = 3
282282
version = "v0.5.3"
283283
hash = "sha256-5jQftEvEhL88yWeVnu+IZKzV5p9osZcgFmwP1zlrjzY="
284284
[mod."github.com/hashicorp/go-plugin"]
285-
version = "v1.6.0"
286-
hash = "sha256-NeY86Z+qJwt0NPV4cNmWDiTryDPSiyKMkS1ivRX9ThE="
285+
version = "v1.6.1"
286+
hash = "sha256-HEeJ8TV67PcAuUnGCOHphFpZ/BShvJo5B6Obu3P7t8M="
287287
[mod."github.com/hashicorp/go-safetemp"]
288288
version = "v1.0.0"
289289
hash = "sha256-g5i9m7FSRInQzZ4iRpIsoUu685AY7fppUwjhuZCezT8="

0 commit comments

Comments
 (0)