Skip to content

Commit

Permalink
feat: fix volcengine error handling (#37)
Browse files Browse the repository at this point in the history
* bugfix: fixed volcengine sdk component error handling

* Update volcengine.go

---------

Co-authored-by: hsluoyz <[email protected]>
  • Loading branch information
maratrixx and hsluoyz authored Dec 22, 2023
1 parent 9195708 commit 95d3d8b
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions volcengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
package go_sms_sender

import (
"bytes"
"encoding/json"
"fmt"
"strings"

"github.com/volcengine/volc-sdk-golang/service/sms"
)
Expand Down Expand Up @@ -58,20 +58,24 @@ func (c *VolcClient) SendMessage(param map[string]string, targetPhoneNumber ...s
return fmt.Errorf("missing parameter: targetPhoneNumber")
}

phoneNumbers := bytes.Buffer{}
phoneNumbers.WriteString(targetPhoneNumber[0])
for _, s := range targetPhoneNumber[1:] {
phoneNumbers.WriteString(",")
phoneNumbers.WriteString(s)
}

req := &sms.SmsRequest{
SmsAccount: c.smsAccount,
Sign: c.sign,
TemplateID: c.template,
TemplateParam: string(requestParam),
PhoneNumbers: phoneNumbers.String(),
PhoneNumbers: strings.Join(targetPhoneNumber, ","),
}

resp, statusCode, err := c.core.Send(req)
if err != nil {
return fmt.Errorf("send message failed, error: %q", err.Error())
}
if statusCode < 200 || statusCode > 299 {
return fmt.Errorf("send message failed, statusCode :%d", statusCode)
}
_, _, err = c.core.Send(req)
return err
if resp.ResponseMetadata.Error != nil {
return fmt.Errorf("send message failed, code: %q, message: %q", resp.ResponseMetadata.Error.Code, resp.ResponseMetadata.Error.Message)
}

return nil
}

0 comments on commit 95d3d8b

Please sign in to comment.