Skip to content

Commit bc2885c

Browse files
committed
fix errors as append
1 parent 3899abd commit bc2885c

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

errors.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func As(err error, reason ...interface{}) Error {
142142
as = append(as, reason...)
143143
}
144144
return &errImpl{
145-
append(ErrData{e.data[0]}, e.data[1].(ErrData), as),
145+
append(e.data, as),
146146
}
147147
}
148148

@@ -215,12 +215,12 @@ func (e *errImpl) MarshalJSON() ([]byte, error) {
215215

216216
// Record the stack when call, and return a new error with new stack.
217217
func (e *errImpl) As(reason ...interface{}) Error {
218-
as := []interface{}{caller(2)}
218+
as := ErrData{caller(2)}
219219
if len(reason) > 0 {
220220
as = append(as, reason...)
221221
}
222222
return &errImpl{
223-
append(ErrData{e.data[0]}, e.data[1].(ErrData), as),
223+
append(e.data, as),
224224
}
225225
}
226226

errors_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package errors
22

33
import (
4+
"encoding/json"
45
"errors"
56
"fmt"
67
"testing"
@@ -109,7 +110,8 @@ func TestAs(t *testing.T) {
109110
fmt.Println("As1:", outErr1)
110111
fmt.Println("As2:", outErr2)
111112
fmt.Println("As3:", outErr3)
112-
fmt.Println("As4:", outErr4)
113+
fmt.Println("As4:", outErr4.As("two as"))
114+
fmt.Println("As5:", outErr4)
113115
}
114116

115117
func TestError(t *testing.T) {
@@ -139,8 +141,13 @@ func TestError(t *testing.T) {
139141
if err2 == outErr2 {
140142
t.Fatal("need return another error")
141143
}
144+
jsonIndent, err := json.MarshalIndent(err1.(*errImpl), "", " ")
145+
if err != nil {
146+
t.Fatal(err)
147+
}
142148
fmt.Println("Err:", outErr4.Error())
143149
fmt.Println("Err:", err1.(*errImpl))
144150
fmt.Println("Err:", err1.As(err2))
145151
fmt.Println("Err:", As(New("two caller without args")))
152+
fmt.Println("Json Indent:", string(jsonIndent))
146153
}

0 commit comments

Comments
 (0)