From 1acc4509e06a04dded8becb894288b98ac0db5b1 Mon Sep 17 00:00:00 2001 From: Eela Nagaraj <7308464+eelanagaraj@users.noreply.github.com> Date: Thu, 8 Apr 2021 14:14:01 +0200 Subject: [PATCH] Fix error logging to not modify underlying object (#157) * Fix error logging to not modify underlying object * Add license --- service/rpc/errors.go | 6 +++-- service/rpc/errors_test.go | 46 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 service/rpc/errors_test.go diff --git a/service/rpc/errors.go b/service/rpc/errors.go index b7d227d4..f9be1a1c 100644 --- a/service/rpc/errors.go +++ b/service/rpc/errors.go @@ -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 { diff --git a/service/rpc/errors_test.go b/service/rpc/errors_test.go new file mode 100644 index 00000000..9831632d --- /dev/null +++ b/service/rpc/errors_test.go @@ -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)) + }) +}