Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
sh7ning committed Nov 12, 2019
1 parent 2d93dae commit e618aa8
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,14 @@ func (robot *Robot) SendMessage(msg interface{}) error {
if err != nil {
return fmt.Errorf("send dingTalk message failed, error: %v", err.Error())
}
defer func() { _ = res.Body.Close() }()
result, err := ioutil.ReadAll(res.Body)

if res.StatusCode != 200 {
return fmt.Errorf("send dingTalk message failed, status code: %v", res.StatusCode)
return fmt.Errorf("send dingTalk message failed, %s", httpError(request, res, result, "http code is not 200"))
}

defer func() { _ = res.Body.Close() }()
result, err := ioutil.ReadAll(res.Body)
if err != nil {
return fmt.Errorf("send dingTalk message failed, error: %v", err.Error())
return fmt.Errorf("send dingTalk message failed, %s", httpError(request, res, result, err.Error()))
}

type response struct {
Expand All @@ -74,16 +73,27 @@ func (robot *Robot) SendMessage(msg interface{}) error {
var ret response

if err := json.Unmarshal(result, &ret); err != nil {
return fmt.Errorf("send dingTalk message failed, result: %s, error: %v", result, err.Error())
return fmt.Errorf("send dingTalk message failed, %s", httpError(request, res, result, err.Error()))
}

if ret.ErrCode != 0 {
return fmt.Errorf("send dingTalk message failed, result: %s", result)
return fmt.Errorf("send dingTalk message failed, %s", httpError(request, res, result, "errcode is not 0"))
}

return nil
}

func httpError(request *http.Request, response *http.Response, body []byte, error string) string {
return fmt.Sprintf(
"http request failure, error: %s, status code: %d, %s %s, body:\n%s",
error,
response.StatusCode,
request.Method,
request.URL.String(),
string(body),
)
}

func (robot *Robot) SendTextMessage(content string, atMobiles []string, isAtAll bool) error {
msg := map[string]interface{}{
"msgtype": "text",
Expand Down

0 comments on commit e618aa8

Please sign in to comment.