-
Notifications
You must be signed in to change notification settings - Fork 152
/
catch.go
49 lines (41 loc) · 1.29 KB
/
catch.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
package experimental
import (
"context"
"github.com/influxdata/flux"
"github.com/influxdata/flux/interpreter"
"github.com/influxdata/flux/runtime"
"github.com/influxdata/flux/semantic"
"github.com/influxdata/flux/values"
)
const CatchKind = "catch"
const pkg = "experimental"
func init() {
runtime.RegisterPackageValue(pkg, CatchKind, Catch())
}
func Catch() values.Function {
return values.NewFunction(
CatchKind,
runtime.MustLookupBuiltinType(pkg, CatchKind),
func(ctx context.Context, args values.Object) (values.Value, error) {
return interpreter.DoFunctionCallContext(func(ctx context.Context, args interpreter.Arguments) (values.Value, error) {
fn, err := args.GetRequiredFunction("fn")
if err != nil {
return nil, err
}
value, err := fn.Call(ctx, values.NewObject(semantic.NewObjectType(nil)))
if err == nil {
return values.NewObjectWithValues(map[string]values.Value{
"value": value,
"code": values.NewNull(semantic.BasicUint),
"msg": values.NewNull(semantic.BasicString),
}), nil
}
return values.NewObjectWithValues(map[string]values.Value{
"value": values.Null,
"code": values.NewUInt(uint64(flux.ErrorCode(err))),
"msg": values.NewString(err.Error()),
}), nil
}, ctx, args)
}, false,
)
}