Skip to content

Commit

Permalink
Support new error format
Browse files Browse the repository at this point in the history
  • Loading branch information
tsandall committed Jan 20, 2017
1 parent e796d3b commit b4d466d
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package main

import (
"encoding/json"
"errors"
"flag"
"fmt"
"io"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b4d466d

Please sign in to comment.