Skip to content

Commit

Permalink
feat: improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
hsluoyz committed Dec 7, 2023
1 parent f5e94f9 commit f6dc61a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 16 deletions.
17 changes: 14 additions & 3 deletions aliyun.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,21 @@ func (c *AliyunClient) SendMessage(param map[string]string, targetPhoneNumber ..
request.SignName = c.sign

response, err := c.core.SendSms(request)
if err != nil {
return err
}

if response.Code != "OK" {
aliyunResult := AliyunResult{}
json.Unmarshal(response.GetHttpContentBytes(), &aliyunResult)
return fmt.Errorf(aliyunResult.Message)
err = json.Unmarshal(response.GetHttpContentBytes(), &aliyunResult)
if err != nil {
return err
}

if aliyunResult.Message != "" {
return fmt.Errorf(aliyunResult.Message)
}
}
return err

return nil
}
48 changes: 35 additions & 13 deletions submail.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io/ioutil"
"mime/multipart"
"net/http"
"strings"
)

type SubmailClient struct {
Expand All @@ -36,7 +37,7 @@ type SubmailResult struct {
Msg string `json:"msg"`
}

func buildSubmailPostdata(param map[string]string, appid string, signature string, project string, targetPhoneNumber []string) map[string]string {
func buildSubmailPostdata(param map[string]string, appid string, signature string, project string, targetPhoneNumber []string) (map[string]string, error) {
multi := make([]map[string]interface{}, 0, 32)

for _, phoneNumber := range targetPhoneNumber[0:] {
Expand All @@ -46,13 +47,17 @@ func buildSubmailPostdata(param map[string]string, appid string, signature strin
})
}

m, _ := json.Marshal(multi)
m, err := json.Marshal(multi)
if err != nil {
return nil, err
}

postdata := make(map[string]string)
postdata["appid"] = appid
postdata["signature"] = signature
postdata["project"] = project
postdata["multi"] = string(m)
return postdata
return postdata, nil
}

func GetSubmailClient(appid string, signature string, project string) (*SubmailClient, error) {
Expand All @@ -66,24 +71,36 @@ func GetSubmailClient(appid string, signature string, project string) (*SubmailC
}

func (c *SubmailClient) SendMessage(param map[string]string, targetPhoneNumber ...string) error {
postdata := buildSubmailPostdata(param, c.appid, c.signature, c.project, targetPhoneNumber)
postdata, err := buildSubmailPostdata(param, c.appid, c.signature, c.project, targetPhoneNumber)
if err != nil {
return err
}

body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
for key, val := range postdata {
_ = writer.WriteField(key, val)
err = writer.WriteField(key, val)
if err != nil {
return err
}
}

contentType := writer.FormDataContentType()
writer.Close()
err = writer.Close()
if err != nil {
return err
}

resp, err := http.Post(c.api, contentType, body)
if err != nil {
return err
}

result, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}

return handleSubmailResult(result)
}

Expand All @@ -92,22 +109,27 @@ func handleSubmailResult(result []byte) error {
err := json.Unmarshal(result, &submailSuccessResult)
if err != nil {
var submailErrorResult SubmailResult
err := json.Unmarshal(result, &submailErrorResult)
err = json.Unmarshal(result, &submailErrorResult)
if err != nil {
return err
}
return fmt.Errorf(submailErrorResult.Msg)

if submailErrorResult.Msg != "" {
return fmt.Errorf(submailErrorResult.Msg)
}
}

errMsg := ""
errMsgs := []string{}
for _, submailResult := range submailSuccessResult {
if submailResult.Status != "success" {
errMsg = fmt.Sprintf("%s %s", errMsg, submailResult.Msg)
errMsg := fmt.Sprintf("%s, %d, %s", submailResult.Status, submailResult.Code, submailResult.Msg)
errMsgs = append(errMsgs, errMsg)
}
}
if errMsg != "" {
return fmt.Errorf(errMsg)

if len(errMsgs) > 0 {
return fmt.Errorf(strings.Join(errMsgs, "|"))
}

return err
return nil
}

0 comments on commit f6dc61a

Please sign in to comment.