@@ -96,7 +96,7 @@ func NewCustomError(message string) error {
9696}
9797
9898func TestWithStack (t * testing.T ) {
99- t .Run ("when WithStack is called on a custom error type composed with Err, it should add a stack trace" , func (t * testing.T ) {
99+ t .Run ("when WithStack is provided with an error of type Err, it should add a stack trace to the error " , func (t * testing.T ) {
100100 err := NewCustomError ("this is a custom error type with stack" )
101101
102102 if err .(CustomError ).Stack == nil {
@@ -115,3 +115,26 @@ func TestWithStack(t *testing.T) {
115115 }
116116 })
117117}
118+
119+ // CustomError2 is a custom error type composed with Err.
120+ type CustomError2 struct {
121+ * Err
122+ }
123+
124+ // NewCustom2Error returns a new CustomError2 and adds a cause to the error.
125+ func NewCustom2Error (message string , cause error ) error {
126+ customError2 := CustomError2 {Err : & Err {Message : message }}
127+ WithCause (customError2 .Err , cause )
128+ return customError2
129+ }
130+
131+ func TestWithCause (t * testing.T ) {
132+ t .Run ("when WithCause is provided with an error and a cause, it should add the cause to the error" , func (t * testing.T ) {
133+ causeErr := New ("inner error" )
134+ err := NewCustom2Error ("outer error" , causeErr )
135+
136+ if err .(CustomError2 ).Cause != causeErr {
137+ t .Errorf (`expected cause to be %v, got %v` , causeErr , err .(CustomError2 ).Cause )
138+ }
139+ })
140+ }
0 commit comments