-
Notifications
You must be signed in to change notification settings - Fork 40
/
fail.go
67 lines (53 loc) · 1.45 KB
/
fail.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
64
65
66
67
package builtin
import (
"fmt"
"github.com/fission/fission-workflows/pkg/types"
"github.com/fission/fission-workflows/pkg/types/typedvalues"
"github.com/sirupsen/logrus"
)
const (
Fail = "fail"
FailInputMsg = types.InputMain
)
var defaultErrMsg = typedvalues.MustWrap("fail function triggered")
/*
FunctionFail is a function that always fails. This can be used to short-circuit workflows in
specific branches. Optionally you can provide a custom message to the failure.
**Specification**
**input** | required | types | description
------------|----------|--------|---------------------------------
default | no | string | custom message to show on error
**output** None
**Example**
```yaml
# ...
foo:
run: fail
inputs: "all has failed"
# ...
```
A runnable example of this function can be found in the [failwhale](../examples/whales/failwhale.wf.yaml) example.
*/
type FunctionFail struct{}
func (fn *FunctionFail) Invoke(spec *types.TaskInvocationSpec) (*typedvalues.TypedValue, error) {
var output *typedvalues.TypedValue
switch len(spec.GetInputs()) {
case 0:
output = defaultErrMsg
default:
defaultInput, ok := spec.GetInputs()[FailInputMsg]
if ok {
output = defaultInput
break
}
}
logrus.WithFields(logrus.Fields{
"spec": spec,
"output": output,
}).Info("Internal Fail-function invoked.")
msg, err := typedvalues.Unwrap(output)
if err != nil {
return nil, err
}
return nil, fmt.Errorf("%v", msg)
}