Skip to content

Commit

Permalink
Merge pull request #1 from Deft-s/main
Browse files Browse the repository at this point in the history
return error instead of exit the whole process when error occurd
  • Loading branch information
bensch777 authored Apr 8, 2023
2 parents 286f369 + 6e97011 commit d885452
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions discordwebhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package discordwebhook
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
)
Expand Down Expand Up @@ -57,26 +57,26 @@ func ExecuteWebhook(link string, data []byte) error {

req, err := http.NewRequest("POST", link, bytes.NewBuffer(data))
if err != nil {
log.Fatal(err)
return err
}
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
return err
}
defer resp.Body.Close()
bodyText, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
return err
}
if resp.StatusCode != 200 {
fmt.Printf("%s\n", bodyText)
return errors.New(fmt.Sprintf("%s\n", bodyText))
}
if resp.StatusCode == 429 {
fmt.Println("Rate limit reached")
time.Sleep(time.Second * 5)
ExecuteWebhook(link, data)
return ExecuteWebhook(link, data)
}
return err
}
Expand All @@ -87,9 +87,9 @@ func SendEmbed(link string, embeds Embed) error {
}
payload, err := json.Marshal(hook)
if err != nil {
log.Fatal(err)
return err
}
err = ExecuteWebhook(link, payload)
return err

}
}

0 comments on commit d885452

Please sign in to comment.