mirror of
https://github.com/gogrlx/nats-server.git
synced 2026-04-02 03:38:42 -07:00
1: Improves error reporting for weighted mappings:
As it was, any error in a weighted mapping would return a very unhelpfull error message.
e.g. `nats-server: mappingtest.cfg:38:39: interface conversion: interface {} is []interface {}, not string`
This was because the line `err := &configErr{tk, fmt.Sprintf("Error adding mapping for %q to %q : %v", subj, v.(string), err)}` would panic on the `v.(string)` since in weighted mapping that interface{} is actually a map[string]interface{} (since there's can be more than one mapping in weighted mappings).
Now returns the actual error:
e.g. `nats-server: mappingtest.cfg:40:3: Error adding mapping for "bla" : invalid mapping destination: wildcard index out of range in {{wildcard(1)}}`
2: improves subject transform checking and catches if the destination is using a mapping function and there are no partial wildcards in the source.
Signed-off-by: Jean-Noël Moyne <jnmoyne@gmail.com>
This commit is contained in:
@@ -2690,7 +2690,7 @@ func parseAccountMappings(v interface{}, acc *Account, errors *[]error, warnings
|
||||
|
||||
// Now add them in..
|
||||
if err := acc.AddWeightedMappings(subj, mappings...); err != nil {
|
||||
err := &configErr{tk, fmt.Sprintf("Error adding mapping for %q to %q : %v", subj, v.(string), err)}
|
||||
err := &configErr{tk, fmt.Sprintf("Error adding mapping for %q : %v", subj, err)}
|
||||
*errors = append(*errors, err)
|
||||
continue
|
||||
}
|
||||
@@ -2702,7 +2702,7 @@ func parseAccountMappings(v interface{}, acc *Account, errors *[]error, warnings
|
||||
}
|
||||
// Now add it in..
|
||||
if err := acc.AddWeightedMappings(subj, mdest); err != nil {
|
||||
err := &configErr{tk, fmt.Sprintf("Error adding mapping for %q to %q : %v", subj, v.(string), err)}
|
||||
err := &configErr{tk, fmt.Sprintf("Error adding mapping for %q : %v", subj, err)}
|
||||
*errors = append(*errors, err)
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -114,6 +114,12 @@ func NewSubjectTransformWithStrict(src, dest string, strict bool) (*subjectTrans
|
||||
}
|
||||
}
|
||||
|
||||
if npwcs == 0 {
|
||||
if tranformType != NoTransform {
|
||||
return nil, &mappingDestinationErr{token, ErrorMappingDestinationFunctionWildcardIndexOutOfRange}
|
||||
}
|
||||
}
|
||||
|
||||
if tranformType == NoTransform {
|
||||
dtokMappingFunctionTypes = append(dtokMappingFunctionTypes, NoTransform)
|
||||
dtokMappingFunctionTokenIndexes = append(dtokMappingFunctionTokenIndexes, []int{-1})
|
||||
@@ -140,6 +146,18 @@ func NewSubjectTransformWithStrict(src, dest string, strict bool) (*subjectTrans
|
||||
// not all wildcards are being used in the destination
|
||||
return nil, &mappingDestinationErr{dest, ErrMappingDestinationNotUsingAllWildcards}
|
||||
}
|
||||
} else {
|
||||
// no wildcards used in the source: check that no transform functions are used in the destination
|
||||
for _, token := range dtokens {
|
||||
tranformType, _, _, _, err := indexPlaceHolders(token)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if tranformType != NoTransform {
|
||||
return nil, &mappingDestinationErr{token, ErrorMappingDestinationFunctionWildcardIndexOutOfRange}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return &subjectTransform{
|
||||
|
||||
@@ -149,6 +149,7 @@ func TestSubjectTransforms(t *testing.T) {
|
||||
shouldErr("foo.*", "foo.{{wildcard(1,2)}}", false) // Too many arguments passed to the mapping function
|
||||
shouldErr("foo.*", "foo.{{ wildcard5) }}", false) // Bad mapping function
|
||||
shouldErr("foo.*", "foo.{{splitLeft(2,2}}", false) // arg out of range
|
||||
shouldErr("foo", "bla.{{wildcard(1)}}", false) // arg out of range with no wildcard in the source
|
||||
|
||||
shouldBeOK := func(src, dest string, strict bool) *subjectTransform {
|
||||
t.Helper()
|
||||
|
||||
Reference in New Issue
Block a user