Skip to content

Commit

Permalink
fix(RegisterManager): handle error correctly (smartcontractkit#14183)
Browse files Browse the repository at this point in the history
Currently, when something goes wrong in `tx.CreateManager` such as invalid sql, the error returned is ignored. We should handle the error and return accordingly.

Co-authored-by: Ivaylo Novakov <[email protected]>
  • Loading branch information
graham-chainlink and ro-tex authored Aug 22, 2024
1 parent 2900a1e commit 35f68c8
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/smart-pumas-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"chainlink": patch
---

#bugfix Fix incorrect error handling when registering a new feed manager
5 changes: 4 additions & 1 deletion core/services/feeds/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func (s *service) RegisterManager(ctx context.Context, params RegisterManagerPar
var txerr error

id, txerr = tx.CreateManager(ctx, &mgr)
if err != nil {
if txerr != nil {
return txerr
}

Expand All @@ -219,6 +219,9 @@ func (s *service) RegisterManager(ctx context.Context, params RegisterManagerPar

return nil
})
if err != nil {
return 0, err
}

privkey, err := s.getCSAPrivateKey()
if err != nil {
Expand Down
46 changes: 46 additions & 0 deletions core/services/feeds/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,52 @@ func Test_Service_RegisterManager(t *testing.T) {
assert.Equal(t, actual, id)
}

func Test_Service_RegisterManager_InvalidCreateManager(t *testing.T) {
t.Parallel()

var (
id = int64(1)
pubKeyHex = "0f17c3bf72de8beef6e2d17a14c0a972f5d7e0e66e70722373f12b88382d40f9"
)

var pubKey crypto.PublicKey
_, err := hex.Decode([]byte(pubKeyHex), pubKey)
require.NoError(t, err)

var (
mgr = feeds.FeedsManager{
Name: "FMS",
URI: "localhost:8080",
PublicKey: pubKey,
}
params = feeds.RegisterManagerParams{
Name: "FMS",
URI: "localhost:8080",
PublicKey: pubKey,
}
)

svc := setupTestService(t)

svc.orm.On("CountManagers", mock.Anything).Return(int64(0), nil)
svc.orm.On("CreateManager", mock.Anything, &mgr, mock.Anything).
Return(id, errors.New("orm error"))
// ListManagers runs in a goroutine so it might be called.
svc.orm.On("ListManagers", testutils.Context(t)).Return([]feeds.FeedsManager{mgr}, nil).Maybe()

transactCall := svc.orm.On("Transact", mock.Anything, mock.Anything)
transactCall.Run(func(args mock.Arguments) {
fn := args[1].(func(orm feeds.ORM) error)
transactCall.ReturnArguments = mock.Arguments{fn(svc.orm)}
})
_, err = svc.RegisterManager(testutils.Context(t), params)
// We need to stop the service because the manager will attempt to make a
// connection
svc.Close()
require.Error(t, err)
assert.Equal(t, "orm error", err.Error())
}

func Test_Service_ListManagers(t *testing.T) {
t.Parallel()
ctx := testutils.Context(t)
Expand Down

0 comments on commit 35f68c8

Please sign in to comment.