Skip to content

Commit

Permalink
Some better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
benc-uk committed Jan 1, 2023
1 parent b7d5fec commit 312c5c0
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion auth/hmac.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func SignRequestHMAC(secret string, req *http.Request) error {

key, err := base64.StdEncoding.DecodeString(secret)
if err != nil {
return err
return fmt.Errorf("error decoding secret: %s", err)
}

timestamp := time.Now().UTC().Format(http.TimeFormat)
Expand Down
8 changes: 4 additions & 4 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,20 @@ const clientTimeout = 20
func (c *Client) SendEmail(e *Email) (messageID string, err error) {
postBody, err := json.Marshal(e)
if err != nil {
return "", err
return "", fmt.Errorf("email failed JSON marshalling: %s", err)
}

bodyBuffer := bytes.NewBuffer(postBody)

req, err := http.NewRequest("POST", c.Endpoint+sendEndpoint+"?api-version="+c.APIVersion, bodyBuffer)
if err != nil {
return "", err
return "", fmt.Errorf("error creating API request: %s", err)
}

// Sign the request using the ACS access key and HMAC-SHA256
err = auth.SignRequestHMAC(c.AccessKey, req)
if err != nil {
return "", err
return "", fmt.Errorf("error signing API request: %s", err)
}

req.Header.Set("Content-Type", "application/json")
Expand All @@ -72,7 +72,7 @@ func (c *Client) SendEmail(e *Email) (messageID string, err error) {

resp, err := client.Do(req)
if err != nil {
return "", err
return "", fmt.Errorf("error sending API request: %s", err)
}
defer resp.Body.Close()

Expand Down

0 comments on commit 312c5c0

Please sign in to comment.