From 4f73ddc40c3aa52953cf95e97e6c8c4a1161f118 Mon Sep 17 00:00:00 2001 From: Umputun Date: Mon, 25 Mar 2019 23:31:02 -0500 Subject: [PATCH] move error parsing for rest to separate func --- backend/app/rest/api/rest_private.go | 46 ++++++++++++++--------- backend/app/rest/api/rest_private_test.go | 26 +++++++++++++ 2 files changed, 54 insertions(+), 18 deletions(-) diff --git a/backend/app/rest/api/rest_private.go b/backend/app/rest/api/rest_private.go index b7063656..f03f335e 100644 --- a/backend/app/rest/api/rest_private.go +++ b/backend/app/rest/api/rest_private.go @@ -130,14 +130,9 @@ func (s *Rest) updateCommentCtrl(w http.ResponseWriter, r *http.Request) { rest.SendErrorJSON(w, r, http.StatusBadRequest, err, "invalid comment", rest.ErrCommentValidation) return } + if err != nil { - code := rest.ErrCommentRejected - switch { - case strings.HasPrefix(err.Error(), "too late to edit"): - code = rest.ErrCommentEditExpired - case strings.HasPrefix(err.Error(), "parent comment with reply can't be edited"): - code = rest.ErrCommentEditChanged - } + code := s.parseError(err, rest.ErrCommentRejected) rest.SendErrorJSON(w, r, http.StatusBadRequest, err, "can't update comment", code) return } @@ -178,17 +173,7 @@ func (s *Rest) voteCtrl(w http.ResponseWriter, r *http.Request) { comment, err := s.DataService.Vote(locator, id, user.ID, vote) if err != nil { - code := rest.ErrVoteRejected - switch { - case strings.Contains(err.Error(), "can not vote for his own comment"): - code = rest.ErrVoteSelf - case strings.Contains(err.Error(), "already voted for"): - code = rest.ErrVoteDbl - case strings.Contains(err.Error(), "maximum number of votes exceeded for comment"): - code = rest.ErrVoteMax - case strings.Contains(err.Error(), "minimal score reached for comment"): - code = rest.ErrVoteMinScore - } + code := s.parseError(err, rest.ErrVoteRejected) rest.SendErrorJSON(w, r, http.StatusBadRequest, err, "can't vote for comment", code) return } @@ -319,3 +304,28 @@ func (s *Rest) isReadOnly(locator store.Locator) bool { } return s.DataService.IsReadOnly(locator) // ro manually } + +func (s *Rest) parseError(err error, defaultCode int) (code int) { + code = defaultCode + + switch { + // voting errors + case strings.Contains(err.Error(), "can not vote for his own comment"): + code = rest.ErrVoteSelf + case strings.Contains(err.Error(), "already voted for"): + code = rest.ErrVoteDbl + case strings.Contains(err.Error(), "maximum number of votes exceeded for comment"): + code = rest.ErrVoteMax + case strings.Contains(err.Error(), "minimal score reached for comment"): + code = rest.ErrVoteMinScore + + // edit errors + case strings.HasPrefix(err.Error(), "too late to edit"): + code = rest.ErrCommentEditExpired + case strings.HasPrefix(err.Error(), "parent comment with reply can't be edited"): + code = rest.ErrCommentEditChanged + + } + + return code +} diff --git a/backend/app/rest/api/rest_private_test.go b/backend/app/rest/api/rest_private_test.go index e43d7ff1..9d9b1a13 100644 --- a/backend/app/rest/api/rest_private_test.go +++ b/backend/app/rest/api/rest_private_test.go @@ -10,14 +10,17 @@ import ( "mime/multipart" "net/http" "os" + "strconv" "strings" "testing" "time" "github.com/go-pkgz/lgr" R "github.com/go-pkgz/rest" + "github.com/pkg/errors" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/umputun/remark/backend/app/rest" "github.com/umputun/remark/backend/app/store" "github.com/umputun/remark/backend/app/store/image" @@ -632,3 +635,26 @@ func TestRest_CreateWithPictures(t *testing.T) { _, err = os.Stat("/tmp/remark42/images/" + id3) assert.NoError(t, err, "moved from staging") } + +func TestRest_parseError(t *testing.T) { + tbl := []struct { + err error + res int + }{ + {errors.New("can not vote for his own comment"), rest.ErrVoteSelf}, + {errors.New("already voted for"), rest.ErrVoteDbl}, + {errors.New("maximum number of votes exceeded for comment"), rest.ErrVoteMax}, + {errors.New("minimal score reached for comment"), rest.ErrVoteMinScore}, + {errors.New("too late to edit"), rest.ErrCommentEditExpired}, + {errors.New("parent comment with reply can't be edited"), rest.ErrCommentEditChanged}, + {errors.New("blah blah"), rest.ErrInternal}, + } + + svc := Rest{} + for n, tt := range tbl { + t.Run(strconv.Itoa(n), func(t *testing.T) { + res := svc.parseError(tt.err, rest.ErrInternal) + assert.Equal(t, tt.res, res) + }) + } +}