[PLT-6936] Translate AppError.Message automatically by default (#8063)

Этот коммит содержится в:
Jesús Espino
2018-01-08 19:13:24 +01:00
коммит произвёл Joram Wilander
родитель 591ef9f352
Коммит dd9ad10d70
15 изменённых файлов: 28 добавлений и 20 удалений

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

@@ -455,7 +455,6 @@ func IsApiCall(r *http.Request) bool {
func Handle404(w http.ResponseWriter, r *http.Request) {
err := model.NewAppError("Handle404", "api.context.404.app_error", nil, "", http.StatusNotFound)
err.Translate(utils.T)
l4g.Debug("%v: code=404 ip=%v", r.URL.Path, utils.GetIpAddress(r))

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

@@ -230,7 +230,6 @@ func Init(a *app.App, root *mux.Router, full bool) *API {
func Handle404(w http.ResponseWriter, r *http.Request) {
err := model.NewAppError("Handle404", "api.context.404.app_error", nil, "", http.StatusNotFound)
err.Translate(utils.T)
l4g.Debug("%v: code=404 ip=%v", r.URL.Path, utils.GetIpAddress(r))

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

@@ -1937,7 +1937,7 @@ func TestInviteUsersToTeam(t *testing.T) {
if err == nil {
t.Fatal("Adding users with non-restricted domains was allowed")
}
if err.Where != "InviteNewUsersToTeam" || err.Message != "api.team.invite_members.invalid_email.app_error" {
if err.Where != "InviteNewUsersToTeam" || err.Id != "api.team.invite_members.invalid_email.app_error" {
t.Log(err)
t.Fatal("Got wrong error message!")
}

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

@@ -101,6 +101,7 @@ func New(options ...Option) *App {
if utils.T == nil {
utils.TranslationsPreInit()
}
model.AppErrorInit(utils.T)
utils.LoadGlobalConfig(app.configFile)
utils.InitTranslations(utils.Cfg.LocalizationSettings)
@@ -472,7 +473,6 @@ func (a *App) HTTPClient(trustURLs bool) *http.Client {
func (a *App) Handle404(w http.ResponseWriter, r *http.Request) {
err := model.NewAppError("Handle404", "api.context.404.app_error", nil, "", http.StatusNotFound)
err.Translate(utils.T)
l4g.Debug("%v: code=404 ip=%v", r.URL.Path, utils.GetIpAddress(r))

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

@@ -50,7 +50,6 @@ func (cfg *AutoChannelCreator) createRandomChannel() (*model.Channel, bool) {
println(cfg.client.GetTeamRoute())
result, err := cfg.client.CreateChannel(channel)
if err != nil {
err.Translate(utils.T)
println(err.Error())
println(err.DetailedError)
return nil, false

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

@@ -75,7 +75,6 @@ func (cfg *AutoUserCreator) createRandomUser() (*model.User, bool) {
result, err := cfg.client.CreateUserWithInvite(user, "", "", cfg.team.InviteId)
if err != nil {
err.Translate(utils.T)
l4g.Error(err.Error())
return nil, false
}
@@ -84,7 +83,6 @@ func (cfg *AutoUserCreator) createRandomUser() (*model.User, bool) {
status := &model.Status{UserId: ruser.Id, Status: model.STATUS_ONLINE, Manual: false, LastActivityAt: model.GetMillis(), ActiveChannel: ""}
if result := <-cfg.app.Srv.Store.Status().SaveOrUpdate(status); result.Err != nil {
result.Err.Translate(utils.T)
l4g.Error(result.Err.Error())
return nil, false
}

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

@@ -428,7 +428,6 @@ func (a *App) InitPlugins(pluginPath, webappPath string, supervisorOverride plug
func (a *App) ServePluginRequest(w http.ResponseWriter, r *http.Request) {
if a.PluginEnv == nil || !*a.Config().PluginSettings.Enable {
err := model.NewAppError("ServePluginRequest", "app.plugin.disabled.app_error", nil, "Enable plugins to serve plugin requests", http.StatusNotImplemented)
err.Translate(utils.T)
l4g.Error(err.Error())
w.WriteHeader(err.StatusCode)
w.Header().Set("Content-Type", "application/json")

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

@@ -13,7 +13,6 @@ import (
"github.com/mattermost/mattermost-server/app/plugin"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/utils"
)
type Plugin struct {
@@ -65,7 +64,6 @@ func (p *Plugin) handleGetAttributes(w http.ResponseWriter, r *http.Request) {
attributes, err := p.api.GetLdapUserAttributes(id, config.Attributes)
if err != nil {
err.Translate(utils.T)
http.Error(w, fmt.Sprintf("Errored getting attributes: %v", err.Error()), http.StatusInternalServerError)
}

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

@@ -69,7 +69,7 @@ func TestCreateTeamWithUser(t *testing.T) {
t.Log(err.Message)
t.Fatal("Should not create a team with user when user has set email without domain")
} else {
if err.Message != "model.team.is_valid.email.app_error" {
if err.Id != "model.team.is_valid.email.app_error" {
t.Log(err)
t.Fatal("Invalid error message")
}

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

@@ -32,6 +32,7 @@ func init() {
func configValidateCmdF(cmd *cobra.Command, args []string) error {
utils.TranslationsPreInit()
model.AppErrorInit(utils.T)
filePath, err := cmd.Flags().GetString("config")
if err != nil {
return err

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

@@ -26,6 +26,11 @@ func initDBCommandContextCobra(cmd *cobra.Command) (*app.App, error) {
}
func initDBCommandContext(configFileLocation string) (*app.App, error) {
if err := utils.TranslationsPreInit(); err != nil {
return nil, err
}
model.AppErrorInit(utils.T)
if err := utils.InitAndLoadConfig(configFileLocation); err != nil {
return nil, err
}

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

@@ -42,6 +42,12 @@ func runServerCmd(cmd *cobra.Command, args []string) error {
}
func runServer(configFileLocation string) {
if err := utils.TranslationsPreInit(); err != nil {
l4g.Exit("Unable to load Mattermost configuration file: ", err)
return
}
model.AppErrorInit(utils.T)
if err := utils.InitAndLoadConfig(configFileLocation); err != nil {
l4g.Exit("Unable to load Mattermost configuration file: ", err)
return

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

@@ -11,7 +11,6 @@ import (
l4g "github.com/alecthomas/log4go"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/utils"
)
const (
@@ -82,7 +81,6 @@ func (srv *JobServer) SetJobError(job *model.Job, jobError *model.AppError) *mod
if job.Data == nil {
job.Data = make(map[string]string)
}
jobError.Translate(utils.T)
job.Data["error"] = jobError.Message + " (" + jobError.DetailedError + ")"
if result := <-srv.Store.Job().UpdateOptimistically(job, model.JOB_STATUS_IN_PROGRESS); result.Err != nil {

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

@@ -36,6 +36,12 @@ type StringInterface map[string]interface{}
type StringMap map[string]string
type StringArray []string
var translateFunc goi18n.TranslateFunc = nil
func AppErrorInit(t goi18n.TranslateFunc) {
translateFunc = t
}
type AppError struct {
Id string `json:"id"`
Message string `json:"message"` // Message to be display to the end user without debugging information
@@ -52,6 +58,11 @@ func (er *AppError) Error() string {
}
func (er *AppError) Translate(T goi18n.TranslateFunc) {
if T == nil {
er.Message = er.Id
return
}
if er.params == nil {
er.Message = T(er.Id)
} else {
@@ -105,6 +116,7 @@ func NewAppError(where string, id string, params map[string]interface{}, details
ap.DetailedError = details
ap.StatusCode = status
ap.IsOAuth = false
ap.Translate(translateFunc)
return ap
}

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

@@ -268,10 +268,6 @@ func DisableConfigWatch() {
}
func InitAndLoadConfig(filename string) error {
if err := TranslationsPreInit(); err != nil {
return err
}
LoadGlobalConfig(filename)
InitializeConfigWatch()
EnableConfigWatch()
@@ -381,13 +377,12 @@ func LoadGlobalConfig(fileName string) *model.Config {
config.SetDefaults()
if err := config.IsValid(); err != nil {
panic(T(err.Id))
panic(err.Message)
}
if needSave {
cfgMutex.Unlock()
if err := SaveConfig(CfgFileName, config); err != nil {
err.Translate(T)
l4g.Warn(err.Error())
}
cfgMutex.Lock()
@@ -396,7 +391,6 @@ func LoadGlobalConfig(fileName string) *model.Config {
if err := ValidateLocales(config); err != nil {
cfgMutex.Unlock()
if err := SaveConfig(CfgFileName, config); err != nil {
err.Translate(T)
l4g.Warn(err.Error())
}
cfgMutex.Lock()