Browse code

Typed errors for iptables chain raw command output. YAYYYYYY.

Docker-DCO-1.1-Signed-off-by: Jessica Frazelle <jess@docker.com> (github: jfrazelle)

Jessica Frazelle authored on 2014/11/21 09:07:55
Showing 1 changed files
... ...
@@ -20,9 +20,9 @@ const (
20 20
 )
21 21
 
22 22
 var (
23
-	ErrIptablesNotFound = errors.New("Iptables not found")
24 23
 	nat                 = []string{"-t", "nat"}
25 24
 	supportsXlock       = false
25
+	ErrIptablesNotFound = errors.New("Iptables not found")
26 26
 )
27 27
 
28 28
 type Chain struct {
... ...
@@ -30,6 +30,15 @@ type Chain struct {
30 30
 	Bridge string
31 31
 }
32 32
 
33
+type ChainError struct {
34
+	Chain  string
35
+	Output []byte
36
+}
37
+
38
+func (e *ChainError) Error() string {
39
+	return fmt.Sprintf("Error iptables %s: %s", e.Chain, string(e.Output))
40
+}
41
+
33 42
 func init() {
34 43
 	supportsXlock = exec.Command("iptables", "--wait", "-L", "-n").Run() == nil
35 44
 }
... ...
@@ -78,7 +87,7 @@ func (c *Chain) Forward(action Action, ip net.IP, port int, proto, dest_addr str
78 78
 		"--to-destination", net.JoinHostPort(dest_addr, strconv.Itoa(dest_port))); err != nil {
79 79
 		return err
80 80
 	} else if len(output) != 0 {
81
-		return fmt.Errorf("Error iptables forward: %s", output)
81
+		return &ChainError{Chain: "FORWARD", Output: output}
82 82
 	}
83 83
 
84 84
 	fAction := action
... ...
@@ -94,7 +103,7 @@ func (c *Chain) Forward(action Action, ip net.IP, port int, proto, dest_addr str
94 94
 		"-j", "ACCEPT"); err != nil {
95 95
 		return err
96 96
 	} else if len(output) != 0 {
97
-		return fmt.Errorf("Error iptables forward: %s", output)
97
+		return &ChainError{Chain: "FORWARD", Output: output}
98 98
 	}
99 99
 
100 100
 	return nil
... ...
@@ -108,7 +117,7 @@ func (c *Chain) Prerouting(action Action, args ...string) error {
108 108
 	if output, err := Raw(append(a, "-j", c.Name)...); err != nil {
109 109
 		return err
110 110
 	} else if len(output) != 0 {
111
-		return fmt.Errorf("Error iptables prerouting: %s", output)
111
+		return &ChainError{Chain: "PREROUTING", Output: output}
112 112
 	}
113 113
 	return nil
114 114
 }
... ...
@@ -121,7 +130,7 @@ func (c *Chain) Output(action Action, args ...string) error {
121 121
 	if output, err := Raw(append(a, "-j", c.Name)...); err != nil {
122 122
 		return err
123 123
 	} else if len(output) != 0 {
124
-		return fmt.Errorf("Error iptables output: %s", output)
124
+		return &ChainError{Chain: "OUTPUT", Output: output}
125 125
 	}
126 126
 	return nil
127 127
 }