Fix error message when sharing already shared channel; also make idempotent (#25854)
* fix appError/error assignment bug * make ShareChannel plugin API idempotent --------- Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
@@ -6,6 +6,7 @@ package app
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -1298,7 +1299,12 @@ func (api *PluginAPI) UnregisterPluginForSharedChannels(pluginID string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (api *PluginAPI) ShareChannel(sc *model.SharedChannel) (*model.SharedChannel, error) {
|
func (api *PluginAPI) ShareChannel(sc *model.SharedChannel) (*model.SharedChannel, error) {
|
||||||
return api.app.ShareChannel(api.ctx, sc)
|
scShared, err := api.app.ShareChannel(api.ctx, sc)
|
||||||
|
if errors.Is(err, ErrChannelAlreadyShared) {
|
||||||
|
// sharing an already shared channel is not an error; treat as idempotent and return the existing shared channel
|
||||||
|
return api.app.GetSharedChannel(sc.ChannelId)
|
||||||
|
}
|
||||||
|
return scShared, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (api *PluginAPI) UpdateSharedChannel(sc *model.SharedChannel) (*model.SharedChannel, error) {
|
func (api *PluginAPI) UpdateSharedChannel(sc *model.SharedChannel) (*model.SharedChannel, error) {
|
||||||
|
|||||||
@@ -15,23 +15,21 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
errNotFound = errors.New("not found")
|
errNotFound = errors.New("not found")
|
||||||
|
ErrChannelAlreadyShared = errors.New("channel is already shared")
|
||||||
)
|
)
|
||||||
|
|
||||||
func (a *App) checkChannelNotShared(c request.CTX, channelId string) error {
|
func (a *App) checkChannelNotShared(c request.CTX, channelId string) error {
|
||||||
// check that channel exists.
|
// check that channel exists.
|
||||||
if _, err := a.GetChannel(c, channelId); err != nil {
|
if _, appErr := a.GetChannel(c, channelId); appErr != nil {
|
||||||
return fmt.Errorf("cannot share this channel: %w", err)
|
return fmt.Errorf("cannot find channel: %w", appErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check channel is not already shared.
|
// Check channel is not already shared.
|
||||||
if _, err := a.GetSharedChannel(channelId); err == nil {
|
if _, err := a.GetSharedChannel(channelId); err == nil {
|
||||||
var errNotFound *store.ErrNotFound
|
return ErrChannelAlreadyShared
|
||||||
if errors.As(err, &errNotFound) {
|
|
||||||
return fmt.Errorf("channel is already shared: %w", err)
|
|
||||||
}
|
|
||||||
return fmt.Errorf("cannot find channel: %w", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user