Skip to content

Commit

Permalink
chore: crash when API key is wrong
Browse files Browse the repository at this point in the history
  • Loading branch information
fiksn committed Nov 2, 2022
1 parent 7f79084 commit 25d0e72
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions cmd/balance-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,17 @@ func getHttpClient() *http.Client {
return httpClient
}

func shouldCrash(status int, body string) {
if status != http.StatusUnauthorized {
return
}

if strings.Contains(body, "Invalid token") {
glog.Warningf("API token is invalid")
os.Exit(1)
}
}

func infoCallback(ctx context.Context, report *agent_entities.InfoReport) bool {
rep, err := json.Marshal(report)
if err != nil {
Expand Down Expand Up @@ -361,19 +372,18 @@ func infoCallback(ctx context.Context, report *agent_entities.InfoReport) bool {
resp, err := client.Do(req)
if err != nil {
glog.Warningf("Error when doing request, %v", err)
if glog.V(2) {
glog.V(2).Infof("Failed to send out callback %s", string(rep))
}
glog.V(2).Infof("Failed to send out callback %s", string(rep))
return false
}

if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
glog.Warningf("Status was not OK but, %d", resp.StatusCode)
if glog.V(2) {
defer resp.Body.Close()
bodyData, _ := ioutil.ReadAll(resp.Body)
glog.V(2).Infof("Failed to send out callback %s, server said %s", string(rep), string(bodyData))
}
defer resp.Body.Close()
bodyData, _ := ioutil.ReadAll(resp.Body)

glog.V(2).Infof("Failed to send out callback %s, server said %s", string(rep), string(bodyData))
shouldCrash(resp.StatusCode, string(bodyData))

return false
}

Expand Down Expand Up @@ -436,11 +446,12 @@ func balanceCallback(ctx context.Context, report *agent_entities.ChannelBalanceR

if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
glog.Warningf("Status was not OK but, %d", resp.StatusCode)
if glog.V(2) {
defer resp.Body.Close()
bodyData, _ := ioutil.ReadAll(resp.Body)
glog.V(2).Infof("Failed to send out callback %s, server said %s", string(rep), string(bodyData))
}
defer resp.Body.Close()
bodyData, _ := ioutil.ReadAll(resp.Body)

glog.V(2).Infof("Failed to send out callback %s, server said %s", string(rep), string(bodyData))
shouldCrash(resp.StatusCode, string(bodyData))

return false
}

Expand Down

0 comments on commit 25d0e72

Please sign in to comment.