adjust errors and logger (#10)

This commit is contained in:
Sarv
2025-04-01 19:41:40 +08:00
committed by GitHub
parent f31953c42b
commit 167a9ca873
49 changed files with 553 additions and 981 deletions

View File

@@ -2,10 +2,10 @@ package repository
import (
"context"
"fmt"
"sort"
"strings"
"github.com/sjzar/chatlog/internal/errors"
"github.com/sjzar/chatlog/internal/model"
)
@@ -14,7 +14,7 @@ func (r *Repository) initChatRoomCache(ctx context.Context) error {
// 加载所有群聊到缓存
chatRooms, err := r.ds.GetChatRooms(ctx, "", 0, 0)
if err != nil {
return fmt.Errorf("加载群聊失败: %w", err)
return err
}
chatRoomMap := make(map[string]*model.ChatRoom)
@@ -75,7 +75,7 @@ func (r *Repository) GetChatRooms(ctx context.Context, key string, limit, offset
if key != "" {
ret = r.findChatRooms(key)
if len(ret) == 0 {
return nil, fmt.Errorf("未找到群聊: %s", key)
return nil, errors.ChatRoomNotFound(key)
}
if limit > 0 {
@@ -111,7 +111,7 @@ func (r *Repository) GetChatRooms(ctx context.Context, key string, limit, offset
func (r *Repository) GetChatRoom(ctx context.Context, key string) (*model.ChatRoom, error) {
chatRoom := r.findChatRoom(key)
if chatRoom == nil {
return nil, fmt.Errorf("未找到群聊: %s", key)
return nil, errors.ChatRoomNotFound(key)
}
return chatRoom, nil
}

View File

@@ -2,10 +2,10 @@ package repository
import (
"context"
"fmt"
"sort"
"strings"
"github.com/sjzar/chatlog/internal/errors"
"github.com/sjzar/chatlog/internal/model"
)
@@ -14,7 +14,7 @@ func (r *Repository) initContactCache(ctx context.Context) error {
// 加载所有联系人到缓存
contacts, err := r.ds.GetContacts(ctx, "", 0, 0)
if err != nil {
return fmt.Errorf("加载联系人失败: %w", err)
return err
}
contactMap := make(map[string]*model.Contact)
@@ -78,7 +78,7 @@ func (r *Repository) GetContact(ctx context.Context, key string) (*model.Contact
// 先尝试从缓存中获取
contact := r.findContact(key)
if contact == nil {
return nil, fmt.Errorf("未找到联系人: %s", key)
return nil, errors.ContactNotFound(key)
}
return contact, nil
}
@@ -88,7 +88,7 @@ func (r *Repository) GetContacts(ctx context.Context, key string, limit, offset
if key != "" {
ret = r.findContacts(key)
if len(ret) == 0 {
return nil, fmt.Errorf("未找到联系人: %s", key)
return nil, errors.ContactNotFound(key)
}
if limit > 0 {
end := offset + limit

View File

@@ -6,7 +6,7 @@ import (
"github.com/sjzar/chatlog/internal/model"
log "github.com/sirupsen/logrus"
"github.com/rs/zerolog/log"
)
// GetMessages 实现 Repository 接口的 GetMessages 方法
@@ -25,7 +25,7 @@ func (r *Repository) GetMessages(ctx context.Context, startTime, endTime time.Ti
// 补充消息信息
if err := r.EnrichMessages(ctx, messages); err != nil {
log.Debugf("EnrichMessages failed: %v", err)
log.Debug().Msgf("EnrichMessages failed: %v", err)
}
return messages, nil

View File

@@ -2,8 +2,8 @@ package repository
import (
"context"
"fmt"
"github.com/sjzar/chatlog/internal/errors"
"github.com/sjzar/chatlog/internal/model"
"github.com/sjzar/chatlog/internal/wechatdb/datasource"
)
@@ -58,7 +58,7 @@ func New(ds datasource.DataSource) (*Repository, error) {
// 初始化缓存
if err := r.initCache(context.Background()); err != nil {
return nil, fmt.Errorf("初始化缓存失败: %w", err)
return nil, errors.InitCacheFailed(err)
}
return r, nil