Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add close body after use and fix lint #22248

Merged
merged 5 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions collections/quad.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ type quadKeyCodec[K1, K2, K3, K4 any] struct {

type jsonQuadKey [4]json.RawMessage


// EncodeJSON encodes Quads to json
func (t quadKeyCodec[K1, K2, K3, K4]) EncodeJSON(value Quad[K1, K2, K3, K4]) ([]byte, error) {
json1, err := t.keyCodec1.EncodeJSON(*value.k1)
Expand Down Expand Up @@ -210,7 +209,6 @@ func (t quadKeyCodec[K1, K2, K3, K4]) KeyType() string {
return fmt.Sprintf("Quad[%s,%s,%s,%s]", t.keyCodec1.KeyType(), t.keyCodec2.KeyType(), t.keyCodec3.KeyType(), t.keyCodec4.KeyType())
}


func (t quadKeyCodec[K1, K2, K3, K4]) Encode(buffer []byte, key Quad[K1, K2, K3, K4]) (int, error) {
writtenTotal := 0
if key.k1 != nil {
Expand Down Expand Up @@ -244,7 +242,6 @@ func (t quadKeyCodec[K1, K2, K3, K4]) Encode(buffer []byte, key Quad[K1, K2, K3,
return writtenTotal, nil
}


func (t quadKeyCodec[K1, K2, K3, K4]) Decode(buffer []byte) (int, Quad[K1, K2, K3, K4], error) {
readTotal := 0
read, key1, err := t.keyCodec1.DecodeNonTerminal(buffer)
Expand Down
2 changes: 1 addition & 1 deletion schema/indexer/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func StartIndexing(opts IndexingOptions) (IndexingTarget, error) {

targetCfg.Config, err = unmarshalIndexerCustomConfig(targetCfg.Config, init.ConfigType)
if err != nil {
return IndexingTarget{}, fmt.Errorf("failed to unmarshal indexer config for target %q: %v", targetName, err)
return IndexingTarget{}, fmt.Errorf("failed to unmarshal indexer config for target %q: %w", targetName, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Ensure consistent error wrapping by using %w in all fmt.Errorf calls

The following fmt.Errorf instances in schema/indexer/start.go do not use the %w verb and should be updated to enhance error handling consistency:

  • return IndexingTarget{}, fmt.Errorf("indexer type %q not found", targetCfg.Type)
  • return IndexingTarget{}, fmt.Errorf("indexer filter options are not supported yet")
  • return nil, fmt.Errorf("can't convert %T to %T", cfg, IndexingConfig{})

Recommended Action:

Update these instances to utilize the %w verb for error wrapping, ensuring that the original error is preserved and can be unwrapped later. For example:

return IndexingTarget{}, fmt.Errorf("indexer type %q not found: %w", targetCfg.Type, err)

This change aligns with Go's error handling best practices and improves the ability to trace error chains.

🔗 Analysis chain

Improved error wrapping using %w verb

The change from %v to %w in the error formatting is a good improvement. This modification allows for better error handling and debugging capabilities.

Reasons for approval:

  1. Using %w verb wraps the original error, preserving the error chain.
  2. This change aligns with Go 1.13+ error wrapping best practices.
  3. It follows the Uber Go Style Guide recommendation for error wrapping.

Suggestion for further improvement:
Consider updating other error handling instances in this file to use %w where appropriate, for consistency. This would involve checking other fmt.Errorf calls within the StartIndexing function and potentially other functions in this file.

To ensure consistency across the file, let's check for other instances of error formatting:

This will help identify if there are other error formatting calls that might benefit from using %w.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for other fmt.Errorf calls in the file
rg --type go 'fmt\.Errorf\(' schema/indexer/start.go

Length of output: 411

hoank101 marked this conversation as resolved.
Show resolved Hide resolved
}

initRes, err := init.InitFunc(InitParams{
Expand Down
2 changes: 1 addition & 1 deletion server/v2/cometbft/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func NewConsensus[T transaction.Tx](
indexedEvents: indexedEvents,
initialHeight: 0,
queryHandlersMap: queryHandlersMap,
getProtoRegistry: sync.OnceValues(func() (*protoregistry.Files, error) { return gogoproto.MergedRegistry() }),
getProtoRegistry: sync.OnceValues(gogoproto.MergedRegistry),
}
}

Expand Down
18 changes: 0 additions & 18 deletions tests/integration/tx/aminojson/aminojson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package aminojson

import (
"bytes"
"encoding/json"
"fmt"
stdmath "math"
"testing"
Expand Down Expand Up @@ -454,20 +453,3 @@ func postFixPulsarMessage(msg proto.Message) {
}
}
}

// sortJson sorts the JSON bytes by way of the side effect of unmarshalling and remarshalling the JSON
// using encoding/json. This hacky way of sorting JSON fields was used by the legacy amino JSON encoding in
// x/auth/migrations/legacytx.StdSignBytes. It is used here ensure the x/tx JSON encoding is equivalent to
// the legacy amino JSON encoding.
func sortJson(bz []byte) ([]byte, error) {
var c any
err := json.Unmarshal(bz, &c)
if err != nil {
return nil, err
}
js, err := json.Marshal(c)
if err != nil {
return nil, err
}
return js, nil
}
5 changes: 4 additions & 1 deletion tools/hubl/internal/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ func GetChainRegistryEntry(chain string) (*ChainRegistryEntry, error) {
if err != nil {
return nil, err
}

bz, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}

if err = res.Body.Close(); err != nil {
return nil, err
}

data := &ChainRegistryEntry{}
if err = json.Unmarshal(bz, data); err != nil {
return nil, err
Expand Down
Loading