From 8b86e1276e7d7279029e6b3372c04ea0fdaacb36 Mon Sep 17 00:00:00 2001 From: Pavel Mokeev <75482510+pmokeev@users.noreply.github.com> Date: Tue, 10 Dec 2024 16:32:13 +0300 Subject: [PATCH] GH-27059: Rewrite error messages about msg length (#29252) Co-authored-by: Mattermost Build Co-authored-by: Ben Schumacher --- server/i18n/en.json | 8 ++++---- server/public/model/draft.go | 3 ++- server/public/model/post.go | 3 ++- server/public/model/post_test.go | 5 +++++ 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/server/i18n/en.json b/server/i18n/en.json index 6c4f4de8c2..c1ac873327 100644 --- a/server/i18n/en.json +++ b/server/i18n/en.json @@ -9277,8 +9277,8 @@ "translation": "Invalid file ids." }, { - "id": "model.draft.is_valid.msg.app_error", - "translation": "Invalid message." + "id": "model.draft.is_valid.message_length.app_error", + "translation": "Draft Message property is longer than the maximum permitted length." }, { "id": "model.draft.is_valid.priority.app_error", @@ -9729,8 +9729,8 @@ "translation": "Invalid Id." }, { - "id": "model.post.is_valid.msg.app_error", - "translation": "Invalid message." + "id": "model.post.is_valid.message_length.app_error", + "translation": "Post Message property is longer than the maximum permitted length." }, { "id": "model.post.is_valid.original_id.app_error", diff --git a/server/public/model/draft.go b/server/public/model/draft.go index be07894f10..53035497e0 100644 --- a/server/public/model/draft.go +++ b/server/public/model/draft.go @@ -28,7 +28,8 @@ type Draft struct { func (o *Draft) IsValid(maxDraftSize int) *AppError { if utf8.RuneCountInString(o.Message) > maxDraftSize { - return NewAppError("Drafts.IsValid", "model.draft.is_valid.msg.app_error", nil, "channelid="+o.ChannelId, http.StatusBadRequest) + return NewAppError("Drafts.IsValid", "model.draft.is_valid.message_length.app_error", + map[string]any{"Length": utf8.RuneCountInString(o.Message), "MaxLength": maxDraftSize}, "channelid="+o.ChannelId, http.StatusBadRequest) } return o.BaseIsValid() diff --git a/server/public/model/post.go b/server/public/model/post.go index c3df9b9e4a..1b4dc43a7b 100644 --- a/server/public/model/post.go +++ b/server/public/model/post.go @@ -437,7 +437,8 @@ func (o *Post) IsValid(maxPostSize int) *AppError { } if utf8.RuneCountInString(o.Message) > maxPostSize { - return NewAppError("Post.IsValid", "model.post.is_valid.msg.app_error", nil, "id="+o.Id, http.StatusBadRequest) + return NewAppError("Post.IsValid", "model.post.is_valid.message_length.app_error", + map[string]any{"Length": utf8.RuneCountInString(o.Message), "MaxLength": maxPostSize}, "id="+o.Id, http.StatusBadRequest) } if utf8.RuneCountInString(o.Hashtags) > PostHashtagsMaxRunes { diff --git a/server/public/model/post_test.go b/server/public/model/post_test.go index 66c6ae0752..4d2cc679cc 100644 --- a/server/public/model/post_test.go +++ b/server/public/model/post_test.go @@ -59,6 +59,11 @@ func TestPostIsValid(t *testing.T) { appErr = o.IsValid(maxPostSize) require.NotNil(t, appErr) + // In case message property length is too long. + o.Message = strings.Repeat("0", maxPostSize+1) + appErr = o.IsValid(maxPostSize) + require.NotNil(t, appErr) + o.Message = strings.Repeat("0", maxPostSize) appErr = o.IsValid(maxPostSize) require.Nil(t, appErr)