• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

looplab / eventhorizon / 12622512440

05 Jan 2025 07:58PM UTC coverage: 27.495% (-39.9%) from 67.361%
12622512440

Pull #419

github

web-flow
Merge b3c17d928 into ac3a97277
Pull Request #419: fix(ci): update to up/download-artifact v4

1769 of 6434 relevant lines covered (27.49%)

1.41 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/eventhandler/saga/eventhandler.go
1
// Copyright (c) 2016 - The Event Horizon authors.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15
package saga
16

17
import (
18
        "context"
19
        "fmt"
20

21
        eh "github.com/looplab/eventhorizon"
22
)
23

24
// EventHandler is a CQRS saga handler to run a Saga implementation.
25
type EventHandler struct {
26
        saga           Saga
27
        commandHandler eh.CommandHandler
28
}
29

30
var _ = eh.EventHandler(&EventHandler{})
31

32
// Saga is an interface for a CQRS saga that listens to events and generate
33
// commands. It is used for any long lived transaction and can be used to react
34
// on multiple events.
35
type Saga interface {
36
        // SagaType returns the type of the saga.
37
        SagaType() Type
38

39
        // RunSaga handles an event in the saga that can return commands.
40
        // If an error is returned from the saga, the event will be run again.
41
        RunSaga(context.Context, eh.Event, eh.CommandHandler) error
42
}
43

44
// Type is the type of a saga, used as its unique identifier.
45
type Type string
46

47
// String returns the string representation of a saga type.
48
func (t Type) String() string {
×
49
        return string(t)
×
50
}
×
51

52
// Error is an error in the projector.
53
type Error struct {
54
        // Err is the error that happened when projecting the event.
55
        Err error
56
        // Saga is the saga where the error happened.
57
        Saga string
58
}
59

60
// Error implements the Error method of the errors.Error interface.
61
func (e *Error) Error() string {
×
62
        return fmt.Sprintf("%s: %s", e.Saga, e.Err)
×
63
}
×
64

65
// Unwrap implements the errors.Unwrap method.
66
func (e *Error) Unwrap() error {
×
67
        return e.Err
×
68
}
×
69

70
// Cause implements the github.com/pkg/errors Unwrap method.
71
func (e *Error) Cause() error {
×
72
        return e.Unwrap()
×
73
}
×
74

75
// NewEventHandler creates a new EventHandler.
76
func NewEventHandler(saga Saga, commandHandler eh.CommandHandler) *EventHandler {
×
77
        return &EventHandler{
×
78
                saga:           saga,
×
79
                commandHandler: commandHandler,
×
80
        }
×
81
}
×
82

83
// HandlerType implements the HandlerType method of the eventhorizon.EventHandler interface.
84
func (h *EventHandler) HandlerType() eh.EventHandlerType {
×
85
        return eh.EventHandlerType("saga_" + h.saga.SagaType())
×
86
}
×
87

88
// HandleEvent implements the HandleEvent method of the eventhorizon.EventHandler interface.
89
func (h *EventHandler) HandleEvent(ctx context.Context, event eh.Event) error {
×
90
        if event == nil {
×
91
                return &Error{
×
92
                        Err:  eh.ErrMissingEvent,
×
93
                        Saga: h.saga.SagaType().String(),
×
94
                }
×
95
        }
×
96

97
        // Run the saga which can issue commands on the provided command handler.
98
        if err := h.saga.RunSaga(ctx, event, h.commandHandler); err != nil {
×
99
                return &Error{
×
100
                        Err:  err,
×
101
                        Saga: h.saga.SagaType().String(),
×
102
                }
×
103
        }
×
104

105
        return nil
×
106
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc