Skip to content

Commit 26b2cc4

Browse files
author
lenye
committed
fix: feishu
1 parent 3cbb084 commit 26b2cc4

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

im/feishu/bot/message.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,15 @@ const sendURL = "https://open.feishu.cn/open-apis/bot/v2/hook/"
6060
func Send(accessToken string, msg *Message) error {
6161
u := sendURL + accessToken
6262
var resp feishu.ResponseMeta
63-
_, err := client.PostJSON(u, msg, &resp)
63+
headers, err := client.PostJSON(u, msg, &resp)
6464
if err != nil {
65-
return err
65+
if headers == nil {
66+
return err
67+
}
68+
return fmt.Errorf("%w, %s", err, resp.String())
6669
}
6770
if !resp.Succeed() {
68-
return fmt.Errorf("%w, %s", feishu.ErrRequest, resp)
71+
return fmt.Errorf("%s", resp.String())
6972
}
7073
return nil
7174
}

im/feishu/bot/message_cmd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func CmdSend(arg *CmdSendParams) error {
9494
httpclient.SetUserAgent(arg.UserAgent)
9595

9696
if err := Send(arg.AccessToken, &msg); err != nil {
97-
return err
97+
return fmt.Errorf("%w, %w", feishu.ErrRequest, err)
9898
}
9999
fmt.Println(feishu.MessageOK)
100100

im/feishu/client/client.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ import (
2424
"github.com/lenye/pmsg/im"
2525
)
2626

27-
const contentTypeJson = "application/json"
28-
2927
func PostJSON(url string, reqBody, respBody any) (http.Header, error) {
3028
body, err := im.JsonEncode(reqBody)
3129
if err != nil {
@@ -34,20 +32,23 @@ func PostJSON(url string, reqBody, respBody any) (http.Header, error) {
3432

3533
resp, err := httpclient.Post(url, httpclient.HdrValApplicationJson, body)
3634
if err != nil {
37-
return nil, fmt.Errorf("%w, %w", httpclient.ErrRequest, err)
35+
return nil, err
3836
}
3937
defer func(Body io.ReadCloser) {
4038
_ = Body.Close()
4139
}(resp.Body)
4240

43-
isRespJson := strings.EqualFold(resp.Header.Get("content-type"), contentTypeJson)
44-
if isRespJson {
45-
if err := im.JsonDecode(resp.Body, respBody); err != nil {
46-
return nil, fmt.Errorf("json decode failed, %w", err)
47-
}
41+
// "application/json" 响应内容类型
42+
// "application/json; charset=utf-8" 错误响应内容类型
43+
contentType := resp.Header.Get("content-type")
44+
if !strings.EqualFold(contentType, httpclient.HdrValApplicationJson) && !strings.EqualFold(contentType, httpclient.HdrValApplicationJsonCharset) {
45+
return nil, fmt.Errorf("http.response.header.content-type != %s", httpclient.HdrValApplicationJson)
46+
}
47+
if err := im.JsonDecode(resp.Body, respBody); err != nil {
48+
return nil, fmt.Errorf("http.response.body json decode failed, %w", err)
4849
}
49-
if resp.StatusCode/100 != 2 {
50-
return nil, fmt.Errorf("%w, http response status: %s", httpclient.ErrRequest, resp.Status)
50+
if resp.StatusCode != http.StatusOK {
51+
return resp.Header, fmt.Errorf("invalid http.response.status: %s", resp.Status)
5152
}
5253
return resp.Header, nil
5354
}

0 commit comments

Comments
 (0)