Skip to content

Commit

Permalink
empty error_handler labels (#432)
Browse files Browse the repository at this point in the history
* Fix and complete error message for empty error_handler labels

* use more specific range for empty label; simpler error message

Co-authored-by: Johannes Koch <johannes.koch@avenga.com>
Co-authored-by: Johannes Koch <53434855+johakoch@users.noreply.github.com>
(cherry picked from commit 353c628)
  • Loading branch information
Alex Schneider authored and Marcel Ludwig committed Mar 3, 2022
1 parent 169506d commit 91d3c18
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
15 changes: 10 additions & 5 deletions config/configload/error_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func newErrorHandlerContent(content *hcl.BodyContent) (errorHandlerContent, erro
}

for _, block := range content.Blocks.OfType(errorHandler) {
kinds, err := newKindsFromLabels(block.Labels)
kinds, err := newKindsFromLabels(block)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -107,13 +107,18 @@ func newErrorHandlerContent(content *hcl.BodyContent) (errorHandlerContent, erro
}

// newKindsFromLabels reads two possible kind formats and returns them per slice entry.
func newKindsFromLabels(labels []string) ([]string, error) {
func newKindsFromLabels(block *hcl.Block) ([]string, error) {
var allKinds []string
for _, kinds := range labels {
for _, kinds := range block.Labels {
all := strings.Split(kinds, " ")
for _, a := range all {
for i, a := range all {
if a == "" {
return nil, errors.Configuration.Messagef("invalid format: %v", labels)
err := hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "empty error_handler label",
Subject: &block.LabelRanges[i],
}
return nil, errors.Configuration.Message(err.Error())
}
}
allKinds = append(allKinds, all...)
Expand Down
34 changes: 34 additions & 0 deletions config/configload/error_handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package configload

import (
"fmt"
"strings"
"testing"

"github.com/hashicorp/hcl/v2"
)

func TestErrorHandler_newKindsFromLabels(t *testing.T) {
b := &hcl.Block{
Labels: []string{""},
LabelRanges: []hcl.Range{
hcl.Range{
Start: hcl.Pos{
Line: 123,
Column: 321,
},
End: hcl.Pos{
Line: 123,
Column: 323,
},
},
},
}

_, err := newKindsFromLabels(b)

exp := `message:":123,321-323: empty error_handler label; ", synopsis:"configuration error"`
if got := fmt.Sprintf("%#v", err); !strings.Contains(got, exp) {
t.Errorf("Unexpected error message given: %s", got)
}
}

0 comments on commit 91d3c18

Please sign in to comment.