forked from slackhq/nebula
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Be more like a library to support mobile (slackhq#247)
- Loading branch information
Showing
21 changed files
with
477 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package nebula | ||
|
||
import ( | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type ContextualError struct { | ||
RealError error | ||
Fields map[string]interface{} | ||
Context string | ||
} | ||
|
||
func NewContextualError(msg string, fields map[string]interface{}, realError error) ContextualError { | ||
return ContextualError{Context: msg, Fields: fields, RealError: realError} | ||
} | ||
|
||
func (ce ContextualError) Error() string { | ||
return ce.RealError.Error() | ||
} | ||
|
||
func (ce ContextualError) Unwrap() error { | ||
return ce.RealError | ||
} | ||
|
||
func (ce *ContextualError) Log(lr *logrus.Logger) { | ||
if ce.RealError != nil { | ||
lr.WithFields(ce.Fields).WithError(ce.RealError).Error(ce.Context) | ||
} else { | ||
lr.WithFields(ce.Fields).Error(ce.Context) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package nebula | ||
|
||
import ( | ||
"errors" | ||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
type TestLogWriter struct { | ||
Logs []string | ||
} | ||
|
||
func NewTestLogWriter() *TestLogWriter { | ||
return &TestLogWriter{Logs: make([]string, 0)} | ||
} | ||
|
||
func (tl *TestLogWriter) Write(p []byte) (n int, err error) { | ||
tl.Logs = append(tl.Logs, string(p)) | ||
return len(p), nil | ||
} | ||
|
||
func (tl *TestLogWriter) Reset() { | ||
tl.Logs = tl.Logs[:0] | ||
} | ||
|
||
func TestContextualError_Log(t *testing.T) { | ||
l := logrus.New() | ||
l.Formatter = &logrus.TextFormatter{ | ||
DisableTimestamp: true, | ||
DisableColors: true, | ||
} | ||
|
||
tl := NewTestLogWriter() | ||
l.Out = tl | ||
|
||
// Test a full context line | ||
tl.Reset() | ||
e := NewContextualError("test message", m{"field": "1"}, errors.New("error")) | ||
e.Log(l) | ||
assert.Equal(t, []string{"level=error msg=\"test message\" error=error field=1\n"}, tl.Logs) | ||
|
||
// Test a line with an error and msg but no fields | ||
tl.Reset() | ||
e = NewContextualError("test message", nil, errors.New("error")) | ||
e.Log(l) | ||
assert.Equal(t, []string{"level=error msg=\"test message\" error=error\n"}, tl.Logs) | ||
|
||
// Test just a context and fields | ||
tl.Reset() | ||
e = NewContextualError("test message", m{"field": "1"}, nil) | ||
e.Log(l) | ||
assert.Equal(t, []string{"level=error msg=\"test message\" field=1\n"}, tl.Logs) | ||
|
||
// Test just a context | ||
tl.Reset() | ||
e = NewContextualError("test message", nil, nil) | ||
e.Log(l) | ||
assert.Equal(t, []string{"level=error msg=\"test message\"\n"}, tl.Logs) | ||
|
||
// Test just an error | ||
tl.Reset() | ||
e = NewContextualError("", nil, errors.New("error")) | ||
e.Log(l) | ||
assert.Equal(t, []string{"level=error error=error\n"}, tl.Logs) | ||
} |
Oops, something went wrong.