Skip to content

Commit dd4f9e6

Browse files
committed
Refactor error handling in shard.go to use a consistent setError function for ABCI responses.
1 parent 736c6c9 commit dd4f9e6

File tree

1 file changed

+55
-55
lines changed

1 file changed

+55
-55
lines changed

core/node/metadata/shard.go

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,9 @@ import (
3434
)
3535

3636
const (
37-
defaultValidatorPower int64 = 1
38-
chainIDPrefix = "metadata-shard-"
39-
codeEncodingError uint32 = 1
40-
codeValidationError uint32 = 2
41-
codeStorageError uint32 = 3
42-
codeNotFoundError uint32 = 4
37+
defaultValidatorPower int64 = 1
38+
chainIDPrefix = "metadata-shard-"
39+
codeSpaceRiver = "towns-protocol"
4340
)
4441

4542
var _ abci.Application = (*MetadataShard)(nil)
@@ -206,17 +203,28 @@ func decodeMetadataTx(txBytes []byte) (*MetadataTx, error) {
206203
return nil, RiverError(Err_INVALID_ARGUMENT, "unable to decode metadata tx")
207204
}
208205

209-
func abciCodeFromError(err error) uint32 {
206+
type abciErrorResponder interface {
207+
*abci.CheckTxResponse | *abci.ExecTxResult | *abci.QueryResponse
208+
}
209+
210+
func setError[T abciErrorResponder](resp T, err error) {
211+
if err == nil {
212+
return
213+
}
210214
riverErr := AsRiverError(err)
211-
switch riverErr.Code {
212-
case Err_NOT_FOUND:
213-
return codeNotFoundError
214-
case Err_INVALID_ARGUMENT:
215-
return codeValidationError
216-
case Err_ALREADY_EXISTS, Err_FAILED_PRECONDITION:
217-
return codeValidationError
218-
default:
219-
return codeStorageError
215+
switch r := any(resp).(type) {
216+
case *abci.CheckTxResponse:
217+
r.Code = uint32(riverErr.Code)
218+
r.Log = riverErr.Error()
219+
r.Codespace = codeSpaceRiver
220+
case *abci.ExecTxResult:
221+
r.Code = uint32(riverErr.Code)
222+
r.Log = riverErr.Error()
223+
r.Codespace = codeSpaceRiver
224+
case *abci.QueryResponse:
225+
r.Code = uint32(riverErr.Code)
226+
r.Log = riverErr.Error()
227+
r.Codespace = codeSpaceRiver
220228
}
221229
}
222230

@@ -228,7 +236,7 @@ func parsePagination(u *url.URL) (int64, int32, error) {
228236
if rawOffset := values.Get("offset"); rawOffset != "" {
229237
val, err := strconv.ParseInt(rawOffset, 10, 64)
230238
if err != nil {
231-
return 0, 0, fmt.Errorf("invalid offset: %w", err)
239+
return 0, 0, RiverError(Err_INVALID_ARGUMENT, "invalid offset", "err", err)
232240
}
233241
if val < 0 {
234242
return 0, 0, RiverError(Err_INVALID_ARGUMENT, "offset must be >= 0")
@@ -239,7 +247,7 @@ func parsePagination(u *url.URL) (int64, int32, error) {
239247
if rawLimit := values.Get("limit"); rawLimit != "" {
240248
val, err := strconv.ParseInt(rawLimit, 10, 32)
241249
if err != nil {
242-
return 0, 0, fmt.Errorf("invalid limit: %w", err)
250+
return 0, 0, RiverError(Err_INVALID_ARGUMENT, "invalid limit", "err", err)
243251
}
244252
if val > 0 {
245253
limit = int32(val)
@@ -265,14 +273,17 @@ func (m *MetadataShard) Info(ctx context.Context, _ *abci.InfoRequest) (*abci.In
265273
}
266274

267275
func (m *MetadataShard) CheckTx(ctx context.Context, req *abci.CheckTxRequest) (*abci.CheckTxResponse, error) {
276+
resp := &abci.CheckTxResponse{Code: abci.CodeTypeOK}
268277
metaTx, err := decodeMetadataTx(req.Tx)
269278
if err != nil {
270-
return &abci.CheckTxResponse{Code: codeEncodingError, Log: err.Error()}, nil
279+
setError(resp, err)
280+
return resp, nil
271281
}
272282
if err := m.validateTx(metaTx); err != nil {
273-
return &abci.CheckTxResponse{Code: codeValidationError, Log: err.Error()}, nil
283+
setError(resp, err)
284+
return resp, nil
274285
}
275-
return &abci.CheckTxResponse{Code: abci.CodeTypeOK}, nil
286+
return resp, nil
276287
}
277288

278289
func (m *MetadataShard) Commit(ctx context.Context, _ *abci.CommitRequest) (*abci.CommitResponse, error) {
@@ -304,8 +315,7 @@ func (m *MetadataShard) Query(ctx context.Context, req *abci.QueryRequest) (*abc
304315

305316
parsedPath, err := url.Parse(req.Path)
306317
if err != nil {
307-
resp.Code = codeValidationError
308-
resp.Log = fmt.Sprintf("invalid path: %v", err)
318+
setError(resp, RiverError(Err_INVALID_ARGUMENT, "invalid path", "err", err))
309319
return resp, nil
310320
}
311321

@@ -317,14 +327,12 @@ func (m *MetadataShard) Query(ctx context.Context, req *abci.QueryRequest) (*abc
317327
}
318328
streamID, err := shared.StreamIdFromString(streamHex)
319329
if err != nil {
320-
resp.Code = codeValidationError
321-
resp.Log = err.Error()
330+
setError(resp, err)
322331
return resp, nil
323332
}
324333
record, err := m.store.GetStream(ctx, m.opts.ShardID, streamID)
325334
if err != nil {
326-
resp.Code = abciCodeFromError(err)
327-
resp.Log = err.Error()
335+
setError(resp, err)
328336
return resp, nil
329337
}
330338
payload, err := protojson.Marshal(record)
@@ -335,20 +343,17 @@ func (m *MetadataShard) Query(ctx context.Context, req *abci.QueryRequest) (*abc
335343
case parsedPath.Path == "/streams":
336344
offset, limit, err := parsePagination(parsedPath)
337345
if err != nil {
338-
resp.Code = codeValidationError
339-
resp.Log = err.Error()
346+
setError(resp, err)
340347
return resp, nil
341348
}
342349
streams, err := m.store.ListStreams(ctx, m.opts.ShardID, offset, limit)
343350
if err != nil {
344-
resp.Code = abciCodeFromError(err)
345-
resp.Log = err.Error()
351+
setError(resp, err)
346352
return resp, nil
347353
}
348354
count, err := m.store.CountStreams(ctx, m.opts.ShardID)
349355
if err != nil {
350-
resp.Code = abciCodeFromError(err)
351-
resp.Log = err.Error()
356+
setError(resp, err)
352357
return resp, nil
353358
}
354359
payload, err := json.Marshal(struct {
@@ -372,27 +377,23 @@ func (m *MetadataShard) Query(ctx context.Context, req *abci.QueryRequest) (*abc
372377
addrHex = addrHex[2:]
373378
}
374379
if len(addrHex) != 40 {
375-
resp.Code = codeValidationError
376-
resp.Log = "node address must be 20 bytes hex"
380+
setError(resp, RiverError(Err_INVALID_ARGUMENT, "node address must be 20 bytes hex"))
377381
return resp, nil
378382
}
379383
nodeAddr := common.HexToAddress(addrHex)
380384
offset, limit, err := parsePagination(parsedPath)
381385
if err != nil {
382-
resp.Code = codeValidationError
383-
resp.Log = err.Error()
386+
setError(resp, err)
384387
return resp, nil
385388
}
386389
streams, err := m.store.ListStreamsByNode(ctx, m.opts.ShardID, nodeAddr, offset, limit)
387390
if err != nil {
388-
resp.Code = abciCodeFromError(err)
389-
resp.Log = err.Error()
391+
setError(resp, err)
390392
return resp, nil
391393
}
392394
count, err := m.store.CountStreamsByNode(ctx, m.opts.ShardID, nodeAddr)
393395
if err != nil {
394-
resp.Code = abciCodeFromError(err)
395-
resp.Log = err.Error()
396+
setError(resp, err)
396397
return resp, nil
397398
}
398399
payload, err := json.Marshal(struct {
@@ -415,8 +416,7 @@ func (m *MetadataShard) Query(ctx context.Context, req *abci.QueryRequest) (*abc
415416
case parsedPath.Path == "/streams/count":
416417
count, err := m.store.CountStreams(ctx, m.opts.ShardID)
417418
if err != nil {
418-
resp.Code = abciCodeFromError(err)
419-
resp.Log = err.Error()
419+
setError(resp, err)
420420
return resp, nil
421421
}
422422
payload, err := json.Marshal(struct {
@@ -432,15 +432,13 @@ func (m *MetadataShard) Query(ctx context.Context, req *abci.QueryRequest) (*abc
432432
addrHex = addrHex[2:]
433433
}
434434
if len(addrHex) != 40 {
435-
resp.Code = codeValidationError
436-
resp.Log = "node address must be 20 bytes hex"
435+
setError(resp, RiverError(Err_INVALID_ARGUMENT, "node address must be 20 bytes hex"))
437436
return resp, nil
438437
}
439438
nodeAddr := common.HexToAddress(addrHex)
440439
count, err := m.store.CountStreamsByNode(ctx, m.opts.ShardID, nodeAddr)
441440
if err != nil {
442-
resp.Code = abciCodeFromError(err)
443-
resp.Log = err.Error()
441+
setError(resp, err)
444442
return resp, nil
445443
}
446444
payload, err := json.Marshal(struct {
@@ -455,8 +453,7 @@ func (m *MetadataShard) Query(ctx context.Context, req *abci.QueryRequest) (*abc
455453
}
456454
resp.Value = payload
457455
default:
458-
resp.Code = codeValidationError
459-
resp.Log = fmt.Sprintf("unsupported query path %q", req.Path)
456+
setError(resp, RiverError(Err_INVALID_ARGUMENT, "unsupported query path", "path", req.Path))
460457
}
461458
return resp, nil
462459
}
@@ -561,18 +558,21 @@ func (m *MetadataShard) FinalizeBlock(
561558
for i, txBytes := range req.Txs {
562559
metaTx, err := decodeMetadataTx(txBytes)
563560
if err != nil {
564-
txs[i] = &abci.ExecTxResult{Code: codeEncodingError, Log: err.Error()}
561+
res := &abci.ExecTxResult{}
562+
setError(res, err)
563+
txs[i] = res
565564
continue
566565
}
567566
if err := m.validateTx(metaTx); err != nil {
568-
txs[i] = &abci.ExecTxResult{Code: codeValidationError, Log: err.Error()}
567+
res := &abci.ExecTxResult{}
568+
setError(res, err)
569+
txs[i] = res
569570
continue
570571
}
571572
if err := m.store.ApplyMetadataTx(ctx, m.opts.ShardID, req.Height, metaTx); err != nil {
572-
txs[i] = &abci.ExecTxResult{
573-
Code: abciCodeFromError(err),
574-
Log: err.Error(),
575-
}
573+
res := &abci.ExecTxResult{}
574+
setError(res, err)
575+
txs[i] = res
576576
continue
577577
}
578578
txs[i] = &abci.ExecTxResult{Code: abci.CodeTypeOK}

0 commit comments

Comments
 (0)