summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesús Espino <jespinog@gmail.com>2018-01-08 19:13:24 +0100
committerJoram Wilander <jwawilander@gmail.com>2018-01-08 13:13:24 -0500
commitdd9ad10d705fdbd7fbfa7323e6560b69021a2950 (patch)
tree4064e05fa746abae33fc7c6939fcdef2be7317d2
parent591ef9f352efd98a85e6d04c0c9072c4c2987527 (diff)
downloadchat-dd9ad10d705fdbd7fbfa7323e6560b69021a2950.tar.gz
chat-dd9ad10d705fdbd7fbfa7323e6560b69021a2950.tar.bz2
chat-dd9ad10d705fdbd7fbfa7323e6560b69021a2950.zip
[PLT-6936] Translate AppError.Message automatically by default (#8063)
-rw-r--r--api/context.go1
-rw-r--r--api4/api.go1
-rw-r--r--api4/team_test.go2
-rw-r--r--app/app.go2
-rw-r--r--app/auto_channels.go1
-rw-r--r--app/auto_users.go2
-rw-r--r--app/plugin.go1
-rw-r--r--app/plugin/ldapextras/plugin.go2
-rw-r--r--app/team_test.go2
-rw-r--r--cmd/platform/config.go1
-rw-r--r--cmd/platform/init.go5
-rw-r--r--cmd/platform/server.go6
-rw-r--r--jobs/jobs.go2
-rw-r--r--model/utils.go12
-rw-r--r--utils/config.go8
15 files changed, 28 insertions, 20 deletions
diff --git a/api/context.go b/api/context.go
index 4f47271e2..34a87e633 100644
--- a/api/context.go
+++ b/api/context.go
@@ -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))
diff --git a/api4/api.go b/api4/api.go
index b9a24cafd..dd5e56d35 100644
--- a/api4/api.go
+++ b/api4/api.go
@@ -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))
diff --git a/api4/team_test.go b/api4/team_test.go
index b5c036755..ebae18f7a 100644
--- a/api4/team_test.go
+++ b/api4/team_test.go
@@ -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!")
}
diff --git a/app/app.go b/app/app.go
index 4813926e2..8aa894b9b 100644
--- a/app/app.go
+++ b/app/app.go
@@ -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))
diff --git a/app/auto_channels.go b/app/auto_channels.go
index bb67e1aa8..78b500961 100644
--- a/app/auto_channels.go
+++ b/app/auto_channels.go
@@ -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
diff --git a/app/auto_users.go b/app/auto_users.go
index 36b5b7279..8ed6767ad 100644
--- a/app/auto_users.go
+++ b/app/auto_users.go
@@ -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
}
diff --git a/app/plugin.go b/app/plugin.go
index 9083f4785..189ca9b09 100644
--- a/app/plugin.go
+++ b/app/plugin.go
@@ -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")
diff --git a/app/plugin/ldapextras/plugin.go b/app/plugin/ldapextras/plugin.go
index 473ec6393..3af55c2ac 100644
--- a/app/plugin/ldapextras/plugin.go
+++ b/app/plugin/ldapextras/plugin.go
@@ -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)
}
diff --git a/app/team_test.go b/app/team_test.go
index 10f33f50b..084558fb4 100644
--- a/app/team_test.go
+++ b/app/team_test.go
@@ -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")
}
diff --git a/cmd/platform/config.go b/cmd/platform/config.go
index 0b3f26ee0..cd4356529 100644
--- a/cmd/platform/config.go
+++ b/cmd/platform/config.go
@@ -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
diff --git a/cmd/platform/init.go b/cmd/platform/init.go
index a25b39383..7d5593821 100644
--- a/cmd/platform/init.go
+++ b/cmd/platform/init.go
@@ -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
}
diff --git a/cmd/platform/server.go b/cmd/platform/server.go
index d2abbffb9..e240c6583 100644
--- a/cmd/platform/server.go
+++ b/cmd/platform/server.go
@@ -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
diff --git a/jobs/jobs.go b/jobs/jobs.go
index 7e49b2f48..f0a74e45a 100644
--- a/jobs/jobs.go
+++ b/jobs/jobs.go
@@ -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 {
diff --git a/model/utils.go b/model/utils.go
index e84d44f72..648e67c75 100644
--- a/model/utils.go
+++ b/model/utils.go
@@ -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
}
diff --git a/utils/config.go b/utils/config.go
index d75ec732b..83785f045 100644
--- a/utils/config.go
+++ b/utils/config.go
@@ -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()