forked from harness/gitness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_error.go
40 lines (33 loc) · 923 Bytes
/
node_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
package parse
import "fmt"
// ErrorNode executes the body node, and then executes the error node if
// the body node errors. This is similar to defer but only executes on error.
type ErrorNode struct {
NodeType `json:"type"`
Body Node `json:"body"` // evaluate node
Defer Node `json:"defer"` // defer evaluation of node on error.
}
// NewErrorNode returns a new ErrorNode.
func NewErrorNode() *ErrorNode {
return &ErrorNode{NodeType: NodeError}
}
func (n *ErrorNode) SetBody(node Node) *ErrorNode {
n.Body = node
return n
}
func (n *ErrorNode) SetDefer(node Node) *ErrorNode {
n.Defer = node
return n
}
func (n *ErrorNode) Validate() error {
switch {
case n.NodeType != NodeError:
return fmt.Errorf("Error Node uses an invalid type")
case n.Body == nil:
return fmt.Errorf("Error Node body is empty")
case n.Defer == nil:
return fmt.Errorf("Error Node defer is empty")
default:
return nil
}
}