Skip to content

Commit

Permalink
Fix error logging to not modify underlying object (#157)
Browse files Browse the repository at this point in the history
* Fix error logging to not modify underlying object

* Add license
  • Loading branch information
eelanagaraj authored Apr 8, 2021
1 parent 276098f commit 1acc450
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
6 changes: 4 additions & 2 deletions service/rpc/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ func LogErrUnimplemented(rosettaEndpoint string) *types.Error {
}

func LogErrDetails(rosettaErr *types.Error, err error) *types.Error {
rosettaErr.Details = map[string]interface{}{
copyErr := &types.Error{}
*copyErr = *rosettaErr
copyErr.Details = map[string]interface{}{
"context": err.Error(),
}
return rosettaErr
return copyErr
}

func LogErrInternal(err error, params ...interface{}) *types.Error {
Expand Down
46 changes: 46 additions & 0 deletions service/rpc/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2021 Celo Org
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package rpc

import (
"errors"
"testing"

. "github.com/onsi/gomega"
)

func TestLogErrDetails(t *testing.T) {
RegisterTestingT(t)

errCode := int32(-111)
errMsg := "Fake Error"
testErr := errors.New("test error")

rosettaErr := NewErrorResponse(errCode, errMsg)
copyErr := NewErrorResponse(errCode, errMsg)
loggedErr := LogErrDetails(rosettaErr, testErr)

t.Run("Error details populated", func(t *testing.T) {
Ω(loggedErr).ShouldNot(Equal(copyErr))
copyLoggedErr := *loggedErr
copyLoggedErr.Details = nil
Ω(*loggedErr).ShouldNot(Equal(copyLoggedErr))
Ω(copyLoggedErr).Should(Equal(*copyErr))
})

t.Run("Logging doesn't change original arg", func(t *testing.T) {
Ω(rosettaErr).Should(Equal(copyErr))
})
}

0 comments on commit 1acc450

Please sign in to comment.