@@ -99,7 +99,7 @@ func TestErrorRecorder(t *testing.T) {
9999 }, trackingEventStore .Recorded ())
100100 })
101101
102- t .Run ("when handler fails and CaptureError is set to true, no error is returned" , func (t * testing.T ) {
102+ t .Run ("when handler fails and ShouldCaptureError returns true, no error is returned" , func (t * testing.T ) {
103103 eventStore := inmemory .NewEventStore ()
104104 trackingEventStore := inmemory .NewTrackingEventStore (eventStore )
105105
@@ -109,11 +109,13 @@ func TestErrorRecorder(t *testing.T) {
109109 }
110110
111111 handler := command.ErrorRecorder {
112- CaptureErrors : true ,
113112 Handler : command .HandlerFunc (func (ctx context.Context , cmd eventually.Command ) error {
114113 return expectedErr
115114 }),
116115 Appender : trackingEventStore ,
116+ ShouldCaptureError : func (err error ) bool {
117+ return true
118+ },
117119 EventMapper : func (err error , cmd eventually.Command ) eventually.Payload {
118120 return mockCommandHasFailed {
119121 err : err ,
@@ -142,6 +144,53 @@ func TestErrorRecorder(t *testing.T) {
142144 }, trackingEventStore .Recorded ())
143145 })
144146
147+ t .Run ("when handler fails and ShouldCaptureError returns false, error is returned to caller" , func (t * testing.T ) {
148+ eventStore := inmemory .NewEventStore ()
149+ trackingEventStore := inmemory .NewTrackingEventStore (eventStore )
150+
151+ expectedErr := errors .New ("failed command" )
152+ unexpectedErr := errors .New ("unexpected error" )
153+
154+ expectedCommand := eventually.Command {
155+ Payload : mockCommand {message : t .Name ()},
156+ }
157+
158+ handler := command.ErrorRecorder {
159+ Handler : command .HandlerFunc (func (ctx context.Context , cmd eventually.Command ) error {
160+ return expectedErr
161+ }),
162+ Appender : trackingEventStore ,
163+ ShouldCaptureError : func (err error ) bool {
164+ return errors .Is (err , unexpectedErr ) // This will return false
165+ },
166+ EventMapper : func (err error , cmd eventually.Command ) eventually.Payload {
167+ return mockCommandHasFailed {
168+ err : err ,
169+ command : cmd .Payload .(mockCommand ),
170+ }
171+ },
172+ }
173+
174+ err := handler .Handle (context .Background (), expectedCommand )
175+
176+ assert .ErrorIs (t , err , expectedErr )
177+ assert .Equal (t , []eventstore.Event {
178+ {
179+ Version : 1 ,
180+ Stream : stream.ID {
181+ Type : command .FailedType ,
182+ Name : expectedCommand .Payload .Name (),
183+ },
184+ Event : eventually.Event {
185+ Payload : mockCommandHasFailed {
186+ err : expectedErr ,
187+ command : expectedCommand .Payload .(mockCommand ),
188+ },
189+ },
190+ },
191+ }, trackingEventStore .Recorded ())
192+ })
193+
145194 t .Run ("when handler fails, record event with custom stream type" , func (t * testing.T ) {
146195 eventStore := inmemory .NewEventStore ()
147196 trackingEventStore := inmemory .NewTrackingEventStore (eventStore )
0 commit comments