Migrate to idiomatic error handling in app/command.go (#9675)
Этот коммит содержится в:
коммит произвёл
Carlos Tadeu Panato Junior
родитель
557fd9ea18
Коммит
050c9de0f0
124
app/command.go
124
app/command.go
@@ -52,7 +52,9 @@ func (a *App) CreateCommandPost(post *model.Post, teamId string, response *model
|
|||||||
|
|
||||||
if response.ResponseType == model.COMMAND_RESPONSE_TYPE_IN_CHANNEL {
|
if response.ResponseType == model.COMMAND_RESPONSE_TYPE_IN_CHANNEL {
|
||||||
return a.CreatePostMissingChannel(post, true)
|
return a.CreatePostMissingChannel(post, true)
|
||||||
} else if (response.ResponseType == "" || response.ResponseType == model.COMMAND_RESPONSE_TYPE_EPHEMERAL) && (response.Text != "" || response.Attachments != nil) {
|
}
|
||||||
|
|
||||||
|
if (response.ResponseType == "" || response.ResponseType == model.COMMAND_RESPONSE_TYPE_EPHEMERAL) && (response.Text != "" || response.Attachments != nil) {
|
||||||
post.ParentId = ""
|
post.ParentId = ""
|
||||||
a.SendEphemeralPost(post.UserId, post)
|
a.SendEphemeralPost(post.UserId, post)
|
||||||
}
|
}
|
||||||
@@ -83,9 +85,11 @@ func (a *App) ListAutocompleteCommands(teamId string, T goi18n.TranslateFunc) ([
|
|||||||
}
|
}
|
||||||
|
|
||||||
if *a.Config().ServiceSettings.EnableCommands {
|
if *a.Config().ServiceSettings.EnableCommands {
|
||||||
if result := <-a.Srv.Store.Command().GetByTeam(teamId); result.Err != nil {
|
result := <-a.Srv.Store.Command().GetByTeam(teamId)
|
||||||
|
if result.Err != nil {
|
||||||
return nil, result.Err
|
return nil, result.Err
|
||||||
} else {
|
}
|
||||||
|
|
||||||
teamCmds := result.Data.([]*model.Command)
|
teamCmds := result.Data.([]*model.Command)
|
||||||
for _, cmd := range teamCmds {
|
for _, cmd := range teamCmds {
|
||||||
if cmd.AutoComplete && !seen[cmd.Id] {
|
if cmd.AutoComplete && !seen[cmd.Id] {
|
||||||
@@ -95,7 +99,6 @@ func (a *App) ListAutocompleteCommands(teamId string, T goi18n.TranslateFunc) ([
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return commands, nil
|
return commands, nil
|
||||||
}
|
}
|
||||||
@@ -105,11 +108,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 nil, model.NewAppError("ListTeamCommands", "api.command.disabled.app_error", nil, "", http.StatusNotImplemented)
|
||||||
}
|
}
|
||||||
|
|
||||||
if result := <-a.Srv.Store.Command().GetByTeam(teamId); result.Err != nil {
|
result := <-a.Srv.Store.Command().GetByTeam(teamId)
|
||||||
|
if result.Err != nil {
|
||||||
return nil, result.Err
|
return nil, result.Err
|
||||||
} else {
|
|
||||||
return result.Data.([]*model.Command), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return result.Data.([]*model.Command), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) ListAllCommands(teamId string, T goi18n.TranslateFunc) ([]*model.Command, *model.AppError) {
|
func (a *App) ListAllCommands(teamId string, T goi18n.TranslateFunc) ([]*model.Command, *model.AppError) {
|
||||||
@@ -134,9 +138,10 @@ func (a *App) ListAllCommands(teamId string, T goi18n.TranslateFunc) ([]*model.C
|
|||||||
}
|
}
|
||||||
|
|
||||||
if *a.Config().ServiceSettings.EnableCommands {
|
if *a.Config().ServiceSettings.EnableCommands {
|
||||||
if result := <-a.Srv.Store.Command().GetByTeam(teamId); result.Err != nil {
|
result := <-a.Srv.Store.Command().GetByTeam(teamId)
|
||||||
|
if result.Err != nil {
|
||||||
return nil, result.Err
|
return nil, result.Err
|
||||||
} else {
|
}
|
||||||
teamCmds := result.Data.([]*model.Command)
|
teamCmds := result.Data.([]*model.Command)
|
||||||
for _, cmd := range teamCmds {
|
for _, cmd := range teamCmds {
|
||||||
if !seen[cmd.Trigger] {
|
if !seen[cmd.Trigger] {
|
||||||
@@ -146,7 +151,6 @@ func (a *App) ListAllCommands(teamId string, T goi18n.TranslateFunc) ([]*model.C
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return commands, nil
|
return commands, nil
|
||||||
}
|
}
|
||||||
@@ -165,9 +169,11 @@ func (a *App) ExecuteCommand(args *model.CommandArgs) (*model.CommandResponse, *
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if cmd, response, err := a.ExecutePluginCommand(args); err != nil {
|
cmd, response, appErr := a.ExecutePluginCommand(args)
|
||||||
return nil, err
|
if appErr != nil {
|
||||||
} else if cmd != nil {
|
return nil, appErr
|
||||||
|
}
|
||||||
|
if cmd != nil {
|
||||||
return a.HandleCommandResponse(cmd, args, response, true)
|
return a.HandleCommandResponse(cmd, args, response, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -179,30 +185,28 @@ func (a *App) ExecuteCommand(args *model.CommandArgs) (*model.CommandResponse, *
|
|||||||
teamChan := a.Srv.Store.Team().Get(args.TeamId)
|
teamChan := a.Srv.Store.Team().Get(args.TeamId)
|
||||||
userChan := a.Srv.Store.User().Get(args.UserId)
|
userChan := a.Srv.Store.User().Get(args.UserId)
|
||||||
|
|
||||||
if result := <-a.Srv.Store.Command().GetByTeam(args.TeamId); result.Err != nil {
|
result := <-a.Srv.Store.Command().GetByTeam(args.TeamId)
|
||||||
|
if result.Err != nil {
|
||||||
return nil, result.Err
|
return nil, result.Err
|
||||||
} else {
|
}
|
||||||
|
|
||||||
var team *model.Team
|
tr := <-teamChan
|
||||||
if tr := <-teamChan; tr.Err != nil {
|
if tr.Err != nil {
|
||||||
return nil, tr.Err
|
return nil, tr.Err
|
||||||
} else {
|
|
||||||
team = tr.Data.(*model.Team)
|
|
||||||
}
|
}
|
||||||
|
team := tr.Data.(*model.Team)
|
||||||
|
|
||||||
var user *model.User
|
ur := <-userChan
|
||||||
if ur := <-userChan; ur.Err != nil {
|
if ur.Err != nil {
|
||||||
return nil, ur.Err
|
return nil, ur.Err
|
||||||
} else {
|
|
||||||
user = ur.Data.(*model.User)
|
|
||||||
}
|
}
|
||||||
|
user := ur.Data.(*model.User)
|
||||||
|
|
||||||
var channel *model.Channel
|
cr := <-chanChan
|
||||||
if cr := <-chanChan; cr.Err != nil {
|
if cr.Err != nil {
|
||||||
return nil, cr.Err
|
return nil, cr.Err
|
||||||
} else {
|
|
||||||
channel = cr.Data.(*model.Channel)
|
|
||||||
}
|
}
|
||||||
|
channel := cr.Data.(*model.Channel)
|
||||||
|
|
||||||
teamCmds := result.Data.([]*model.Command)
|
teamCmds := result.Data.([]*model.Command)
|
||||||
for _, cmd := range teamCmds {
|
for _, cmd := range teamCmds {
|
||||||
@@ -224,11 +228,11 @@ func (a *App) ExecuteCommand(args *model.CommandArgs) (*model.CommandResponse, *
|
|||||||
p.Set("command", "/"+trigger)
|
p.Set("command", "/"+trigger)
|
||||||
p.Set("text", message)
|
p.Set("text", message)
|
||||||
|
|
||||||
if hook, err := a.CreateCommandWebhook(cmd.Id, args); err != nil {
|
hook, appErr := a.CreateCommandWebhook(cmd.Id, args)
|
||||||
return nil, model.NewAppError("command", "api.command.execute_command.failed.app_error", map[string]interface{}{"Trigger": trigger}, err.Error(), http.StatusInternalServerError)
|
if appErr != nil {
|
||||||
} else {
|
return nil, model.NewAppError("command", "api.command.execute_command.failed.app_error", map[string]interface{}{"Trigger": trigger}, appErr.Error(), http.StatusInternalServerError)
|
||||||
p.Set("response_url", args.SiteURL+"/hooks/commands/"+hook.Id)
|
|
||||||
}
|
}
|
||||||
|
p.Set("response_url", args.SiteURL+"/hooks/commands/"+hook.Id)
|
||||||
|
|
||||||
var req *http.Request
|
var req *http.Request
|
||||||
if cmd.Method == model.COMMAND_METHOD_GET {
|
if cmd.Method == model.COMMAND_METHOD_GET {
|
||||||
@@ -248,24 +252,24 @@ func (a *App) ExecuteCommand(args *model.CommandArgs) (*model.CommandResponse, *
|
|||||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp, err := a.HTTPService.MakeClient(false).Do(req); err != nil {
|
resp, err := a.HTTPService.MakeClient(false).Do(req)
|
||||||
|
if err != nil {
|
||||||
return nil, model.NewAppError("command", "api.command.execute_command.failed.app_error", map[string]interface{}{"Trigger": trigger}, err.Error(), http.StatusInternalServerError)
|
return nil, model.NewAppError("command", "api.command.execute_command.failed.app_error", map[string]interface{}{"Trigger": trigger}, err.Error(), http.StatusInternalServerError)
|
||||||
} else {
|
|
||||||
if resp.StatusCode == http.StatusOK {
|
|
||||||
if response, err := model.CommandResponseFromHTTPBody(resp.Header.Get("Content-Type"), resp.Body); err != nil {
|
|
||||||
return nil, model.NewAppError("command", "api.command.execute_command.failed.app_error", map[string]interface{}{"Trigger": trigger}, err.Error(), http.StatusInternalServerError)
|
|
||||||
} else if response == nil {
|
|
||||||
return nil, model.NewAppError("command", "api.command.execute_command.failed_empty.app_error", map[string]interface{}{"Trigger": trigger}, "", http.StatusInternalServerError)
|
|
||||||
} else {
|
|
||||||
return a.HandleCommandResponse(cmd, args, response, false)
|
|
||||||
}
|
}
|
||||||
} else {
|
if resp.StatusCode != http.StatusOK {
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
body, _ := ioutil.ReadAll(resp.Body)
|
body, _ := ioutil.ReadAll(resp.Body)
|
||||||
return nil, model.NewAppError("command", "api.command.execute_command.failed_resp.app_error", map[string]interface{}{"Trigger": trigger, "Status": resp.Status}, string(body), http.StatusInternalServerError)
|
return nil, model.NewAppError("command", "api.command.execute_command.failed_resp.app_error", map[string]interface{}{"Trigger": trigger, "Status": resp.Status}, string(body), http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
response, err := model.CommandResponseFromHTTPBody(resp.Header.Get("Content-Type"), resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, model.NewAppError("command", "api.command.execute_command.failed.app_error", map[string]interface{}{"Trigger": trigger}, err.Error(), http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
if response == nil {
|
||||||
|
return nil, model.NewAppError("command", "api.command.execute_command.failed_empty.app_error", map[string]interface{}{"Trigger": trigger}, "", http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
return a.HandleCommandResponse(cmd, args, response, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -327,28 +331,31 @@ func (a *App) CreateCommand(cmd *model.Command) (*model.Command, *model.AppError
|
|||||||
|
|
||||||
cmd.Trigger = strings.ToLower(cmd.Trigger)
|
cmd.Trigger = strings.ToLower(cmd.Trigger)
|
||||||
|
|
||||||
if result := <-a.Srv.Store.Command().GetByTeam(cmd.TeamId); result.Err != nil {
|
result := <-a.Srv.Store.Command().GetByTeam(cmd.TeamId)
|
||||||
|
if result.Err != nil {
|
||||||
return nil, result.Err
|
return nil, result.Err
|
||||||
} else {
|
}
|
||||||
|
|
||||||
teamCmds := result.Data.([]*model.Command)
|
teamCmds := result.Data.([]*model.Command)
|
||||||
for _, existingCommand := range teamCmds {
|
for _, existingCommand := range teamCmds {
|
||||||
if cmd.Trigger == existingCommand.Trigger {
|
if cmd.Trigger == existingCommand.Trigger {
|
||||||
return nil, model.NewAppError("CreateCommand", "api.command.duplicate_trigger.app_error", nil, "", http.StatusBadRequest)
|
return nil, model.NewAppError("CreateCommand", "api.command.duplicate_trigger.app_error", nil, "", http.StatusBadRequest)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, builtInProvider := range commandProviders {
|
for _, builtInProvider := range commandProviders {
|
||||||
builtInCommand := builtInProvider.GetCommand(a, utils.T)
|
builtInCommand := builtInProvider.GetCommand(a, utils.T)
|
||||||
if builtInCommand != nil && cmd.Trigger == builtInCommand.Trigger {
|
if builtInCommand != nil && cmd.Trigger == builtInCommand.Trigger {
|
||||||
return nil, model.NewAppError("CreateCommand", "api.command.duplicate_trigger.app_error", nil, "", http.StatusBadRequest)
|
return nil, model.NewAppError("CreateCommand", "api.command.duplicate_trigger.app_error", nil, "", http.StatusBadRequest)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
result = <-a.Srv.Store.Command().Save(cmd)
|
||||||
|
if result.Err != nil {
|
||||||
|
return nil, result.Err
|
||||||
}
|
}
|
||||||
|
|
||||||
if result := <-a.Srv.Store.Command().Save(cmd); result.Err != nil {
|
|
||||||
return nil, result.Err
|
|
||||||
} else {
|
|
||||||
return result.Data.(*model.Command), nil
|
return result.Data.(*model.Command), nil
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) GetCommand(commandId string) (*model.Command, *model.AppError) {
|
func (a *App) GetCommand(commandId string) (*model.Command, *model.AppError) {
|
||||||
@@ -356,12 +363,13 @@ func (a *App) GetCommand(commandId string) (*model.Command, *model.AppError) {
|
|||||||
return nil, model.NewAppError("GetCommand", "api.command.disabled.app_error", nil, "", http.StatusNotImplemented)
|
return nil, model.NewAppError("GetCommand", "api.command.disabled.app_error", nil, "", http.StatusNotImplemented)
|
||||||
}
|
}
|
||||||
|
|
||||||
if result := <-a.Srv.Store.Command().Get(commandId); result.Err != nil {
|
result := <-a.Srv.Store.Command().Get(commandId)
|
||||||
|
if result.Err != nil {
|
||||||
result.Err.StatusCode = http.StatusNotFound
|
result.Err.StatusCode = http.StatusNotFound
|
||||||
return nil, result.Err
|
return nil, result.Err
|
||||||
} else {
|
|
||||||
return result.Data.(*model.Command), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return result.Data.(*model.Command), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) UpdateCommand(oldCmd, updatedCmd *model.Command) (*model.Command, *model.AppError) {
|
func (a *App) UpdateCommand(oldCmd, updatedCmd *model.Command) (*model.Command, *model.AppError) {
|
||||||
@@ -378,17 +386,18 @@ func (a *App) UpdateCommand(oldCmd, updatedCmd *model.Command) (*model.Command,
|
|||||||
updatedCmd.CreatorId = oldCmd.CreatorId
|
updatedCmd.CreatorId = oldCmd.CreatorId
|
||||||
updatedCmd.TeamId = oldCmd.TeamId
|
updatedCmd.TeamId = oldCmd.TeamId
|
||||||
|
|
||||||
if result := <-a.Srv.Store.Command().Update(updatedCmd); result.Err != nil {
|
result := <-a.Srv.Store.Command().Update(updatedCmd)
|
||||||
|
if result.Err != nil {
|
||||||
return nil, result.Err
|
return nil, result.Err
|
||||||
} else {
|
|
||||||
return result.Data.(*model.Command), nil
|
|
||||||
}
|
}
|
||||||
|
return result.Data.(*model.Command), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) MoveCommand(team *model.Team, command *model.Command) *model.AppError {
|
func (a *App) MoveCommand(team *model.Team, command *model.Command) *model.AppError {
|
||||||
command.TeamId = team.Id
|
command.TeamId = team.Id
|
||||||
|
|
||||||
if result := <-a.Srv.Store.Command().Update(command); result.Err != nil {
|
result := <-a.Srv.Store.Command().Update(command)
|
||||||
|
if result.Err != nil {
|
||||||
return result.Err
|
return result.Err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -402,11 +411,12 @@ func (a *App) RegenCommandToken(cmd *model.Command) (*model.Command, *model.AppE
|
|||||||
|
|
||||||
cmd.Token = model.NewId()
|
cmd.Token = model.NewId()
|
||||||
|
|
||||||
if result := <-a.Srv.Store.Command().Update(cmd); result.Err != nil {
|
result := <-a.Srv.Store.Command().Update(cmd)
|
||||||
|
if result.Err != nil {
|
||||||
return nil, result.Err
|
return nil, result.Err
|
||||||
} else {
|
|
||||||
return result.Data.(*model.Command), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return result.Data.(*model.Command), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) DeleteCommand(commandId string) *model.AppError {
|
func (a *App) DeleteCommand(commandId string) *model.AppError {
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user