From b4d466d811f3166d316aa65927c1cd033626337d Mon Sep 17 00:00:00 2001 From: Torin Sandall Date: Thu, 19 Jan 2017 18:14:17 -0800 Subject: [PATCH] Support new error format --- main.go | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 0763f381..c248697a 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ package main import ( "encoding/json" + "errors" "flag" "fmt" "io" @@ -104,12 +105,27 @@ func LoadPolicy(opaURL, f string) error { } if resp.StatusCode != 200 { - d := json.NewDecoder(resp.Body) + var e map[string]interface{} - if err := d.Decode(&e); err != nil { + + if err := json.NewDecoder(resp.Body).Decode(&e); err != nil { return err } - return fmt.Errorf("policy PUT failed (code: %v): %v", e["Code"], e["Message"]) + + msg := fmt.Sprintf("policy upsert failed (code %v): %v", e["code"], e["message"]) + + if errs, ok := e["errors"].([]interface{}); ok { + msg += ":\n" + for i := range errs { + bs, err := json.Marshal(errs[i]) + if err != nil { + return err + } + msg += string(bs) + "\n" + } + } + + return errors.New(msg) } return nil