feat 错误包装

This commit is contained in:
2026-01-16 22:13:15 +08:00
parent 584319e5ec
commit 3d7b650b4c
3 changed files with 26 additions and 27 deletions

View File

@@ -15,8 +15,9 @@ type Server struct {
func NewServer(ttl int64) *Server {
s := &Server{
Base: service.Base{
Target: common.KeyDiscoverQgdzs,
EtcdTTL: ttl,
Target: common.KeyDiscoverQgdzs,
ServiceName: common.KeyDiscoverServiceNameQgdzs,
EtcdTTL: ttl,
},
}
s.Base.OnInit = s.OnInit

View File

@@ -0,0 +1,10 @@
package grpc_server
import (
"context"
)
// UpdateLeaderboard 更新排行榜
func (s *Server) UpdateLeaderboard(ctx context.Context) error {
return nil
}

View File

@@ -58,8 +58,7 @@ func (s *Server) GenerateQuestion(ctx context.Context, req *grpc_pb.GenerateQues
categoryDao := repository.NewCategoryDao(ctx, redis.GetCacheClient())
category, err := categoryDao.FindAll()
if err != nil {
log.Errorf("GenerateQuestion FindCategory error: %v", err)
return nil, err
return nil, utils.ErrorsWrap(err)
}
question := make([]*Question, 0)
err = ai.NewAIClient(false, "", 0.9).
@@ -77,17 +76,15 @@ func (s *Server) GenerateQuestion(ctx context.Context, req *grpc_pb.GenerateQues
return nil
}
step := make([]*Question, 0)
if err := json.Unmarshal([]byte(content), &step); err != nil {
log.Errorf("RequestAI json.Unmarshal error: %v, data: %v", err, content)
return err
if err = json.Unmarshal([]byte(content), &step); err != nil {
return utils.ErrorsWrapF(err, "data: %v", content)
}
question = append(question, step...)
return nil
},
)
if err != nil {
log.Errorf("RequestAI error: %v", err)
return nil, err
return nil, utils.ErrorsWrap(err)
}
questionDao := repository.NewQuestionDao(ctx, redis.GetCacheClient())
@@ -117,8 +114,7 @@ func (s *Server) GenerateQuestion(ctx context.Context, req *grpc_pb.GenerateQues
CategorySn: categorySn,
Difficulty: q.Difficulty,
}); err != nil {
log.Errorf("GenerateQuestion Create error: %v", err)
return nil, err
return nil, utils.ErrorsWrap(err)
}
}
return nil, nil
@@ -128,13 +124,11 @@ func (s *Server) GenerateQuestion(ctx context.Context, req *grpc_pb.GenerateQues
func (s *Server) GetQuestion(ctx context.Context, req *grpc_pb.GetQuestionReq) (*grpc_pb.GetQuestionResp, error) {
question, err := repository.NewQuestionDao(ctx).FindByRandom(req.CategorySn)
if err != nil {
log.Errorf("GetQuestion error: %v", err)
return nil, err
return nil, utils.ErrorsWrap(err)
}
options := make([]string, 0)
if err = json.Unmarshal([]byte(question.Options), &options); err != nil {
log.Errorf("GetQuestion json.Unmarshal error: %v, data: %v", err, question.Options)
return nil, err
return nil, utils.ErrorsWrapF(err, "data: %v", question.Options)
}
category, _ := repository.NewCategoryDao(ctx).FindNameBySn(question.CategorySn)
return &grpc_pb.GetQuestionResp{
@@ -150,13 +144,11 @@ func (s *Server) GetQuestion(ctx context.Context, req *grpc_pb.GetQuestionReq) (
func (s *Server) GetQuestionInfo(ctx context.Context, req *grpc_pb.GetQuestionInfoReq) (*grpc_pb.GetQuestionInfoResp, error) {
question, err := repository.NewQuestionDao(ctx).FindBySn(req.QuestionSn)
if err != nil {
log.Errorf("GetQuestionInfo error: %v", err)
return nil, err
return nil, utils.ErrorsWrap(err)
}
options := make([]string, 0)
if err = json.Unmarshal([]byte(question.Options), &options); err != nil {
log.Errorf("GetQuestionInfo json.Unmarshal error: %v, data: %v", err, question.Options)
return nil, err
return nil, utils.ErrorsWrapF(err, "data: %v", question.Options)
}
category, _ := repository.NewCategoryDao(ctx).FindNameBySn(question.CategorySn)
return &grpc_pb.GetQuestionInfoResp{
@@ -172,13 +164,11 @@ func (s *Server) GetQuestionInfo(ctx context.Context, req *grpc_pb.GetQuestionIn
func (s *Server) AnswerQuestion(ctx context.Context, req *grpc_pb.AnswerQuestionReq) (*grpc_pb.AnswerQuestionResp, error) {
question, err := repository.NewQuestionDao(ctx).FindBySn(req.Sn)
if err != nil {
log.Errorf("AnswerQuestion error: %v", err)
return nil, err
return nil, utils.ErrorsWrap(err)
}
options := make([]string, 0)
if err = json.Unmarshal([]byte(question.Options), &options); err != nil {
log.Errorf("AnswerQuestion json.Unmarshal error: %v, data: %v", err, question.Options)
return nil, err
return nil, utils.ErrorsWrapF(err, "data: %v", question.Options)
}
// 保存答题记录
if utils.ShouldBindUsn(ctx, &req.USN) {
@@ -203,8 +193,7 @@ func (s *Server) AnswerQuestion(ctx context.Context, req *grpc_pb.AnswerQuestion
func (s *Server) GetAllCategory(ctx context.Context, req *grpc_pb.GetAllCategoryReq) (*grpc_pb.GetAllCategoryResp, error) {
categoryList, err := repository.NewCategoryDao(ctx).FindAll()
if err != nil {
log.Errorf("GetAllCategory error: %v", err)
return nil, err
return nil, utils.ErrorsWrap(err)
}
categories := make([]*grpc_pb.GetAllCategoryItem, 0)
for _, category := range categoryList {
@@ -225,8 +214,7 @@ func (s *Server) GetRecord(ctx context.Context, req *grpc_pb.GetRecordReq) (*grp
}
records, count, err := repository.NewRecordDao(ctx).FindByUSN(req.USN, int(req.Page), int(req.PageSize))
if err != nil {
log.Errorf("GetRecord error: %v", err)
return nil, err
return nil, utils.ErrorsWrap(err)
}
resp := make([]*grpc_pb.GetRecordItem, 0)
for _, record := range records {