-
Notifications
You must be signed in to change notification settings - Fork 19
/
error.go
63 lines (53 loc) · 1.49 KB
/
error.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package events
import (
"github.com/nyaruka/goflow/flows"
)
func init() {
RegisterType(TypeError, func() flows.Event { return &ErrorEvent{} })
}
// TypeError is the type of our error events
const TypeError string = "error"
// ErrorEvent events will be created whenever an error is encountered during flow execution. This
// can vary from template evaluation errors to invalid actions.
//
// {
// "type": "error",
// "created_on": "2006-01-02T15:04:05Z",
// "text": "invalid date format: '12th of October'"
// }
//
// @event error
type ErrorEvent struct {
BaseEvent
callerOrEngineEvent
Text string `json:"text" validate:"required"`
Fatal bool `json:"fatal"`
}
// NewErrorEvent returns a new error event for the passed in error
func NewErrorEvent(err error) *ErrorEvent {
return &ErrorEvent{
BaseEvent: NewBaseEvent(),
Text: err.Error(),
}
}
// NewFatalErrorEvent returns a new fatal error event for the passed in error
func NewFatalErrorEvent(err error) *ErrorEvent {
return &ErrorEvent{
BaseEvent: NewBaseEvent(),
Text: err.Error(),
Fatal: true,
}
}
// Type returns the type of this event
func (e *ErrorEvent) Type() string { return TypeError }
// Validate validates our event is valid and has all the assets it needs
func (e *ErrorEvent) Validate(assets flows.SessionAssets) error {
return nil
}
// Apply applies this event to the given run
func (e *ErrorEvent) Apply(run flows.FlowRun) error {
if e.Fatal {
run.Exit(flows.RunStatusErrored)
}
return nil
}