44 "errors"
55 "fmt"
66 "testing"
7+ "time"
78
89 "github.com/stretchr/testify/require"
910)
@@ -29,8 +30,7 @@ func TestPanicErrorx(t *testing.T) {
2930 output := fmt .Sprintf ("%v" , r )
3031
3132 require .Contains (t , output , "awful" , output )
32- // original error was non-errorx, no use of adding panic callstack to message
33- require .NotContains (t , output , "errorx.funcWithBadPanic()" , output )
33+ require .Contains (t , output , "errorx.funcWithBadPanic()" , output )
3434 }()
3535
3636 funcWithBadPanic ()
@@ -111,3 +111,68 @@ func funcWithBadPanic() {
111111func funcWithBadErr () error {
112112 return errors .New ("awful" )
113113}
114+
115+ func TestPanicChain (t * testing.T ) {
116+ ch0 := make (chan error , 1 )
117+ ch1 := make (chan error , 1 )
118+
119+ doMischief (ch1 )
120+ doMoreMischief (ch0 , ch1 )
121+
122+ select {
123+ case err := <- ch0 :
124+ require .Error (t , err )
125+ output := fmt .Sprintf ("%+v" , err )
126+ require .Contains (t , output , "mischiefProper" , output )
127+ require .Contains (t , output , "mischiefAsPanic" , output )
128+ require .Contains (t , output , "doMischief" , output )
129+ require .Contains (t , output , "handleMischief" , output )
130+ require .Contains (t , output , "doMoreMischief" , output )
131+ t .Log (output )
132+ case <- time .After (time .Second ):
133+ require .Fail (t , "expected error" )
134+ }
135+ }
136+
137+ func doMoreMischief (ch0 chan error , ch1 chan error ) {
138+ defer func () {
139+ if e := recover (); e != nil {
140+ err , ok := ErrorFromPanic (e )
141+ if ok {
142+ ch0 <- Decorate (err , "hop 2" )
143+ return
144+ }
145+ }
146+ ch0 <- AssertionFailed .New ("test failed" )
147+ }()
148+
149+ handleMischief (ch1 )
150+ }
151+
152+ func handleMischief (ch chan error ) {
153+ err := <- ch
154+ Panic (Decorate (err , "handle" ))
155+ }
156+
157+ func doMischief (ch chan error ) {
158+ defer func () {
159+ if e := recover (); e != nil {
160+ err , ok := ErrorFromPanic (e )
161+ if ok {
162+ ch <- Decorate (err , "hop 1" )
163+ return
164+ }
165+ }
166+ ch <- AssertionFailed .New ("test failed" ) // todo check
167+ }()
168+
169+ mischiefAsPanic ()
170+ }
171+
172+ func mischiefAsPanic () {
173+ Panic (mischiefProper ())
174+ }
175+
176+ func mischiefProper () error {
177+ return ExternalError .New ("mischief" )
178+ }
0 commit comments