mirror of
https://github.com/slackhq/nebula.git
synced 2025-11-22 00:15:37 +01:00
Combine ca, cert, and key handling (#952)
This commit is contained in:
@@ -12,18 +12,38 @@ type ContextualError struct {
|
||||
Context string
|
||||
}
|
||||
|
||||
func NewContextualError(msg string, fields map[string]interface{}, realError error) ContextualError {
|
||||
return ContextualError{Context: msg, Fields: fields, RealError: realError}
|
||||
func NewContextualError(msg string, fields map[string]interface{}, realError error) *ContextualError {
|
||||
return &ContextualError{Context: msg, Fields: fields, RealError: realError}
|
||||
}
|
||||
|
||||
func (ce ContextualError) Error() string {
|
||||
// ContextualizeIfNeeded is a helper function to turn an error into a ContextualError if it is not already one
|
||||
func ContextualizeIfNeeded(msg string, err error) error {
|
||||
switch err.(type) {
|
||||
case *ContextualError:
|
||||
return err
|
||||
default:
|
||||
return NewContextualError(msg, nil, err)
|
||||
}
|
||||
}
|
||||
|
||||
// LogWithContextIfNeeded is a helper function to log an error line for an error or ContextualError
|
||||
func LogWithContextIfNeeded(msg string, err error, l *logrus.Logger) {
|
||||
switch v := err.(type) {
|
||||
case *ContextualError:
|
||||
v.Log(l)
|
||||
default:
|
||||
l.WithError(err).Error(msg)
|
||||
}
|
||||
}
|
||||
|
||||
func (ce *ContextualError) Error() string {
|
||||
if ce.RealError == nil {
|
||||
return ce.Context
|
||||
}
|
||||
return ce.RealError.Error()
|
||||
}
|
||||
|
||||
func (ce ContextualError) Unwrap() error {
|
||||
func (ce *ContextualError) Unwrap() error {
|
||||
if ce.RealError == nil {
|
||||
return errors.New(ce.Context)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package util
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
@@ -67,3 +68,44 @@ func TestContextualError_Log(t *testing.T) {
|
||||
e.Log(l)
|
||||
assert.Equal(t, []string{"level=error error=error\n"}, tl.Logs)
|
||||
}
|
||||
|
||||
func TestLogWithContextIfNeeded(t *testing.T) {
|
||||
l := logrus.New()
|
||||
l.Formatter = &logrus.TextFormatter{
|
||||
DisableTimestamp: true,
|
||||
DisableColors: true,
|
||||
}
|
||||
|
||||
tl := NewTestLogWriter()
|
||||
l.Out = tl
|
||||
|
||||
// Test ignoring fallback context
|
||||
tl.Reset()
|
||||
e := NewContextualError("test message", m{"field": "1"}, errors.New("error"))
|
||||
LogWithContextIfNeeded("This should get thrown away", e, l)
|
||||
assert.Equal(t, []string{"level=error msg=\"test message\" error=error field=1\n"}, tl.Logs)
|
||||
|
||||
// Test using fallback context
|
||||
tl.Reset()
|
||||
err := fmt.Errorf("this is a normal error")
|
||||
LogWithContextIfNeeded("Fallback context woo", err, l)
|
||||
assert.Equal(t, []string{"level=error msg=\"Fallback context woo\" error=\"this is a normal error\"\n"}, tl.Logs)
|
||||
}
|
||||
|
||||
func TestContextualizeIfNeeded(t *testing.T) {
|
||||
// Test ignoring fallback context
|
||||
e := NewContextualError("test message", m{"field": "1"}, errors.New("error"))
|
||||
assert.Same(t, e, ContextualizeIfNeeded("should be ignored", e))
|
||||
|
||||
// Test using fallback context
|
||||
err := fmt.Errorf("this is a normal error")
|
||||
cErr := ContextualizeIfNeeded("Fallback context woo", err)
|
||||
|
||||
switch v := cErr.(type) {
|
||||
case *ContextualError:
|
||||
assert.Equal(t, err, v.RealError)
|
||||
default:
|
||||
t.Error("Error was not wrapped")
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user