diff --git a/internal/logic/commentservice/get_comments_logic_test.go b/internal/logic/commentservice/get_comments_logic_test.go index 08ee6e7..be66c91 100644 --- a/internal/logic/commentservice/get_comments_logic_test.go +++ b/internal/logic/commentservice/get_comments_logic_test.go @@ -6,10 +6,11 @@ import ( "app-cloudep-tweeting-service/internal/svc" "context" "errors" + "testing" + "github.com/stretchr/testify/assert" "go.mongodb.org/mongo-driver/bson/primitive" "go.uber.org/mock/gomock" - "testing" mocklib "app-cloudep-tweeting-service/internal/mock/lib" mockmodel "app-cloudep-tweeting-service/internal/mock/model" diff --git a/internal/logic/commentservice/new_comment_logic_test.go b/internal/logic/commentservice/new_comment_logic_test.go index 68edd03..a7873ce 100644 --- a/internal/logic/commentservice/new_comment_logic_test.go +++ b/internal/logic/commentservice/new_comment_logic_test.go @@ -6,10 +6,11 @@ import ( "app-cloudep-tweeting-service/internal/svc" "context" "errors" + "testing" + "github.com/stretchr/testify/assert" "go.mongodb.org/mongo-driver/bson/primitive" "go.uber.org/mock/gomock" - "testing" mocklib "app-cloudep-tweeting-service/internal/mock/lib" mockmodel "app-cloudep-tweeting-service/internal/mock/model" diff --git a/internal/logic/commentservice/update_comment_logic_test.go b/internal/logic/commentservice/update_comment_logic_test.go index 88762c9..ec4973a 100644 --- a/internal/logic/commentservice/update_comment_logic_test.go +++ b/internal/logic/commentservice/update_comment_logic_test.go @@ -7,12 +7,13 @@ import ( "app-cloudep-tweeting-service/internal/svc" "context" "errors" + "testing" + "github.com/stretchr/testify/assert" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" "go.uber.org/mock/gomock" "google.golang.org/protobuf/proto" - "testing" ) func TestUpdateComment(t *testing.T) { diff --git a/internal/logic/postservice/create_post_logic.go b/internal/logic/postservice/create_post_logic.go index 73023e7..e3fbb0d 100644 --- a/internal/logic/postservice/create_post_logic.go +++ b/internal/logic/postservice/create_post_logic.go @@ -4,14 +4,9 @@ import ( "app-cloudep-tweeting-service/internal/domain" model "app-cloudep-tweeting-service/internal/model/mongo" - "code.30cm.net/digimon/library-go/errs/code" - - "context" - "fmt" - "strings" - "app-cloudep-tweeting-service/gen_result/pb/tweeting" "app-cloudep-tweeting-service/internal/svc" + "context" ers "code.30cm.net/digimon/library-go/errs" "github.com/zeromicro/go-zero/core/logx" @@ -41,23 +36,6 @@ type newTweetingReq struct { IsAd bool `json:"is_ad"` // default false } -// 定義 Error - -// CreatePostError 0502102 資料庫錯誤 -func CreatePostError(s ...string) *ers.LibError { - return ers.NewError(code.CloudEPTweeting, code.DBError, - domain.CreatePostError.ToUint32(), - fmt.Sprintf("%s", strings.Join(s, " "))) -} - -// CreatePostErrorL logs error message and returns Error -func CreatePostErrorL(l logx.Logger, filed []logx.LogField, s ...string) *ers.LibError { - e := CreatePostError(s...) - l.WithCallerSkip(1).WithFields(filed...).Error(e.Error()) - - return e -} - // CreatePost 新增貼文 func (l *CreatePostLogic) CreatePost(in *tweeting.NewPostReq) (*tweeting.PostResp, error) { // 驗證資料 @@ -97,7 +75,9 @@ func (l *CreatePostLogic) CreatePost(in *tweeting.NewPostReq) (*tweeting.PostRes // ============ insert ============ err := l.svcCtx.PostModel.Insert(l.ctx, tweet) if err != nil { - e := CreatePostErrorL( + // 錯誤代碼 05-021-02 + e := domain.CommentErrorL( + domain.CreatePostError, logx.WithContext(l.ctx), []logx.LogField{ {Key: "req", Value: in}, diff --git a/internal/logic/postservice/delete_post_logic.go b/internal/logic/postservice/delete_post_logic.go index 33a9e81..ffa6bda 100644 --- a/internal/logic/postservice/delete_post_logic.go +++ b/internal/logic/postservice/delete_post_logic.go @@ -1,16 +1,10 @@ package postservicelogic import ( - "app-cloudep-tweeting-service/internal/domain" - "context" - "fmt" - "strings" - - ers "code.30cm.net/digimon/library-go/errs" - "code.30cm.net/digimon/library-go/errs/code" - "app-cloudep-tweeting-service/gen_result/pb/tweeting" + "app-cloudep-tweeting-service/internal/domain" "app-cloudep-tweeting-service/internal/svc" + "context" "github.com/zeromicro/go-zero/core/logx" ) @@ -29,26 +23,13 @@ func NewDeletePostLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Delete } } -// DeletePostError 0502103 資料庫錯誤 -func DeletePostError(s ...string) *ers.LibError { - return ers.NewError(code.CloudEPTweeting, code.DBError, - domain.DelPostError.ToUint32(), - fmt.Sprintf("%s", strings.Join(s, " "))) -} - -// DeletePostErrorL logs error message and returns Error -func DeletePostErrorL(l logx.Logger, filed []logx.LogField, s ...string) *ers.LibError { - e := DeletePostError(s...) - l.WithCallerSkip(1).WithFields(filed...).Error(e.Error()) - - return e -} - // DeletePost 刪除貼文 func (l *DeletePostLogic) DeletePost(in *tweeting.DeletePostsReq) (*tweeting.OKResp, error) { _, err := l.svcCtx.PostModel.DeleteMany(l.ctx, in.GetPostId()...) if err != nil { - e := DeletePostErrorL( + // 錯誤代碼 05-021-03 + e := domain.CommentErrorL( + domain.DelPostError, logx.WithContext(l.ctx), []logx.LogField{ {Key: "req", Value: in}, diff --git a/internal/logic/postservice/list_posts_logic.go b/internal/logic/postservice/list_posts_logic.go index a3e7f08..544327d 100644 --- a/internal/logic/postservice/list_posts_logic.go +++ b/internal/logic/postservice/list_posts_logic.go @@ -4,11 +4,8 @@ import ( "app-cloudep-tweeting-service/internal/domain" model "app-cloudep-tweeting-service/internal/model/mongo" "context" - "fmt" - "strings" ers "code.30cm.net/digimon/library-go/errs" - "code.30cm.net/digimon/library-go/errs/code" "google.golang.org/protobuf/proto" "app-cloudep-tweeting-service/gen_result/pb/tweeting" @@ -38,21 +35,6 @@ type listReq struct { PageIndex int64 `json:"page_index" validate:"required"` } -// ListPostError 0502105 資料庫錯誤 -func ListPostError(s ...string) *ers.LibError { - return ers.NewError(code.CloudEPTweeting, code.DBError, - domain.ListPostError.ToUint32(), - fmt.Sprintf("%s", strings.Join(s, " "))) -} - -// ListPostErrorL logs error message and returns Error -func ListPostErrorL(l logx.Logger, filed []logx.LogField, s ...string) *ers.LibError { - e := ListPostError(s...) - l.WithCallerSkip(1).WithFields(filed...).Error(e.Error()) - - return e -} - // 將單個 Post 轉換為 PostDetailItem func convertToPostDetailItem(item *model.Post) *tweeting.PostDetailItem { media := make([]*tweeting.Media, 0, len(item.MediaURL)) @@ -110,7 +92,9 @@ func (l *ListPostsLogic) ListPosts(in *tweeting.QueryPostsReq) (*tweeting.ListPo // 執行查詢 find, count, err := l.svcCtx.PostModel.Find(l.ctx, query) if err != nil { - e := ListPostErrorL( + // 錯誤代碼 05-021-05 + e := domain.CommentErrorL( + domain.ListPostError, logx.WithContext(l.ctx), []logx.LogField{ {Key: "query", Value: query}, diff --git a/internal/logic/postservice/update_post_logic.go b/internal/logic/postservice/update_post_logic.go index 4343e89..2275fcc 100644 --- a/internal/logic/postservice/update_post_logic.go +++ b/internal/logic/postservice/update_post_logic.go @@ -4,11 +4,8 @@ import ( "app-cloudep-tweeting-service/internal/domain" model "app-cloudep-tweeting-service/internal/model/mongo" "context" - "fmt" - "strings" ers "code.30cm.net/digimon/library-go/errs" - "code.30cm.net/digimon/library-go/errs/code" "go.mongodb.org/mongo-driver/bson/primitive" "app-cloudep-tweeting-service/gen_result/pb/tweeting" @@ -36,21 +33,6 @@ type checkPostId struct { Content string `json:"content,omitempty" validate:"lte=500"` } -// UpdatePostError 0502104 資料庫錯誤 -func UpdatePostError(s ...string) *ers.LibError { - return ers.NewError(code.CloudEPTweeting, code.DBError, - domain.UpdatePostError.ToUint32(), - fmt.Sprintf("%s", strings.Join(s, " "))) -} - -// UpdatePostErrorL logs error message and returns Error -func UpdatePostErrorL(l logx.Logger, filed []logx.LogField, s ...string) *ers.LibError { - e := UpdatePostError(s...) - l.WithCallerSkip(1).WithFields(filed...).Error(e.Error()) - - return e -} - // UpdatePost 更新貼文 func (l *UpdatePostLogic) UpdatePost(in *tweeting.UpdatePostReq) (*tweeting.OKResp, error) { // 驗證資料 @@ -96,7 +78,9 @@ func (l *UpdatePostLogic) UpdatePost(in *tweeting.UpdatePostReq) (*tweeting.OKRe _, err = l.svcCtx.PostModel.UpdateOptional(l.ctx, &update) if err != nil { - e := UpdatePostErrorL( + // 錯誤代碼 05-021-04 + e := domain.CommentErrorL( + domain.UpdatePostError, logx.WithContext(l.ctx), []logx.LogField{ {Key: "req", Value: in},