-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgoerr_test.go
140 lines (116 loc) · 3.04 KB
/
goerr_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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package stackerr
import (
"bytes"
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func f1() *Err {
err := New("message")
return err.Stack()
}
func f2() *Err {
err := f1()
return err.StackWithContext("context")
}
type t1 struct{}
func (t *t1) f3() *Err {
err := f2()
return err.Stack()
}
func TestStackTrace(t *testing.T) {
path, _ := os.Getwd()
if os.Getenv("GOPATH") != "" {
path = strings.Replace(path, os.Getenv("GOPATH")+"/src/", "", -1)
}
ts := t1{}
err := ts.f3()
assert.NotNil(t, err)
assert.Equal(t, "message", err.Error())
assert.Equal(t,
fmt.Sprintf(`Error Stacktrace:
-> %[1]s/goerr_test.go:40 (stackerr.TestStackTrace)
-> %[1]s/goerr_test.go:31 (stackerr.(*t1).f3)
-> %[1]s/goerr_test.go:24 (stackerr.f2) context
-> %[1]s/goerr_test.go:18 (stackerr.f1)
`, path), err.Sprint())
}
func TestNew(t *testing.T) {
err := New("message")
assert.NotNil(t, err)
assert.Equal(t, "message", err.Error())
assert.Equal(t, http.StatusInternalServerError, err.StatusCode)
}
func TestNewFromError(t *testing.T) {
e := errors.New("message")
err := NewFromError(e)
assert.NotNil(t, err)
assert.Equal(t, "message", err.Error())
assert.Equal(t, http.StatusInternalServerError, err.StatusCode)
errErr := NewFromError(err)
assert.NotNil(t, errErr)
assert.Equal(t, "message", errErr.Error())
assert.Equal(t, http.StatusInternalServerError, errErr.StatusCode)
}
func TestNewWithStatusCode(t *testing.T) {
err := NewWithStatusCode("message", http.StatusOK)
assert.NotNil(t, err)
assert.Equal(t, "message", err.Error())
assert.Equal(t, http.StatusOK, err.StatusCode)
}
func TestLog(t *testing.T) {
path, _ := os.Getwd()
if os.Getenv("GOPATH") != "" {
path = strings.Replace(path, os.Getenv("GOPATH")+"/src/", "", -1)
}
var buf bytes.Buffer
log.SetOutput(&buf)
err := f2()
err.Log()
log.SetOutput(os.Stderr)
assert.Contains(t, buf.String(),
fmt.Sprintf(`Error Stacktrace:
-> %[1]s/goerr_test.go:91 (stackerr.TestLog)
-> %[1]s/goerr_test.go:24 (stackerr.f2) context
-> %[1]s/goerr_test.go:18 (stackerr.f1)
`, path))
}
func TestIsNotFound(t *testing.T) {
err := NewWithStatusCode("message", http.StatusOK)
assert.False(t, err.IsNotFound())
errNotFound := NewWithStatusCode("message", http.StatusNotFound)
assert.True(t, errNotFound.IsNotFound())
}
func TestPrint(t *testing.T) {
path, _ := os.Getwd()
if os.Getenv("GOPATH") != "" {
path = strings.Replace(path, os.Getenv("GOPATH")+"/src/", "", -1)
}
old := os.Stdout // keep backup of the real stdout
r, w, _ := os.Pipe()
os.Stdout = w
outC := make(chan string)
// copy the output in a separate goroutine so printing can't block indefinitely
go func() {
var buf bytes.Buffer
io.Copy(&buf, r)
outC <- buf.String()
}()
err := f2()
err.Print()
w.Close()
os.Stdout = old // restoring the real stdout
out := <-outC
assert.Equal(t, out,
fmt.Sprintf(`Error Stacktrace:
-> %[1]s/goerr_test.go:128 (stackerr.TestPrint)
-> %[1]s/goerr_test.go:24 (stackerr.f2) context
-> %[1]s/goerr_test.go:18 (stackerr.f1)
`, path))
}