Browse code

Make sure we error out instead of panic during interpolation

Use type assertion to error out if the type isn't the right one
instead of panic as before this change.

Signed-off-by: Vincent Demeester <vincent@sbr.pm>

Vincent Demeester authored on 2017/03/24 00:05:24
Showing 1 changed files
... ...
@@ -1,9 +1,8 @@
1 1
 package interpolation
2 2
 
3 3
 import (
4
-	"fmt"
5
-
6 4
 	"github.com/docker/docker/cli/compose/template"
5
+	"github.com/pkg/errors"
7 6
 )
8 7
 
9 8
 // Interpolate replaces variables in a string with the values from a mapping
... ...
@@ -15,7 +14,11 @@ func Interpolate(config map[string]interface{}, section string, mapping template
15 15
 			out[name] = nil
16 16
 			continue
17 17
 		}
18
-		interpolatedItem, err := interpolateSectionItem(name, item.(map[string]interface{}), section, mapping)
18
+		mapItem, ok := item.(map[string]interface{})
19
+		if !ok {
20
+			return nil, errors.Errorf("Invalid type for %s : %T instead of %T", name, item, out)
21
+		}
22
+		interpolatedItem, err := interpolateSectionItem(name, mapItem, section, mapping)
19 23
 		if err != nil {
20 24
 			return nil, err
21 25
 		}
... ...
@@ -37,7 +40,7 @@ func interpolateSectionItem(
37 37
 	for key, value := range item {
38 38
 		interpolatedValue, err := recursiveInterpolate(value, mapping)
39 39
 		if err != nil {
40
-			return nil, fmt.Errorf(
40
+			return nil, errors.Errorf(
41 41
 				"Invalid interpolation format for %#v option in %s %#v: %#v. You may need to escape any $ with another $.",
42 42
 				key, section, name, err.Template,
43 43
 			)