@@ -145,7 +145,8 @@ func TestErrorRecorder(t *testing.T) {
145145 eventStore := inmemory .NewEventStore ()
146146 trackingEventStore := inmemory .NewTrackingEventStore (eventStore )
147147
148- expectedStreamType := "mocks-command"
148+ const expectedStreamType = "mocks-command"
149+
149150 expectedErr := errors .New ("failed command" )
150151 expectedCommand := eventually.Command {
151152 Payload : mockCommand {message : t .Name ()},
@@ -184,4 +185,51 @@ func TestErrorRecorder(t *testing.T) {
184185 },
185186 }, trackingEventStore .Recorded ())
186187 })
188+
189+ t .Run ("when handler fails, record event with custom stream name" , func (t * testing.T ) {
190+ eventStore := inmemory .NewEventStore ()
191+ trackingEventStore := inmemory .NewTrackingEventStore (eventStore )
192+
193+ expectedStreamType := "mocks-command"
194+ expectedErr := errors .New ("failed command" )
195+ expectedCommand := eventually.Command {
196+ Payload : mockCommand {message : t .Name ()},
197+ }
198+
199+ handler := command.ErrorRecorder {
200+ Handler : command .HandlerFunc (func (ctx context.Context , cmd eventually.Command ) error {
201+ return expectedErr
202+ }),
203+ Appender : trackingEventStore ,
204+ StreamType : expectedStreamType ,
205+ StreamNameMapper : func (cmd eventually.Command ) string {
206+ return cmd .Payload .(mockCommand ).message
207+ },
208+ EventMapper : func (err error , cmd eventually.Command ) eventually.Payload {
209+ return mockCommandHasFailed {
210+ err : err ,
211+ command : cmd .Payload .(mockCommand ),
212+ }
213+ },
214+ }
215+
216+ err := handler .Handle (context .Background (), expectedCommand )
217+
218+ assert .Error (t , err )
219+ assert .Equal (t , []eventstore.Event {
220+ {
221+ Version : 1 ,
222+ Stream : eventstore.StreamID {
223+ Type : expectedStreamType ,
224+ Name : expectedCommand .Payload .(mockCommand ).message ,
225+ },
226+ Event : eventually.Event {
227+ Payload : mockCommandHasFailed {
228+ err : expectedErr ,
229+ command : expectedCommand .Payload .(mockCommand ),
230+ },
231+ },
232+ },
233+ }, trackingEventStore .Recorded ())
234+ })
187235}
0 commit comments