Skip to content

Commit

Permalink
fix: 处理错误
Browse files Browse the repository at this point in the history
  • Loading branch information
Clov614 committed Jul 22, 2024
1 parent 01d53d7 commit 3fff4d4
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 11 deletions.
11 changes: 8 additions & 3 deletions rikkabot/onebot/dto/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@
package event

import (
"fmt"
"errors"
"github.com/google/uuid"
"sync"
"time"
"wechat-demo/rikkabot/message"
"wechat-demo/rikkabot/utils/timeutil"
)

var (
ErrEventPoolFull = errors.New("EventPool is full")
ErrNoEventIn = errors.New("no events in pool")
)

type IEvent interface{}

type Event struct {
Expand Down Expand Up @@ -111,7 +116,7 @@ func (ep *EventPool) AddEvent(event IEvent) error {
case ep.Events <- event:
return nil
default:
return fmt.Errorf("EventPool is full")
return ErrEventPoolFull
}
}

Expand All @@ -121,7 +126,7 @@ func (ep *EventPool) GetEvent() (IEvent, error) {
case event := <-ep.Events:
return event, nil
default:
return Event{}, fmt.Errorf("no events in pool")
return Event{}, ErrNoEventIn
}
}

Expand Down
11 changes: 7 additions & 4 deletions rikkabot/onebot/httpapi/httpapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func (c HttpClient) Run() {
c.client = &http.Client{
Timeout: time.Duration(c.timeout) * time.Second,
}
logging.Info(fmt.Sprintf("Http Post 上报器已启动!%s", c.postUrl))
logging.Info("Http Post 上报器已启动!" + c.postUrl)
// 注册事件处理
c.bot.OnEventPush(c.HandlerPostEvent)
}
Expand Down Expand Up @@ -301,14 +301,17 @@ func HandlerHeartBeat(bot *rikkabot.RikkaBot) {
// HandlerPostEvent 处理 post 事件
func (c HttpClient) HandlerPostEvent(event event.IEvent) {
// todo 失败的请求根据 MaxRetries 重试
eventJSON, _ := json.Marshal(event)
eventJSON, err := json.Marshal(event)
if err != nil {
logging.Error("marshal event failed", map[string]interface{}{"err": err})
}

req, err := http.NewRequest("POST", c.postUrl, bytes.NewBuffer(eventJSON))
if err != nil {
logHttpPostError(event, err, "request create failed")
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", encrypt(c.secret)))
req.Header.Set("Authorization", "Bearer "+encrypt(c.secret))

resp, err := c.client.Do(req)
if err != nil {
Expand All @@ -331,7 +334,7 @@ func (c HttpClient) HandlerPostEvent(event event.IEvent) {
logHttpPostError(event, nil, "response status code not 200")
}

logging.Debug(fmt.Sprintf("response body: %s", string(body)), map[string]interface{}{"event": event})
logging.Debug("response body: "+string(body), map[string]interface{}{"event": event})
}

func logHttpPostError(event event.IEvent, err error, msg string) {
Expand Down
6 changes: 6 additions & 0 deletions rikkabot/utils/configutil/configUtil.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ func SaveConfig(data []byte, path string, filename string) error {
return fmt.Errorf("error path load: %w", err)
}
err = os.WriteFile(path, data, 0644)
if err != nil {
return fmt.Errorf("error path save: %w", err)
}
return nil
}

Expand All @@ -45,6 +48,9 @@ func LoadConfig(v interface{}, path string, filename string) error {
return fmt.Errorf("error loading config file: %w", err)
}
err = yaml.Unmarshal(data, v)
if err != nil {
return fmt.Errorf("error parsing config file: %w", err)
}
return nil
}

Expand Down
17 changes: 13 additions & 4 deletions rikkabot/utils/imgutil/imgutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package imgutil

import (
"fmt"
"io"
"net/http"
"os"
Expand All @@ -26,18 +27,26 @@ func isURL(path string) bool {
func fetchFromURL(url string) ([]byte, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
return nil, fmt.Errorf("fetchFromURL: http.Get(%q): %w", url, err)
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
bytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("fetchFromURL: resp.Body.ReadAll(): %w", err)
}
return bytes, nil
}

// fetchFromFile fetches the content from the file
func fetchFromFile(filePath string) ([]byte, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, err
return nil, fmt.Errorf("fetchFromFile: os.Open(%q): %w", filePath, err)
}
defer file.Close()
return io.ReadAll(file)
bytes, err := io.ReadAll(file)
if err != nil {
return nil, fmt.Errorf("fetchFromFile: io.ReadAll(file): %w", err)
}
return bytes, nil
}
3 changes: 3 additions & 0 deletions rikkabot/utils/serializer/serializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ func Save(path string, filename string, v interface{}) error {
return err
}
err = os.WriteFile(path, data, 0644)
if err != nil {
return fmt.Errorf("save Error: %w", err)
}
return nil
}

Expand Down

0 comments on commit 3fff4d4

Please sign in to comment.