• 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.go
1
// Copyright (c) 2014 - 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 eventhorizon
16

17
import (
18
        "context"
19
        "errors"
20
        "reflect"
21
        "runtime"
22
        "strings"
23
)
24

25
// ErrMissingEvent is when there is no event to be handled.
26
var ErrMissingEvent = errors.New("missing event")
27

28
// EventHandlerType is the type of an event handler, used as its unique identifier.
29
type EventHandlerType string
30

31
// String returns the string representation of an event handler type.
32
func (ht EventHandlerType) String() string {
×
33
        return string(ht)
×
34
}
×
35

36
// EventHandler is a handler of events. If registered on a bus as a handler only
37
// one handler of the same type will receive each event. If registered on a bus
38
// as an observer all handlers of the same type will receive each event.
39
type EventHandler interface {
40
        // HandlerType is the type of the handler.
41
        HandlerType() EventHandlerType
42

43
        // HandleEvent handles an event.
44
        HandleEvent(context.Context, Event) error
45
}
46

47
// EventHandlerFunc is a function that can be used as a event handler.
48
type EventHandlerFunc func(context.Context, Event) error
49

50
// HandleEvent implements the HandleEvent method of the EventHandler.
51
func (f EventHandlerFunc) HandleEvent(ctx context.Context, e Event) error {
×
52
        return f(ctx, e)
×
53
}
×
54

55
// HandlerType implements the HandlerType method of the EventHandler by returning
56
// the name of the package and function:
57
// "github.com/looplab/eventhorizon.Function" becomes "eventhorizon-Function".
58
func (f EventHandlerFunc) HandlerType() EventHandlerType {
×
59
        fullName := runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name() // Extract full func name: github.com/...
×
60
        parts := strings.Split(fullName, "/")                              // Split URL.
×
61
        name := parts[len(parts)-1]                                        // Take only the last part: package.Function.
×
62

×
63
        return EventHandlerType(strings.ReplaceAll(name, ".", "-")) // Use - as separator.
×
64
}
×
65

66
// EventHandlerError is an error returned when an event could not be
67
// handled by an event handler.
68
type EventHandlerError struct {
69
        // Err is the error.
70
        Err error
71
        // Event is the event that failed to be handled.
72
        Event Event
73
}
74

75
// Error implements the Error method of the errors.Error interface.
76
func (e *EventHandlerError) Error() string {
×
77
        str := "could not handle event: "
×
78

×
79
        if e.Err != nil {
×
80
                str += e.Err.Error()
×
81
        } else {
×
82
                str += "unknown error"
×
83
        }
×
84

85
        if e.Event != nil {
×
86
                str += ", " + e.Event.String()
×
87
        }
×
88

89
        return str
×
90
}
91

92
// Unwrap implements the errors.Unwrap method.
93
func (e *EventHandlerError) Unwrap() error {
×
94
        return e.Err
×
95
}
×
96

97
// Cause implements the github.com/pkg/errors Unwrap method.
98
func (e *EventHandlerError) Cause() error {
×
99
        return e.Unwrap()
×
100
}
×
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