From 28c9d0d15d945965ac03721562e6147912ce84ff Mon Sep 17 00:00:00 2001 From: Kevin Kreitner Date: Sun, 27 Feb 2022 18:54:51 +0100 Subject: [PATCH 1/3] Make caller nillable --- error.go | 10 +++++----- logging.go | 8 ++++---- logging_test.go | 2 +- stack.go | 6 +++--- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/error.go b/error.go index e603728..30bd0a2 100644 --- a/error.go +++ b/error.go @@ -4,14 +4,14 @@ type Error interface { error Cause() error Severity() Severity - Caller() Caller + Caller() *Caller Enrich(args ...interface{}) - setCaller(caller Caller) + setCaller(caller *Caller) } type BaseError struct { message string - caller Caller + caller *Caller severity Severity cause error } @@ -28,7 +28,7 @@ func (e *BaseError) Severity() Severity { return e.severity } -func (e *BaseError) Caller() Caller { +func (e *BaseError) Caller() *Caller { return e.caller } @@ -70,6 +70,6 @@ func (e *BaseError) Enrich(args ...interface{}) { } // setCaller is used during the creation to set the caller -func (e *BaseError) setCaller(caller Caller) { +func (e *BaseError) setCaller(caller *Caller) { e.caller = caller } diff --git a/logging.go b/logging.go index 605f708..e1f5a9b 100644 --- a/logging.go +++ b/logging.go @@ -1,15 +1,15 @@ package sterrors -var defaultFormatter Logger = &LogrusLogger{} +var defaultLogger Logger = &LogrusLogger{} type Logger interface { Log(err error) } -func SetLogger(formatter Logger) { - defaultFormatter = formatter +func SetLogger(logger Logger) { + defaultLogger = logger } func Log(err error) { - defaultFormatter.Log(err) + defaultLogger.Log(err) } diff --git a/logging_test.go b/logging_test.go index 212b3c6..caa950c 100644 --- a/logging_test.go +++ b/logging_test.go @@ -17,7 +17,7 @@ func TestSetFormatter(t *testing.T) { formatter := &TestLogFormatter{} SetLogger(formatter) - assert.Equal(t, formatter, defaultFormatter) + assert.Equal(t, formatter, defaultLogger) } func TestLog(t *testing.T) { diff --git a/stack.go b/stack.go index 9cc63e3..7bcd216 100644 --- a/stack.go +++ b/stack.go @@ -14,7 +14,7 @@ type Caller struct { type CallStackEntry struct { ErrMessage string `json:"msg,omitempty"` Severity Severity `json:"severity,omitempty"` - Caller Caller `json:"caller,omitempty"` + Caller *Caller `json:"caller,omitempty"` } // CallStack returns the callstack @@ -42,7 +42,7 @@ func CallStack(err error) []CallStackEntry { return res } -func caller() Caller { +func caller() *Caller { pc, file, line, _ := runtime.Caller(2) details := runtime.FuncForPC(pc) nameSegments := strings.Split(details.Name(), "/") @@ -50,5 +50,5 @@ func caller() Caller { if len(nameSegments) > 0 { funcName = nameSegments[len(nameSegments)-1] } - return Caller{File: file, Line: line, FuncName: funcName} + return &Caller{File: file, Line: line, FuncName: funcName} } From b95beadf18a6bed973868a7c1533dd31ef95df31 Mon Sep 17 00:00:00 2001 From: Kevin Kreitner Date: Sun, 27 Feb 2022 18:57:06 +0100 Subject: [PATCH 2/3] Fix stack test --- stack_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stack_test.go b/stack_test.go index 45b8c61..a119734 100644 --- a/stack_test.go +++ b/stack_test.go @@ -6,7 +6,7 @@ import ( "testing" ) -func callFake() Caller { +func callFake() *Caller { return caller() } From 8c74e832b0929e9de93de263bff22498a095fb03 Mon Sep 17 00:00:00 2001 From: Kevin Kreitner Date: Sun, 27 Feb 2022 19:01:28 +0100 Subject: [PATCH 3/3] Fix stack errors --- stack_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/stack_test.go b/stack_test.go index a119734..732b6e0 100644 --- a/stack_test.go +++ b/stack_test.go @@ -25,9 +25,10 @@ func TestCallStack(t *testing.T) { assert.Equal(t, "second error", callStack[1].ErrMessage) assert.Equal(t, "initial error", callStack[2].ErrMessage) + var nilCaller *Caller assert.Equal(t, thirdCall, callStack[0].Caller) assert.Equal(t, secondCall, callStack[1].Caller) - assert.Equal(t, Caller{}, callStack[2].Caller) + assert.Equal(t, nilCaller, callStack[2].Caller) } func TestCallStack_WithNotTraceableErr(t *testing.T) {