File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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.
217217func (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
Original file line number Diff line number Diff line change 11package errors
22
33import (
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
115117func 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}
You can’t perform that action at this time.
0 commit comments