Propagate json error object to the caller

When plugin errors out, it prints out a JSON object to stdout
describing the failure. This object needs to be propagated out
through the plugins and to the container runtime. This change
also adds Print method to both the result and error structs
for easy serialization to stdout.
This commit is contained in:
Eugene Yakubovich
2015-06-01 17:34:00 -07:00
parent a6a822268c
commit 31c0dbf110
3 changed files with 64 additions and 28 deletions

View File

@ -17,6 +17,7 @@ package plugin
import (
"encoding/json"
"net"
"os"
"github.com/appc/cni/pkg/ip"
)
@ -36,6 +37,10 @@ type Result struct {
IP6 *IPConfig `json:"ip6,omitempty"`
}
func (r *Result) Print() error {
return prettyPrint(r)
}
// IPConfig contains values necessary to configure an interface
type IPConfig struct {
IP net.IPNet
@ -54,6 +59,14 @@ type Error struct {
Details string `json:"details,omitempty"`
}
func (e *Error) Error() string {
return e.Msg
}
func (e *Error) Print() error {
return prettyPrint(e)
}
// net.IPNet is not JSON (un)marshallable so this duality is needed
// for our custom ip.IPNet type
@ -110,3 +123,12 @@ func (r *Route) MarshalJSON() ([]byte, error) {
return json.Marshal(rt)
}
func prettyPrint(obj interface{}) error {
data, err := json.MarshalIndent(obj, "", " ")
if err != nil {
return err
}
_, err = os.Stdout.Write(data)
return err
}