From 2e14e0b46deaa19f5b275bd1d0b05b3dde93f5c9 Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Wed, 27 Jul 2016 14:51:04 -0700 Subject: [PATCH] madeit so that for flog.Error(), flog.Errorf(), flog.Errorln() that anything that fits an error that is passed at a parameter gets collected into the "~errors" key in the context. the thing under the "~errors" key is a []error. --- error.go | 44 ++++++++++- error_test.go | 198 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 239 insertions(+), 3 deletions(-) create mode 100644 error_test.go diff --git a/error.go b/error.go index dcdd265..e5f77eb 100644 --- a/error.go +++ b/error.go @@ -17,14 +17,18 @@ func (flogger *internalFlogger) Error(v ...interface{}) { msg := fmt.Sprint(v...) - flogger.route(msg, errorContext) + errCtx := newErrorContext(errorContext, v...) + + flogger.route(msg, errCtx) } func (flogger *internalFlogger) Errorf(format string, v ...interface{}) { msg := fmt.Sprintf(format, v...) - flogger.route(msg, errorContext) + errCtx := newErrorContext(errorContext, v...) + + flogger.route(msg, errCtx) } func (flogger *internalFlogger) Errorfe(err error, format string, v ...interface{}) { @@ -44,5 +48,39 @@ func (flogger *internalFlogger) Errorln(v ...interface{}) { msg := fmt.Sprintln(v...) - flogger.route(msg, errorContext) + errCtx := newErrorContext(errorContext, v...) + + flogger.route(msg, errCtx) +} + + +func newErrorContext(baseContext map[string]interface{}, v ...interface{}) map[string]interface{} { + + + // Collect any errors. + errs := []error{} + for _, datum := range v { + if err, ok := datum.(error); ok { + errs = append(errs, err) + } + } + + + if 0 == len(errs) { + return baseContext + } + + + // Copy the base context. + context := map[string]interface{}{} + for k,v := range baseContext { + context[k] = v + } + + + // Put the collected errors in this new context. + context["~errors"] = errs + + + return context } diff --git a/error_test.go b/error_test.go new file mode 100644 index 0000000..b5edac8 --- /dev/null +++ b/error_test.go @@ -0,0 +1,198 @@ +package flog + + +import ( + "errors" + "reflect" + + "testing" +) + + +func TestNewErrorContext(t *testing.T) { + + err1 := errors.New("error 1") + err2 := errors.New("error 2") + err3 := errors.New("error 3") + err4 := errors.New("error 4") + err5 := errors.New("error 5") + + + tests := []struct{ + BaseContext map[string]interface{} + V []interface{} + Expected map[string]interface{} + }{ + { + BaseContext: map[string]interface{}{ + "~type":"error", + }, + V: []interface{}{ + // Nothing here. + }, + Expected: map[string]interface{}{ + "~type":"error", + }, + }, + + + + { + BaseContext: map[string]interface{}{ + "~type":"error", + }, + V: []interface{}{ + 1, + }, + Expected: map[string]interface{}{ + "~type":"error", + }, + }, + { + BaseContext: map[string]interface{}{ + "~type":"error", + }, + V: []interface{}{ + 1, "two", + }, + Expected: map[string]interface{}{ + "~type":"error", + }, + }, + { + BaseContext: map[string]interface{}{ + "~type":"error", + }, + V: []interface{}{ + 1, "two", 3.0, + }, + Expected: map[string]interface{}{ + "~type":"error", + }, + }, + + + + { + BaseContext: map[string]interface{}{ + "~type":"error", + }, + V: []interface{}{ + 1, "two", 3.0, + }, + Expected: map[string]interface{}{ + "~type":"error", + }, + }, + + + + { + BaseContext: map[string]interface{}{ + "~type":"error", + }, + V: []interface{}{ + err1, + }, + Expected: map[string]interface{}{ + "~type":"error", + "~errors": []error{ + err1, + }, + }, + }, + { + BaseContext: map[string]interface{}{ + "~type":"error", + }, + V: []interface{}{ + err1, err2, + }, + Expected: map[string]interface{}{ + "~type":"error", + "~errors": []error{ + err1, err2, + }, + }, + }, + { + BaseContext: map[string]interface{}{ + "~type":"error", + }, + V: []interface{}{ + err1, err2, err3, + }, + Expected: map[string]interface{}{ + "~type":"error", + "~errors": []error{ + err1, err2, err3, + }, + }, + }, + { + BaseContext: map[string]interface{}{ + "~type":"error", + }, + V: []interface{}{ + err1, err2, err3, err4, + }, + Expected: map[string]interface{}{ + "~type":"error", + "~errors": []error{ + err1, err2, err3, err4, + }, + }, + }, + { + BaseContext: map[string]interface{}{ + "~type":"error", + }, + V: []interface{}{ + err1, err2, err3, err4, err5, + }, + Expected: map[string]interface{}{ + "~type":"error", + "~errors": []error{ + err1, err2, err3, err4, err5, + }, + }, + }, + + + + { + BaseContext: map[string]interface{}{ + "~type":"error", + }, + V: []interface{}{ + 1, err1, "two", err2, 3.0, err3, '4', + }, + Expected: map[string]interface{}{ + "~type":"error", + "~errors": []error{ + err1, err2, err3, + }, + }, + }, + } + + + for testNumber, test := range tests { + + actualContext := newErrorContext(test.BaseContext, test.V...) + if actual := actualContext; nil == actual { + t.Errorf("For test #%d, did not expected nil, but actually got %v", testNumber, actual) + continue + } + + if expected, actual := len(test.Expected), len(actualContext); expected != actual { + t.Errorf("For test #%d, expected %d, but actually got %d.", testNumber, expected, actual) + continue + } + + if expected, actual := test.Expected, actualContext; !reflect.DeepEqual(expected, actual) { + t.Errorf("For test #%d, expected...\n%#v\nbut actually got...\n%#v", testNumber, expected, actual) + continue + } + } +}