MM-25476: Migrate AppError from command_store.go (#14643)

Automatic Merge
Этот коммит содержится в:
Joshua Bezaleel Abednego
2020-07-16 20:26:07 +07:00
коммит произвёл GitHub
родитель 5ddf8d4099
Коммит 48f0b7fd76
13 изменённых файлов: 353 добавлений и 257 удалений

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

@@ -88,7 +88,7 @@ func (a *App) ListAutocompleteCommands(teamId string, T goi18n.TranslateFunc) ([
if *a.Config().ServiceSettings.EnableCommands {
teamCmds, err := a.Srv().Store.Command().GetByTeam(teamId)
if err != nil {
return nil, err
return nil, model.NewAppError("ListAutocompleteCommands", "app.command.listautocompletecommands.internal_error", nil, err.Error(), http.StatusInternalServerError)
}
for _, cmd := range teamCmds {
@@ -119,7 +119,12 @@ func (a *App) ListTeamCommands(teamId string) ([]*model.Command, *model.AppError
return nil, model.NewAppError("ListTeamCommands", "api.command.disabled.app_error", nil, "", http.StatusNotImplemented)
}
return a.Srv().Store.Command().GetByTeam(teamId)
teamCmds, err := a.Srv().Store.Command().GetByTeam(teamId)
if err != nil {
return nil, model.NewAppError("ListTeamCommands", "app.command.listteamcommands.internal_error", nil, err.Error(), http.StatusInternalServerError)
}
return teamCmds, nil
}
func (a *App) ListAllCommands(teamId string, T goi18n.TranslateFunc) ([]*model.Command, *model.AppError) {
@@ -146,7 +151,7 @@ func (a *App) ListAllCommands(teamId string, T goi18n.TranslateFunc) ([]*model.C
if *a.Config().ServiceSettings.EnableCommands {
teamCmds, err := a.Srv().Store.Command().GetByTeam(teamId)
if err != nil {
return nil, err
return nil, model.NewAppError("ListAllCommands", "app.command.listallcommands.internal_error", nil, err.Error(), http.StatusInternalServerError)
}
for _, cmd := range teamCmds {
if !seen[cmd.Trigger] {
@@ -368,7 +373,7 @@ func (a *App) tryExecuteCustomCommand(args *model.CommandArgs, trigger string, m
teamCmds, err := a.Srv().Store.Command().GetByTeam(args.TeamId)
if err != nil {
return nil, nil, err
return nil, nil, model.NewAppError("tryExecuteCustomCommand", "app.command.tryexecutecustomcommand.internal_error", nil, err.Error(), http.StatusInternalServerError)
}
tr := <-teamChan
@@ -602,7 +607,7 @@ func (a *App) CreateCommand(cmd *model.Command) (*model.Command, *model.AppError
teamCmds, err := a.Srv().Store.Command().GetByTeam(cmd.TeamId)
if err != nil {
return nil, err
return nil, model.NewAppError("CreateCommand", "app.command.createcommand.internal_error", nil, err.Error(), http.StatusInternalServerError)
}
for _, existingCommand := range teamCmds {
@@ -618,7 +623,18 @@ func (a *App) CreateCommand(cmd *model.Command) (*model.Command, *model.AppError
}
}
return a.Srv().Store.Command().Save(cmd)
command, nErr := a.Srv().Store.Command().Save(cmd)
if nErr != nil {
var appErr *model.AppError
switch {
case errors.As(nErr, &appErr):
return nil, appErr
default:
return nil, model.NewAppError("CreateCommand", "app.command.createcommand.internal_error", nil, nErr.Error(), http.StatusInternalServerError)
}
}
return command, nil
}
func (a *App) GetCommand(commandId string) (*model.Command, *model.AppError) {
@@ -626,13 +642,17 @@ func (a *App) GetCommand(commandId string) (*model.Command, *model.AppError) {
return nil, model.NewAppError("GetCommand", "api.command.disabled.app_error", nil, "", http.StatusNotImplemented)
}
cmd, err := a.Srv().Store.Command().Get(commandId)
command, err := a.Srv().Store.Command().Get(commandId)
if err != nil {
err.StatusCode = http.StatusNotFound
return nil, err
var nfErr *store.ErrNotFound
switch {
case errors.As(err, &nfErr):
return nil, model.NewAppError("SqlCommandStore.Get", "store.sql_command.get.missing.app_error", map[string]interface{}{"command_id": commandId}, "", http.StatusNotFound)
default:
return nil, model.NewAppError("GetCommand", "app.command.getcommand.internal_error", nil, err.Error(), http.StatusInternalServerError)
}
}
return cmd, nil
return command, nil
}
func (a *App) UpdateCommand(oldCmd, updatedCmd *model.Command) (*model.Command, *model.AppError) {
@@ -649,7 +669,21 @@ func (a *App) UpdateCommand(oldCmd, updatedCmd *model.Command) (*model.Command,
updatedCmd.CreatorId = oldCmd.CreatorId
updatedCmd.TeamId = oldCmd.TeamId
return a.Srv().Store.Command().Update(updatedCmd)
command, err := a.Srv().Store.Command().Update(updatedCmd)
if err != nil {
var nfErr *store.ErrNotFound
var appErr *model.AppError
switch {
case errors.As(err, &nfErr):
return nil, model.NewAppError("SqlCommandStore.Update", "store.sql_command.update.missing.app_error", map[string]interface{}{"command_id": updatedCmd.Id}, "", http.StatusNotFound)
case errors.As(err, &appErr):
return nil, appErr
default:
return nil, model.NewAppError("UpdateCommand", "app.command.updatecommand.internal_error", nil, err.Error(), http.StatusInternalServerError)
}
}
return command, nil
}
func (a *App) MoveCommand(team *model.Team, command *model.Command) *model.AppError {
@@ -657,7 +691,16 @@ func (a *App) MoveCommand(team *model.Team, command *model.Command) *model.AppEr
_, err := a.Srv().Store.Command().Update(command)
if err != nil {
return err
var nfErr *store.ErrNotFound
var appErr *model.AppError
switch {
case errors.As(err, &nfErr):
return model.NewAppError("SqlCommandStore.Update", "store.sql_command.update.missing.app_error", map[string]interface{}{"command_id": command.Id}, "", http.StatusNotFound)
case errors.As(err, &appErr):
return appErr
default:
return model.NewAppError("MoveCommand", "app.command.movecommand.internal_error", nil, err.Error(), http.StatusInternalServerError)
}
}
return nil
@@ -670,7 +713,21 @@ func (a *App) RegenCommandToken(cmd *model.Command) (*model.Command, *model.AppE
cmd.Token = model.NewId()
return a.Srv().Store.Command().Update(cmd)
command, err := a.Srv().Store.Command().Update(cmd)
if err != nil {
var nfErr *store.ErrNotFound
var appErr *model.AppError
switch {
case errors.As(err, &nfErr):
return nil, model.NewAppError("SqlCommandStore.Update", "store.sql_command.update.missing.app_error", map[string]interface{}{"command_id": cmd.Id}, "", http.StatusNotFound)
case errors.As(err, &appErr):
return nil, appErr
default:
return nil, model.NewAppError("RegenCommandToken", "app.command.regencommandtoken.internal_error", nil, err.Error(), http.StatusInternalServerError)
}
}
return command, nil
}
func (a *App) DeleteCommand(commandId string) *model.AppError {
@@ -678,5 +735,10 @@ func (a *App) DeleteCommand(commandId string) *model.AppError {
return model.NewAppError("DeleteCommand", "api.command.disabled.app_error", nil, "", http.StatusNotImplemented)
}
return a.Srv().Store.Command().Delete(commandId, model.GetMillis())
err := a.Srv().Store.Command().Delete(commandId, model.GetMillis())
if err != nil {
return model.NewAppError("DeleteCommand", "app.command.deletecommand.internal_error", nil, err.Error(), http.StatusInternalServerError)
}
return nil
}