forked from benthosdev/benthos
/
processor_catch.go
145 lines (128 loc) · 4.13 KB
/
processor_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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package pure
import (
"context"
"errors"
"strconv"
"github.com/benthosdev/benthos/v4/internal/bundle"
"github.com/benthosdev/benthos/v4/internal/component/processor"
"github.com/benthosdev/benthos/v4/internal/docs"
"github.com/benthosdev/benthos/v4/internal/message"
)
func init() {
err := bundle.AllProcessors.Add(func(conf processor.Config, mgr bundle.NewManagement) (processor.V1, error) {
p, err := newCatch(conf.Catch, mgr)
if err != nil {
return nil, err
}
return processor.NewAutoObservedBatchedProcessor("catch", p, mgr), nil
}, docs.ComponentSpec{
Name: "catch",
Categories: []string{
"Composition",
},
Summary: `
Applies a list of child processors _only_ when a previous processing step has
failed.`,
Description: `
Behaves similarly to the ` + "[`for_each`](/docs/components/processors/for_each)" + ` processor, where a
list of child processors are applied to individual messages of a batch. However,
processors are only applied to messages that failed a processing step prior to
the catch.
For example, with the following config:
` + "```yaml" + `
pipeline:
processors:
- resource: foo
- catch:
- resource: bar
- resource: baz
` + "```" + `
If the processor ` + "`foo`" + ` fails for a particular message, that message
will be fed into the processors ` + "`bar` and `baz`" + `. Messages that do not
fail for the processor ` + "`foo`" + ` will skip these processors.
When messages leave the catch block their fail flags are cleared. This processor
is useful for when it's possible to recover failed messages, or when special
actions (such as logging/metrics) are required before dropping them.
More information about error handling can be found [here](/docs/configuration/error_handling).`,
Config: docs.FieldProcessor("", "").Array().
LinterFunc(func(ctx docs.LintContext, line, col int, value any) []docs.Lint {
childProcs, ok := value.([]any)
if !ok {
return nil
}
for _, child := range childProcs {
childObj, ok := child.(map[string]any)
if !ok {
continue
}
if _, exists := childObj["catch"]; exists {
// No need to lint as a nested catch will clear errors,
// allowing nested try blocks to work as expected.
return nil
}
if _, exists := childObj["try"]; exists {
return []docs.Lint{
docs.NewLintError(line, docs.LintCustom, errors.New("`catch` block contains a `try` block which will never execute due to errors only being cleared at the end of the `catch`, for more information about nesting `try` within `catch` read: https://www.benthos.dev/docs/components/processors/try#nesting-within-a-catch-block")),
}
}
}
return nil
}),
})
if err != nil {
panic(err)
}
}
//------------------------------------------------------------------------------
type catchProc struct {
children []processor.V1
}
func newCatch(conf []processor.Config, mgr bundle.NewManagement) (*catchProc, error) {
var children []processor.V1
for i, pconf := range conf {
pMgr := mgr.IntoPath("catch", strconv.Itoa(i))
proc, err := pMgr.NewProcessor(pconf)
if err != nil {
return nil, err
}
children = append(children, proc)
}
return &catchProc{
children: children,
}, nil
}
func (p *catchProc) ProcessBatch(ctx *processor.BatchProcContext, msg message.Batch) ([]message.Batch, error) {
resultMsgs := make([]message.Batch, msg.Len())
_ = msg.Iter(func(i int, p *message.Part) error {
resultMsgs[i] = message.Batch{p}
return nil
})
var res error
if resultMsgs, res = processor.ExecuteCatchAll(ctx.Context(), p.children, resultMsgs...); res != nil || len(resultMsgs) == 0 {
return nil, res
}
resMsg := message.QuickBatch(nil)
for _, m := range resultMsgs {
_ = m.Iter(func(i int, p *message.Part) error {
resMsg = append(resMsg, p)
return nil
})
}
if resMsg.Len() == 0 {
return nil, res
}
_ = resMsg.Iter(func(i int, p *message.Part) error {
p.ErrorSet(nil)
return nil
})
resMsgs := [1]message.Batch{resMsg}
return resMsgs[:], nil
}
func (p *catchProc) Close(ctx context.Context) error {
for _, child := range p.children {
if err := child.Close(ctx); err != nil {
return err
}
}
return nil
}