mirror of https://gitee.com/answerdev/answer.git
style(simple): Properly handle unhandled errors
This commit is contained in:
parent
a4ad6b7fad
commit
6e1f4841f1
|
@ -7,6 +7,7 @@ import (
|
|||
"github.com/answerdev/answer/internal/service"
|
||||
"github.com/answerdev/answer/internal/service/siteinfo_common"
|
||||
"github.com/robfig/cron/v3"
|
||||
"github.com/segmentfault/pacman/log"
|
||||
)
|
||||
|
||||
// ScheduledTaskManager scheduled task manager
|
||||
|
@ -31,10 +32,13 @@ func (s *ScheduledTaskManager) Run() {
|
|||
fmt.Println("start cron")
|
||||
s.questionService.SitemapCron(context.Background())
|
||||
c := cron.New()
|
||||
c.AddFunc("0 */1 * * *", func() {
|
||||
_, err := c.AddFunc("0 */1 * * *", func() {
|
||||
ctx := context.Background()
|
||||
fmt.Println("sitemap cron execution")
|
||||
s.questionService.SitemapCron(ctx)
|
||||
})
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
c.Start()
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@ import (
|
|||
"github.com/answerdev/answer/internal/service/uploader"
|
||||
"github.com/answerdev/answer/pkg/converter"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/segmentfault/pacman/log"
|
||||
)
|
||||
|
||||
type AvatarMiddleware struct {
|
||||
|
@ -52,7 +53,10 @@ func (am *AvatarMiddleware) AvatarThumb() gin.HandlerFunc {
|
|||
ctx.Next()
|
||||
return
|
||||
}
|
||||
ctx.Writer.WriteString(string(avatarfile))
|
||||
_, err = ctx.Writer.WriteString(string(avatarfile))
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
ctx.Abort()
|
||||
return
|
||||
|
||||
|
|
|
@ -281,9 +281,7 @@ func (qc *QuestionController) AddQuestion(ctx *gin.Context) {
|
|||
if err != nil {
|
||||
errlist, ok := resp.([]*validator.FormErrorField)
|
||||
if ok {
|
||||
for _, item := range errlist {
|
||||
errFields = append(errFields, item)
|
||||
}
|
||||
errFields = append(errFields, errlist...)
|
||||
}
|
||||
}
|
||||
if len(errFields) > 0 {
|
||||
|
@ -335,9 +333,7 @@ func (qc *QuestionController) UpdateQuestion(ctx *gin.Context) {
|
|||
|
||||
errlist, err := qc.questionService.UpdateQuestionCheckTags(ctx, req)
|
||||
if err != nil {
|
||||
for _, item := range errlist {
|
||||
errFields = append(errFields, item)
|
||||
}
|
||||
errFields = append(errFields, errlist...)
|
||||
}
|
||||
|
||||
if len(errFields) > 0 {
|
||||
|
|
|
@ -19,6 +19,9 @@ func (q *TemplateRenderController) TagInfo(ctx context.Context, req *schema.GetT
|
|||
dto := &schema.GetTagInfoReq{}
|
||||
_ = copier.Copy(dto, req)
|
||||
resp, err = q.tagService.GetTagInfo(ctx, dto)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
searchQuestion := &schema.QuestionSearch{}
|
||||
searchQuestion.Page = req.Page
|
||||
searchQuestion.PageSize = req.PageSize
|
||||
|
|
|
@ -15,6 +15,7 @@ import (
|
|||
"github.com/answerdev/answer/internal/service/uploader"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/segmentfault/pacman/errors"
|
||||
"github.com/segmentfault/pacman/log"
|
||||
)
|
||||
|
||||
// UserController user controller
|
||||
|
@ -294,11 +295,13 @@ func (uc *UserController) UserVerifyEmailSend(ctx *gin.Context) {
|
|||
ErrorMsg: translator.GlobalTrans.Tr(handler.GetLang(ctx), reason.CaptchaVerificationFailed),
|
||||
})
|
||||
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), errFields)
|
||||
|
||||
return
|
||||
}
|
||||
uc.actionService.ActionRecordAdd(ctx, schema.ActionRecordTypeEmail, ctx.ClientIP())
|
||||
err := uc.userService.UserVerifyEmailSend(ctx, userInfo.UserID)
|
||||
_, err := uc.actionService.ActionRecordAdd(ctx, schema.ActionRecordTypeEmail, ctx.ClientIP())
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
err = uc.userService.UserVerifyEmailSend(ctx, userInfo.UserID)
|
||||
handler.HandleResponse(ctx, err, nil)
|
||||
}
|
||||
|
||||
|
|
|
@ -27,15 +27,12 @@ type AnswerActivityRepo struct {
|
|||
}
|
||||
|
||||
const (
|
||||
acceptAction = "accept"
|
||||
acceptedAction = "accepted"
|
||||
acceptCancelAction = "accept_cancel"
|
||||
acceptedCancelAction = "accepted_cancel"
|
||||
acceptAction = "accept"
|
||||
acceptedAction = "accepted"
|
||||
)
|
||||
|
||||
var (
|
||||
acceptActionList = []string{acceptAction, acceptedAction}
|
||||
acceptCancelActionList = []string{acceptCancelAction, acceptedCancelAction}
|
||||
acceptActionList = []string{acceptAction, acceptedAction}
|
||||
)
|
||||
|
||||
// NewAnswerActivityRepo new repository
|
||||
|
|
|
@ -170,7 +170,6 @@ func (ar *authRepo) RemoveAllUserTokens(ctx context.Context, userID string) {
|
|||
if err := ar.data.Cache.Del(ctx, key); err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// NewAuthRepo new repository
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
"github.com/answerdev/answer/internal/base/reason"
|
||||
"github.com/answerdev/answer/internal/service/action"
|
||||
"github.com/segmentfault/pacman/errors"
|
||||
"github.com/segmentfault/pacman/log"
|
||||
)
|
||||
|
||||
// captchaRepo captcha repository
|
||||
|
@ -65,7 +66,7 @@ func (cr *captchaRepo) SetCaptcha(ctx context.Context, key, captcha string) (err
|
|||
func (cr *captchaRepo) GetCaptcha(ctx context.Context, key string) (captcha string, err error) {
|
||||
captcha, err = cr.data.Cache.GetString(ctx, key)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
log.Debug(err)
|
||||
}
|
||||
// TODO: cache reflect should return empty when key not found
|
||||
return captcha, nil
|
||||
|
|
|
@ -33,7 +33,6 @@ func Test_commentRepo_AddComment(t *testing.T) {
|
|||
|
||||
err = commentRepo.RemoveComment(context.TODO(), testCommentEntity.ID)
|
||||
assert.NoError(t, err)
|
||||
return
|
||||
}
|
||||
|
||||
func Test_commentRepo_GetCommentPage(t *testing.T) {
|
||||
|
@ -55,7 +54,6 @@ func Test_commentRepo_GetCommentPage(t *testing.T) {
|
|||
|
||||
err = commentRepo.RemoveComment(context.TODO(), testCommentEntity.ID)
|
||||
assert.NoError(t, err)
|
||||
return
|
||||
}
|
||||
|
||||
func Test_commentRepo_UpdateComment(t *testing.T) {
|
||||
|
@ -77,5 +75,4 @@ func Test_commentRepo_UpdateComment(t *testing.T) {
|
|||
|
||||
err = commentRepo.RemoveComment(context.TODO(), testCommentEntity.ID)
|
||||
assert.NoError(t, err)
|
||||
return
|
||||
}
|
||||
|
|
|
@ -3,12 +3,13 @@ package repo_test
|
|||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/answerdev/answer/internal/entity"
|
||||
"github.com/answerdev/answer/internal/repo/question"
|
||||
"github.com/answerdev/answer/internal/repo/revision"
|
||||
"github.com/answerdev/answer/internal/repo/unique"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var q = &entity.Question{
|
||||
|
@ -53,6 +54,7 @@ func Test_revisionRepo_AddRevision(t *testing.T) {
|
|||
assert.NotEqual(t, "", q.ID)
|
||||
|
||||
content, err := json.Marshal(q)
|
||||
assert.NoError(t, err)
|
||||
// auto update false
|
||||
rev := getRev(q.ID, q.Title, string(content))
|
||||
err = revisionRepo.AddRevision(context.TODO(), rev, false)
|
||||
|
|
|
@ -92,7 +92,7 @@ func Test_tagRepo_GetTagListByName(t *testing.T) {
|
|||
tagOnce.Do(addTagList)
|
||||
tagCommonRepo := tag_common.NewTagCommonRepo(testDataSource, unique.NewUniqueIDRepo(testDataSource))
|
||||
|
||||
gotTags, err := tagCommonRepo.GetTagListByName(context.TODO(), testTagList[0].SlugName, 1, false)
|
||||
gotTags, err := tagCommonRepo.GetTagListByName(context.TODO(), testTagList[0].SlugName, false)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, testTagList[0].SlugName, gotTags[0].SlugName)
|
||||
}
|
||||
|
|
|
@ -20,7 +20,7 @@ func Test_userBackyardRepo_GetUserInfo(t *testing.T) {
|
|||
|
||||
func Test_userBackyardRepo_GetUserPage(t *testing.T) {
|
||||
userBackyardRepo := user.NewUserBackyardRepo(testDataSource, auth.NewAuthRepo(testDataSource))
|
||||
got, total, err := userBackyardRepo.GetUserPage(context.TODO(), 1, 1, &entity.User{Username: "admin"}, "")
|
||||
got, total, err := userBackyardRepo.GetUserPage(context.TODO(), 1, 1, &entity.User{Username: "admin"}, "", false)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, int64(1), total)
|
||||
assert.Equal(t, "1", got[0].ID)
|
||||
|
|
|
@ -76,28 +76,27 @@ func (rr *reportRepo) GetReportListPage(ctx context.Context, dto schema.GetRepor
|
|||
}
|
||||
|
||||
// GetByID get report by ID
|
||||
func (ar *reportRepo) GetByID(ctx context.Context, id string) (report entity.Report, exist bool, err error) {
|
||||
report = entity.Report{}
|
||||
exist, err = ar.data.DB.ID(id).Get(&report)
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateByID handle report by ID
|
||||
func (ar *reportRepo) UpdateByID(
|
||||
ctx context.Context,
|
||||
id string,
|
||||
handleData entity.Report,
|
||||
) (err error) {
|
||||
_, err = ar.data.DB.ID(id).Update(&handleData)
|
||||
func (rr *reportRepo) GetByID(ctx context.Context, id string) (report *entity.Report, exist bool, err error) {
|
||||
report = &entity.Report{}
|
||||
exist, err = rr.data.DB.ID(id).Get(report)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (vr *reportRepo) GetReportCount(ctx context.Context) (count int64, err error) {
|
||||
// UpdateByID handle report by ID
|
||||
func (rr *reportRepo) UpdateByID(ctx context.Context, id string, handleData entity.Report) (err error) {
|
||||
_, err = rr.data.DB.ID(id).Update(&handleData)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (rr *reportRepo) GetReportCount(ctx context.Context) (count int64, err error) {
|
||||
list := make([]*entity.Report, 0)
|
||||
count, err = vr.data.DB.Where("status =?", entity.ReportStatusPending).FindAndCount(&list)
|
||||
count, err = rr.data.DB.Where("status =?", entity.ReportStatusPending).FindAndCount(&list)
|
||||
if err != nil {
|
||||
return count, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
|
|
@ -1,36 +0,0 @@
|
|||
package router
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestUIRouter_Register(t *testing.T) {
|
||||
r := gin.Default()
|
||||
|
||||
NewUIRouter().Register(r)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/", nil)
|
||||
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
func TestUIRouter_Static(t *testing.T) {
|
||||
r := gin.Default()
|
||||
|
||||
NewUIRouter().Register(r)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/static/version.txt", nil)
|
||||
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
assert.Equal(t, "OK", w.Body.String())
|
||||
}
|
|
@ -148,6 +148,9 @@ func (os *ObjService) GetInfo(ctx context.Context, objectID string) (objInfo *sc
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !exist {
|
||||
break
|
||||
}
|
||||
objInfo = &schema.SimpleObjectInfo{
|
||||
ObjectID: answerInfo.ID,
|
||||
ObjectCreatorUserID: answerInfo.UserID,
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
|
||||
"github.com/answerdev/answer/internal/service/config"
|
||||
"github.com/answerdev/answer/pkg/htmltext"
|
||||
"github.com/segmentfault/pacman/log"
|
||||
|
||||
"github.com/answerdev/answer/internal/base/pager"
|
||||
"github.com/answerdev/answer/internal/base/reason"
|
||||
|
@ -106,13 +107,13 @@ func (rs *ReportBackyardService) ListReportPage(ctx context.Context, dto schema.
|
|||
// HandleReported handle the reported object
|
||||
func (rs *ReportBackyardService) HandleReported(ctx context.Context, req schema.ReportHandleReq) (err error) {
|
||||
var (
|
||||
reported = entity.Report{}
|
||||
reported *entity.Report
|
||||
handleData = entity.Report{
|
||||
FlaggedContent: req.FlaggedContent,
|
||||
FlaggedType: req.FlaggedType,
|
||||
Status: entity.ReportStatusCompleted,
|
||||
}
|
||||
exist = false
|
||||
exist bool
|
||||
)
|
||||
|
||||
reported, exist, err = rs.reportRepo.GetByID(ctx, req.ID)
|
||||
|
@ -159,6 +160,7 @@ func (rs *ReportBackyardService) parseObject(ctx context.Context, resp *[]*schem
|
|||
|
||||
objIds, err = rs.commonRepo.GetObjectIDMap(r.ObjectID)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
continue
|
||||
}
|
||||
|
||||
|
@ -175,11 +177,19 @@ func (rs *ReportBackyardService) parseObject(ctx context.Context, resp *[]*schem
|
|||
answerId, ok = objIds["answer"]
|
||||
if ok {
|
||||
answer, _, err = rs.answerRepo.GetAnswer(ctx, answerId)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
commentId, ok = objIds["comment"]
|
||||
if ok {
|
||||
cmt, _, err = rs.commentCommonRepo.GetComment(ctx, commentId)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
switch r.OType {
|
||||
|
@ -208,15 +218,20 @@ func (rs *ReportBackyardService) parseObject(ctx context.Context, resp *[]*schem
|
|||
ReasonType: r.ReportType,
|
||||
}
|
||||
err = rs.configRepo.GetJsonConfigByIDAndSetToObject(r.ReportType, r.Reason)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
}
|
||||
if r.FlaggedType > 0 {
|
||||
r.FlaggedReason = &schema.ReasonItem{
|
||||
ReasonType: r.FlaggedType,
|
||||
}
|
||||
_ = rs.configRepo.GetJsonConfigByIDAndSetToObject(r.FlaggedType, r.FlaggedReason)
|
||||
err = rs.configRepo.GetJsonConfigByIDAndSetToObject(r.FlaggedType, r.FlaggedReason)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
res[i] = r
|
||||
}
|
||||
resp = &res
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ import (
|
|||
type ReportRepo interface {
|
||||
AddReport(ctx context.Context, report *entity.Report) (err error)
|
||||
GetReportListPage(ctx context.Context, query schema.GetReportListPageDTO) (reports []entity.Report, total int64, err error)
|
||||
GetByID(ctx context.Context, id string) (report entity.Report, exist bool, err error)
|
||||
GetByID(ctx context.Context, id string) (report *entity.Report, exist bool, err error)
|
||||
UpdateByID(ctx context.Context, id string, handleData entity.Report) (err error)
|
||||
GetReportCount(ctx context.Context) (count int64, err error)
|
||||
}
|
||||
|
|
|
@ -32,7 +32,7 @@ func NewReportHandle(
|
|||
}
|
||||
|
||||
// HandleObject this handle object status
|
||||
func (rh *ReportHandle) HandleObject(ctx context.Context, reported entity.Report, req schema.ReportHandleReq) (err error) {
|
||||
func (rh *ReportHandle) HandleObject(ctx context.Context, reported *entity.Report, req schema.ReportHandleReq) (err error) {
|
||||
var (
|
||||
objectID = reported.ObjectID
|
||||
reportedUserID = reported.ReportedUserID
|
||||
|
|
|
@ -318,7 +318,7 @@ func (sp *SearchParser) parseAccepted(query *string) (accepted bool) {
|
|||
|
||||
if strings.Contains(q, expr) {
|
||||
accepted = true
|
||||
strings.ReplaceAll(q, expr, "")
|
||||
q = strings.ReplaceAll(q, expr, "")
|
||||
}
|
||||
|
||||
*query = strings.TrimSpace(q)
|
||||
|
|
|
@ -119,12 +119,14 @@ func (us *UploaderService) AvatarThumbFile(ctx *gin.Context, uploadPath, fileNam
|
|||
return avatarfile, fmt.Errorf("img extension not exist")
|
||||
}
|
||||
err = imaging.Encode(&buf, new_image, FormatExts[fileSuffix])
|
||||
|
||||
if err != nil {
|
||||
return avatarfile, errors.InternalServer(reason.UnknownError).WithError(err).WithStack()
|
||||
}
|
||||
thumbReader := bytes.NewReader(buf.Bytes())
|
||||
dir.CreateDirIfNotExist(path.Join(us.serviceConfig.UploadPath, avatarThumbSubPath))
|
||||
err = dir.CreateDirIfNotExist(path.Join(us.serviceConfig.UploadPath, avatarThumbSubPath))
|
||||
if err != nil {
|
||||
return nil, errors.InternalServer(reason.UnknownError).WithError(err).WithStack()
|
||||
}
|
||||
avatarFilePath := path.Join(avatarThumbSubPath, thumbFileName)
|
||||
savefilePath := path.Join(us.serviceConfig.UploadPath, avatarFilePath)
|
||||
out, err := os.Create(savefilePath)
|
||||
|
|
|
@ -180,6 +180,7 @@ func (vs *VoteService) ListUserVotes(ctx context.Context, req schema.GetVoteWith
|
|||
objInfo, err = vs.objectService.GetInfo(ctx, voteInfo.ObjectID)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
continue
|
||||
}
|
||||
|
||||
item := schema.GetVoteWithPageResp{
|
||||
|
|
Loading…
Reference in New Issue