Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: print error messages when an API request fails #1640

Merged
merged 1 commit into from
Feb 5, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions tools/delete_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ type auth struct {
Password string `json:"password"`
}

func logAndQuit(fmt string, args ...interface{}) {
log.Printf(fmt, args...)
os.Exit(0)
}

func main() {
var (
auth auth
Expand Down Expand Up @@ -51,12 +56,12 @@ func main() {
// Get an auth token
jwt, err := getJWT(auth)
if err != nil {
log.Fatalln(err)
logAndQuit(err.Error())
}

tags, err := getTags(jwt, repo)
if err != nil {
log.Fatalln(err)
logAndQuit(err.Error())
}

log.Printf("Discovered %d tags pre-filtering\n", len(tags))
Expand Down Expand Up @@ -108,7 +113,7 @@ func getJWT(a auth) (string, error) {
return "", err
}
resp.Body.Close()
log.Fatalf("failed to log in: %v", string(body))
return "", fmt.Errorf("failed to log in: %v", string(body))
}
defer resp.Body.Close()

Expand Down Expand Up @@ -165,11 +170,16 @@ func getTagsFromURL(jwt string, url string) (getTagResponse, error) {
if err != nil {
return res, err
}
if resp.StatusCode != 200 {
return res, errors.New("failed to get tags")
}
defer resp.Body.Close()

if resp.StatusCode/100 != 2 {
bb, err := ioutil.ReadAll(resp.Body)
if err != nil {
return res, err
}
return res, errors.New(string(bb))
}

err = json.NewDecoder(resp.Body).Decode(&res)
return res, err
}
Expand All @@ -188,10 +198,13 @@ func deleteTag(jwt string, repo string, tag string) error {
}
defer resp.Body.Close()

bb, err := ioutil.ReadAll(resp.Body)
if resp.StatusCode/100 != 2 {
return fmt.Errorf("resp code %d: %s", resp.StatusCode, string(bb))
bb, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return errors.New(string(bb))
}

return err
return nil
}