Skip to content

Commit 8cd8832

Browse files
authored
Wrap errors returned from Store (cosmos#404)
1 parent aaaa20d commit 8cd8832

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed

rpc/json/service_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"crypto/rand"
77
"encoding/json"
8+
"fmt"
89
"net/http"
910
"net/http/httptest"
1011
"net/url"
@@ -70,7 +71,7 @@ func TestREST(t *testing.T) {
7071
{"invalid/missing param", "/block", http.StatusOK, int(json2.E_INVALID_REQ), `missing param 'height'`},
7172
{"valid/no params", "/abci_info", http.StatusOK, -1, `"last_block_height":"345"`},
7273
// to keep test simple, allow returning application error in following case
73-
{"valid/int param", "/block?height=321", http.StatusOK, int(json2.E_INTERNAL), `"key not found"`},
74+
{"valid/int param", "/block?height=321", http.StatusOK, int(json2.E_INTERNAL), "failed to load hash from index"},
7475
{"invalid/int param", "/block?height=foo", http.StatusOK, int(json2.E_PARSE), "failed to parse param 'height'"},
7576
{"valid/bool int string params",
7677
"/tx_search?" + txSearchParams.Encode(),
@@ -95,6 +96,7 @@ func TestREST(t *testing.T) {
9596
assert.Equal(c.httpCode, resp.Code)
9697
s := resp.Body.String()
9798
assert.NotEmpty(s)
99+
fmt.Print(s)
98100
assert.Contains(s, c.bodyContains)
99101
var jsonResp response
100102
assert.NoError(json.Unmarshal([]byte(s), &jsonResp))

store/store.go

+24-18
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ func (s *DefaultStore) SaveBlock(block *types.Block, commit *types.Commit) error
5959
hash := block.Header.Hash()
6060
blockBlob, err := block.MarshalBinary()
6161
if err != nil {
62-
return err
62+
return fmt.Errorf("failed to marshal Block to binary: %w", err)
6363
}
6464

6565
commitBlob, err := commit.MarshalBinary()
6666
if err != nil {
67-
return err
67+
return fmt.Errorf("failed to marshal Commit to binary: %w", err)
6868
}
6969

7070
bb := s.db.NewBatch()
@@ -78,7 +78,7 @@ func (s *DefaultStore) SaveBlock(block *types.Block, commit *types.Commit) error
7878
}
7979

8080
if err = bb.Commit(); err != nil {
81-
return err
81+
return fmt.Errorf("failed to commit transaction: %w", err)
8282
}
8383

8484
return nil
@@ -91,7 +91,7 @@ func (s *DefaultStore) SaveBlock(block *types.Block, commit *types.Commit) error
9191
func (s *DefaultStore) LoadBlock(height uint64) (*types.Block, error) {
9292
h, err := s.loadHashFromIndex(height)
9393
if err != nil {
94-
return nil, err
94+
return nil, fmt.Errorf("failed to load hash from index: %w", err)
9595
}
9696
return s.LoadBlockByHash(h)
9797
}
@@ -115,7 +115,7 @@ func (s *DefaultStore) LoadBlockByHash(hash [32]byte) (*types.Block, error) {
115115
func (s *DefaultStore) SaveBlockResponses(height uint64, responses *tmstate.ABCIResponses) error {
116116
data, err := responses.Marshal()
117117
if err != nil {
118-
return err
118+
return fmt.Errorf("failed to marshal response: %w", err)
119119
}
120120
return s.db.Set(getResponsesKey(height), data)
121121
}
@@ -124,18 +124,21 @@ func (s *DefaultStore) SaveBlockResponses(height uint64, responses *tmstate.ABCI
124124
func (s *DefaultStore) LoadBlockResponses(height uint64) (*tmstate.ABCIResponses, error) {
125125
data, err := s.db.Get(getResponsesKey(height))
126126
if err != nil {
127-
return nil, err
127+
return nil, fmt.Errorf("failed to retrieve block results from height %v: %w", height, err)
128128
}
129129
var responses tmstate.ABCIResponses
130130
err = responses.Unmarshal(data)
131-
return &responses, err
131+
if err != nil {
132+
return &responses, fmt.Errorf("failed to unmarshal data: %w", err)
133+
}
134+
return &responses, nil
132135
}
133136

134137
// LoadCommit returns commit for a block at given height, or error if it's not found in Store.
135138
func (s *DefaultStore) LoadCommit(height uint64) (*types.Commit, error) {
136139
hash, err := s.loadHashFromIndex(height)
137140
if err != nil {
138-
return nil, err
141+
return nil, fmt.Errorf("failed to load hash from index: %w", err)
139142
}
140143
return s.LoadCommitByHash(hash)
141144
}
@@ -144,19 +147,22 @@ func (s *DefaultStore) LoadCommit(height uint64) (*types.Commit, error) {
144147
func (s *DefaultStore) LoadCommitByHash(hash [32]byte) (*types.Commit, error) {
145148
commitData, err := s.db.Get(getCommitKey(hash))
146149
if err != nil {
147-
return nil, err
150+
return nil, fmt.Errorf("failed to retrieve commit from hash %v: %w", hash, err)
148151
}
149152
commit := new(types.Commit)
150153
err = commit.UnmarshalBinary(commitData)
151-
return commit, err
154+
if err != nil {
155+
return nil, fmt.Errorf("failed to marshal Commit into object: %w", err)
156+
}
157+
return commit, nil
152158
}
153159

154160
// UpdateState updates state saved in Store. Only one State is stored.
155161
// If there is no State in Store, state will be saved.
156162
func (s *DefaultStore) UpdateState(state types.State) error {
157163
pbState, err := state.ToProto()
158164
if err != nil {
159-
return err
165+
return fmt.Errorf("failed to marshal state to JSON: %w", err)
160166
}
161167
data, err := pbState.Marshal()
162168
if err != nil {
@@ -169,12 +175,12 @@ func (s *DefaultStore) UpdateState(state types.State) error {
169175
func (s *DefaultStore) LoadState() (types.State, error) {
170176
blob, err := s.db.Get(getStateKey())
171177
if err != nil {
172-
return types.State{}, err
178+
return types.State{}, fmt.Errorf("failed to retrieve state: %w", err)
173179
}
174180
var pbState pb.State
175181
err = pbState.Unmarshal(blob)
176182
if err != nil {
177-
return types.State{}, err
183+
return types.State{}, fmt.Errorf("failed to unmarshal state from JSON: %w", err)
178184
}
179185

180186
var state types.State
@@ -186,11 +192,11 @@ func (s *DefaultStore) LoadState() (types.State, error) {
186192
func (s *DefaultStore) SaveValidators(height uint64, validatorSet *tmtypes.ValidatorSet) error {
187193
pbValSet, err := validatorSet.ToProto()
188194
if err != nil {
189-
return err
195+
return fmt.Errorf("failed to marshal ValidatorSet to protobuf: %w", err)
190196
}
191197
blob, err := pbValSet.Marshal()
192198
if err != nil {
193-
return err
199+
return fmt.Errorf("failed to marshal ValidatorSet: %w", err)
194200
}
195201

196202
return s.db.Set(getValidatorsKey(height), blob)
@@ -199,12 +205,12 @@ func (s *DefaultStore) SaveValidators(height uint64, validatorSet *tmtypes.Valid
199205
func (s *DefaultStore) LoadValidators(height uint64) (*tmtypes.ValidatorSet, error) {
200206
blob, err := s.db.Get(getValidatorsKey(height))
201207
if err != nil {
202-
return nil, err
208+
return nil, fmt.Errorf("failed to load Validators for height %v: %w", height, err)
203209
}
204210
var pbValSet tmproto.ValidatorSet
205211
err = pbValSet.Unmarshal(blob)
206212
if err != nil {
207-
return nil, err
213+
return nil, fmt.Errorf("failed to unmarshal to protobuf: %w", err)
208214
}
209215

210216
return tmtypes.ValidatorSetFromProto(&pbValSet)
@@ -215,7 +221,7 @@ func (s *DefaultStore) loadHashFromIndex(height uint64) ([32]byte, error) {
215221

216222
var hash [32]byte
217223
if err != nil {
218-
return hash, err
224+
return hash, fmt.Errorf("failed to load block hash for height %v: %w", height, err)
219225
}
220226
if len(blob) != len(hash) {
221227
return hash, errors.New("invalid hash length")

0 commit comments

Comments
 (0)