-
Notifications
You must be signed in to change notification settings - Fork 14
/
utility_err.go
50 lines (39 loc) · 1.04 KB
/
utility_err.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
package transform
import (
"context"
"encoding/json"
"fmt"
"github.com/brexhq/substation/config"
iconfig "github.com/brexhq/substation/internal/config"
"github.com/brexhq/substation/message"
)
type utilityErrConfig struct {
// Message is the error message to return.
Message string `json:"message"`
}
func (c *utilityErrConfig) Decode(in interface{}) error {
return iconfig.Decode(in, c)
}
func newUtilityErr(_ context.Context, cfg config.Config) (*utilityErr, error) {
conf := utilityErrConfig{}
if err := conf.Decode(cfg.Settings); err != nil {
return nil, fmt.Errorf("transform: utility_err: %v", err)
}
tf := utilityErr{
conf: conf,
}
return &tf, nil
}
type utilityErr struct {
conf utilityErrConfig
}
func (tf *utilityErr) Transform(_ context.Context, msg *message.Message) ([]*message.Message, error) {
if msg.IsControl() {
return []*message.Message{msg}, nil
}
return []*message.Message{msg}, fmt.Errorf("%s", tf.conf.Message)
}
func (tf *utilityErr) String() string {
b, _ := json.Marshal(tf.conf)
return string(b)
}