Skip to content

Commit

Permalink
Merge pull request #3 from Bibob7/make-caller-nillable
Browse files Browse the repository at this point in the history
Make caller nillable
  • Loading branch information
Bibob7 authored Feb 27, 2022
2 parents b4dd3ba + 8c74e83 commit a5d04b4
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 15 deletions.
10 changes: 5 additions & 5 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}
8 changes: 4 additions & 4 deletions logging.go
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 1 addition & 1 deletion logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -42,13 +42,13 @@ 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(), "/")
funcName := details.Name()
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}
}
5 changes: 3 additions & 2 deletions stack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"testing"
)

func callFake() Caller {
func callFake() *Caller {
return caller()
}

Expand All @@ -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) {
Expand Down

0 comments on commit a5d04b4

Please sign in to comment.