-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_test.go
43 lines (34 loc) · 964 Bytes
/
context_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package errors_test
import (
"fmt"
"log"
"testing"
"github.com/stretchr/testify/assert"
"github.com/tim-online/go-errors"
)
func TestContext(t *testing.T) {
errWithoutContext := errors.New("Without Context")
errWithContext := errors.WithContext(errWithoutContext, map[string]interface{}{
"key": "value",
"k": "v",
})
assert.Contains(t, errors.Context(errWithContext), "key")
assert.Contains(t, errors.Context(errWithContext), "k")
assert.Equal(t, errors.Context(errWithContext)["key"], "value")
assert.Equal(t, errors.Context(errWithContext)["k"], "v")
assert.EqualError(t, errWithContext, "Without Context [k:v, key:value]")
}
type customContextError struct {
error
}
func (e customContextError) Context() map[string]interface{} {
return map[string]interface{}{
"key": "value",
}
}
func TestCustomContextError(t *testing.T) {
contextError := customContextError{
error: fmt.Errorf("Base error"),
}
log.Println(contextError)
}