/
program.go
94 lines (72 loc) · 1.65 KB
/
program.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package runtime
import (
"context"
"runtime"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/logging"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/pkg/errors"
)
type Program struct {
src string
body core.Expression
}
func NewProgram(src string, body core.Expression) (*Program, error) {
if src == "" {
return nil, core.Error(core.ErrMissedArgument, "source")
}
if body == nil {
return nil, core.Error(core.ErrMissedArgument, "body")
}
return &Program{src, body}, nil
}
func (p *Program) Source() string {
return p.src
}
func (p *Program) Run(ctx context.Context, setters ...Option) (result []byte, err error) {
ctx = NewOptions(setters).WithContext(ctx)
logger := logging.FromContext(ctx)
defer func() {
if r := recover(); r != nil {
// find out exactly what the error was and set err
switch x := r.(type) {
case string:
err = errors.New(x)
case error:
err = x
default:
err = errors.New("unknown panic")
}
b := make([]byte, 0, 20)
runtime.Stack(b, true)
logger.Error().
Timestamp().
Err(err).
Str("stack", string(b)).
Msg("Panic")
result = nil
}
}()
scope, closeFn := core.NewRootScope()
defer func() {
if err := closeFn(); err != nil {
logger.Error().
Timestamp().
Err(err).
Msg("closing root scope")
}
}()
out, err := p.body.Exec(ctx, scope)
if err != nil {
js, _ := values.None.MarshalJSON()
return js, err
}
return out.MarshalJSON()
}
func (p *Program) MustRun(ctx context.Context, setters ...Option) []byte {
out, err := p.Run(ctx, setters...)
if err != nil {
panic(err)
}
return out
}