MM-57391: improve error message (#26630)

The error reporte when moving channels and failing has been improved to show
that the problem was the repeated name on the team.
The error message has been unified with MM-53756

Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
pacop
2024-04-22 09:40:08 +02:00
коммит произвёл GitHub
родитель e73a9512f6
Коммит 9e6da03ab1
4 изменённых файлов: 33 добавлений и 8 удалений

Просмотреть файл

@@ -4801,6 +4801,29 @@ func TestMoveChannel(t *testing.T) {
require.Equal(t, team2.Id, ch.TeamId) require.Equal(t, team2.Id, ch.TeamId)
}) })
t.Run("Should return custom error with repeated channel", func(t *testing.T) {
channelT1 := &model.Channel{
DisplayName: "repeated",
Name: "repeated",
Type: model.ChannelTypePrivate,
TeamId: team1.Id,
}
channelT1, _, err := th.Client.CreateChannel(context.TODO(), channelT1)
require.NoError(t, err)
channelT2 := &model.Channel{
DisplayName: "repeated",
Name: "repeated",
Type: model.ChannelTypePrivate,
TeamId: team2.Id,
}
_, _, err = th.Client.CreateChannel(context.TODO(), channelT2)
require.NoError(t, err)
_, _, err = th.SystemAdminClient.MoveChannel(context.Background(), channelT1.Id, team2.Id, false)
require.EqualError(t, err, "A channel with that name already exists on the same team.")
})
t.Run("Should move private channel", func(t *testing.T) { t.Run("Should move private channel", func(t *testing.T) {
channel := th.CreatePrivateChannel() channel := th.CreatePrivateChannel()
ch, _, err := th.SystemAdminClient.MoveChannel(context.Background(), channel.Id, team1.Id, false) ch, _, err := th.SystemAdminClient.MoveChannel(context.Background(), channel.Id, team1.Id, false)

Просмотреть файл

@@ -622,12 +622,12 @@ func (a *App) UpdateChannel(c request.CTX, channel *model.Channel) (*model.Chann
_, err := a.Srv().Store().Channel().Update(c, channel) _, err := a.Srv().Store().Channel().Update(c, channel)
if err != nil { if err != nil {
var appErr *model.AppError var appErr *model.AppError
var uniqueConstraintErr *store.ErrUniqueConstraint
var invErr *store.ErrInvalidInput var invErr *store.ErrInvalidInput
switch { switch {
case errors.As(err, &uniqueConstraintErr):
return nil, model.NewAppError("UpdateChannel", store.ChannelExistsError, nil, "", http.StatusBadRequest).Wrap(err)
case errors.As(err, &invErr): case errors.As(err, &invErr):
if invErr.Entity == "Channel" && invErr.Field == "Name" {
return nil, model.NewAppError("UpdateChannel", store.ChannelExistsError, nil, "", http.StatusBadRequest).Wrap(err)
}
return nil, model.NewAppError("UpdateChannel", "app.channel.update.bad_id", nil, "", http.StatusBadRequest).Wrap(err) return nil, model.NewAppError("UpdateChannel", "app.channel.update.bad_id", nil, "", http.StatusBadRequest).Wrap(err)
case errors.As(err, &appErr): case errors.As(err, &appErr):
return nil, appErr return nil, appErr
@@ -3149,10 +3149,13 @@ func (a *App) MoveChannel(c request.CTX, team *model.Team, channel *model.Channe
channel.TeamId = team.Id channel.TeamId = team.Id
if _, err := a.Srv().Store().Channel().Update(c, channel); err != nil { if _, err := a.Srv().Store().Channel().Update(c, channel); err != nil {
var appErr *model.AppError var appErr *model.AppError
var uniqueConstraintErr *store.ErrUniqueConstraint
var invErr *store.ErrInvalidInput var invErr *store.ErrInvalidInput
switch { switch {
case errors.As(err, &invErr): case errors.As(err, &invErr):
return model.NewAppError("MoveChannel", "app.channel.update.bad_id", nil, "", http.StatusBadRequest).Wrap(err) return model.NewAppError("MoveChannel", "app.channel.update.bad_id", nil, "", http.StatusBadRequest).Wrap(err)
case errors.As(err, &uniqueConstraintErr):
return model.NewAppError("MoveChannel", store.ChannelExistsError, nil, "", http.StatusBadRequest).Wrap(err)
case errors.As(err, &appErr): case errors.As(err, &appErr):
return appErr return appErr
default: default:

Просмотреть файл

@@ -745,7 +745,7 @@ func (s SqlChannelStore) updateChannelT(transaction *sqlxTxWrapper, channel *mod
WHERE Id=:Id`, channel) WHERE Id=:Id`, channel)
if err != nil { if err != nil {
if IsUniqueConstraintError(err, []string{"Name", "channels_name_teamid_key"}) { if IsUniqueConstraintError(err, []string{"Name", "channels_name_teamid_key"}) {
return nil, store.NewErrInvalidInput("Channel", "Name", channel.Name) return nil, store.NewErrUniqueConstraint("Name")
} }
return nil, errors.Wrapf(err, "failed to update channel with id=%s", channel.Id) return nil, errors.Wrapf(err, "failed to update channel with id=%s", channel.Id)
} }

Просмотреть файл

@@ -364,10 +364,9 @@ func testChannelStoreUpdate(t *testing.T, rctx request.CTX, ss store.Store) {
// Make sure that the error correctly reports the wrong field to be Name // Make sure that the error correctly reports the wrong field to be Name
// See https://mattermost.atlassian.net/browse/MM-53756 // See https://mattermost.atlassian.net/browse/MM-53756
var invalidInputErr *store.ErrInvalidInput var uniqueConstraintErr *store.ErrUniqueConstraint
require.ErrorAs(t, err, &invalidInputErr) require.ErrorAs(t, err, &uniqueConstraintErr)
require.Equal(t, invalidInputErr.Entity, "Channel") require.Contains(t, uniqueConstraintErr.Columns, "Name")
require.Equal(t, invalidInputErr.Field, "Name")
} }
func testGetChannelUnread(t *testing.T, rctx request.CTX, ss store.Store) { func testGetChannelUnread(t *testing.T, rctx request.CTX, ss store.Store) {