Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(transform): Add Customizable Errors to MetaErr #174

Merged
merged 2 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build/config/substation.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@
},
meta: {
err(settings={}): {
local default = { transform: null },
local default = { transform: null, error_messages: null },

type: 'meta_err',
settings: std.prune(std.mergePatch(default, $.helpers.abbv(settings))),
Expand Down
37 changes: 32 additions & 5 deletions transform/meta_err.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"regexp"

"github.com/brexhq/substation/config"
iconfig "github.com/brexhq/substation/internal/config"
Expand All @@ -14,6 +15,11 @@ import (
type metaErrConfig struct {
// Transform that is applied with error handling.
Transform config.Config `json:"transform"`
// ErrorMessages are regular expressions that match error messages and determine
// if the error should be caught.
//
// This is optional and defaults to an empty list (all errors are caught).
ErrorMessages []string `json:"error_messages"`
}

func (c *metaErrConfig) Decode(in interface{}) error {
Expand Down Expand Up @@ -43,9 +49,20 @@ func newMetaErr(ctx context.Context, cfg config.Config) (*metaErr, error) {
return nil, fmt.Errorf("transform: meta_err: %v", err)
}

errMsgs := make([]*regexp.Regexp, len(conf.ErrorMessages))
for i, eMsg := range conf.ErrorMessages {
r, err := regexp.Compile(eMsg)
if err != nil {
return nil, fmt.Errorf("transform: meta_err: %v", err)
}

errMsgs[i] = r
}

meta := metaErr{
conf: conf,
tf: tf,
conf: conf,
tf: tf,
errorMessages: errMsgs,
}

return &meta, nil
Expand All @@ -54,14 +71,24 @@ func newMetaErr(ctx context.Context, cfg config.Config) (*metaErr, error) {
type metaErr struct {
conf metaErrConfig

tf Transformer
tf Transformer
errorMessages []*regexp.Regexp
}

func (tf *metaErr) Transform(ctx context.Context, msg *message.Message) ([]*message.Message, error) {
msgs, err := tf.tf.Transform(ctx, msg)
if err != nil {
//nolint: nilerr // ignore non-nil error
return []*message.Message{msg}, nil
if len(tf.errorMessages) == 0 {
return []*message.Message{msg}, nil
}

for _, e := range tf.errorMessages {
if e.MatchString(err.Error()) {
return []*message.Message{msg}, nil
}
}

return nil, fmt.Errorf("transform: meta_err: %v", err)
}

return msgs, nil
Expand Down
40 changes: 40 additions & 0 deletions transform/meta_err_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,46 @@ var metaErrTests = []struct {
[]byte(`{"a":"b"}`),
},
},
{
"error_messages string",
config.Config{
Settings: map[string]interface{}{
"transform": config.Config{
Settings: map[string]interface{}{
"message": "test error",
},
Type: "utility_err",
},
"error_messages": []string{
"test error",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests: we should consider adding an additional test with regular expressions in the error _messages.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean a regular expression with special characters? These are all regular expressions.

Copy link
Contributor

@shellcromancer shellcromancer May 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I meant with special characters such as wildcard globs. This way any future refactor or changes in this processor that attempt another approach like strings.Contains(err.Error(), error_message) to do the matching don't regress in functionality.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yep, that's a good idea.

},
},
},
[]byte(`{"a":"b"}`),
[][]byte{
[]byte(`{"a":"b"}`),
},
},
{
"error_messages regex",
config.Config{
Settings: map[string]interface{}{
"transform": config.Config{
Settings: map[string]interface{}{
"message": "test error",
},
Type: "utility_err",
},
"error_messages": []string{
"^test",
},
},
},
[]byte(`{"a":"b"}`),
[][]byte{
[]byte(`{"a":"b"}`),
},
},
}

func TestMetaErr(t *testing.T) {
Expand Down
Loading