feat 错误包装
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user