summaryrefslogtreecommitdiffstats
path: root/utils
diff options
context:
space:
mode:
authorHarrison Healey <harrisonmhealey@gmail.com>2017-05-23 11:06:25 -0400
committerGitHub <noreply@github.com>2017-05-23 11:06:25 -0400
commit5c1049054eace710abd3418bbad141fbb7dd5d7f (patch)
tree24d75c14ce2aae2c6f1a8d5bc7392e958e416666 /utils
parent69f3f2fdce4ae21a037ca61d753279efcc70f0ec (diff)
downloadchat-5c1049054eace710abd3418bbad141fbb7dd5d7f.tar.gz
chat-5c1049054eace710abd3418bbad141fbb7dd5d7f.tar.bz2
chat-5c1049054eace710abd3418bbad141fbb7dd5d7f.zip
PLT-6471 Properly panic when translations can't be loaded (#6414)
* PLT-6471 Properly panic when translations can't be loaded * Print usage messages when errors occur during CLI initialization * Reverted behaviour of FindDir and added second return value to it * Fixed merge conflict
Diffstat (limited to 'utils')
-rw-r--r--utils/config.go25
-rw-r--r--utils/html.go2
-rw-r--r--utils/i18n.go40
-rw-r--r--utils/license.go3
4 files changed, 46 insertions, 24 deletions
diff --git a/utils/config.go b/utils/config.go
index 95cfc43aa..c0771933d 100644
--- a/utils/config.go
+++ b/utils/config.go
@@ -78,17 +78,21 @@ func FindConfigFile(fileName string) string {
return fileName
}
-func FindDir(dir string) string {
+func FindDir(dir string) (string, bool) {
fileName := "."
+ found := false
if _, err := os.Stat("./" + dir + "/"); err == nil {
fileName, _ = filepath.Abs("./" + dir + "/")
+ found = true
} else if _, err := os.Stat("../" + dir + "/"); err == nil {
fileName, _ = filepath.Abs("../" + dir + "/")
+ found = true
} else if _, err := os.Stat("../../" + dir + "/"); err == nil {
fileName, _ = filepath.Abs("../../" + dir + "/")
+ found = true
}
- return fileName + "/"
+ return fileName + "/", found
}
func DisableDebugLogForTest() {
@@ -161,7 +165,8 @@ func configureLog(s *model.LogSettings) {
func GetLogFileLocation(fileLocation string) string {
if fileLocation == "" {
- return FindDir("logs") + LOG_FILENAME
+ logDir, _ := FindDir("logs")
+ return logDir + LOG_FILENAME
} else {
return fileLocation + LOG_FILENAME
}
@@ -258,19 +263,17 @@ func DisableConfigWatch() {
}
}
-func InitAndLoadConfig(filename string) (err string) {
- defer func() {
- if r := recover(); r != nil {
- err = fmt.Sprintf("%v", r)
- }
- }()
- TranslationsPreInit()
+func InitAndLoadConfig(filename string) error {
+ if err := TranslationsPreInit(); err != nil {
+ return err
+ }
+
EnableConfigFromEnviromentVars()
LoadConfig(filename)
InitializeConfigWatch()
EnableConfigWatch()
- return ""
+ return nil
}
// LoadConfig will try to search around for the corresponding config file.
diff --git a/utils/html.go b/utils/html.go
index c902030d8..e6050f62a 100644
--- a/utils/html.go
+++ b/utils/html.go
@@ -33,7 +33,7 @@ func InitHTMLWithDir(dir string) {
return
}
- templatesDir := FindDir(dir)
+ templatesDir, _ := FindDir(dir)
l4g.Debug(T("api.api.init.parsing_templates.debug"), templatesDir)
var err error
if htmlTemplates, err = template.ParseGlob(templatesDir + "*.html"); err != nil {
diff --git a/utils/i18n.go b/utils/i18n.go
index cb5aca568..8b22dbeb7 100644
--- a/utils/i18n.go
+++ b/utils/i18n.go
@@ -1,6 +1,7 @@
package utils
import (
+ "fmt"
"io/ioutil"
"net/http"
"path/filepath"
@@ -18,30 +19,47 @@ var settings model.LocalizationSettings
// this functions loads translations from filesystem
// and assign english while loading server config
-func TranslationsPreInit() {
- InitTranslationsWithDir("i18n")
+func TranslationsPreInit() error {
+ if err := InitTranslationsWithDir("i18n"); err != nil {
+ return err
+ }
+
T = TfuncWithFallback("en")
TDefault = TfuncWithFallback("en")
+
+ return nil
}
-func InitTranslations(localizationSettings model.LocalizationSettings) {
+func InitTranslations(localizationSettings model.LocalizationSettings) error {
settings = localizationSettings
- T = GetTranslationsBySystemLocale()
+
+ var err error
+ T, err = GetTranslationsBySystemLocale()
+ return err
}
-func InitTranslationsWithDir(dir string) {
- i18nDirectory := FindDir(dir)
+func InitTranslationsWithDir(dir string) error {
+ i18nDirectory, found := FindDir(dir)
+ if !found {
+ return fmt.Errorf("Unable to find i18n directory")
+ }
+
files, _ := ioutil.ReadDir(i18nDirectory)
for _, f := range files {
if filepath.Ext(f.Name()) == ".json" {
filename := f.Name()
locales[strings.Split(filename, ".")[0]] = i18nDirectory + filename
- i18n.MustLoadTranslationFile(i18nDirectory + filename)
+
+ if err := i18n.LoadTranslationFile(i18nDirectory + filename); err != nil {
+ return err
+ }
}
}
+
+ return nil
}
-func GetTranslationsBySystemLocale() i18n.TranslateFunc {
+func GetTranslationsBySystemLocale() (i18n.TranslateFunc, error) {
locale := *settings.DefaultServerLocale
if _, ok := locales[locale]; !ok {
l4g.Error("Failed to load system translations for '%v' attempting to fall back to '%v'", locale, model.DEFAULT_LOCALE)
@@ -49,16 +67,16 @@ func GetTranslationsBySystemLocale() i18n.TranslateFunc {
}
if locales[locale] == "" {
- panic("Failed to load system translations for '" + model.DEFAULT_LOCALE + "'")
+ return nil, fmt.Errorf("Failed to load system translations for '%v'", model.DEFAULT_LOCALE)
}
translations := TfuncWithFallback(locale)
if translations == nil {
- panic("Failed to load system translations")
+ return nil, fmt.Errorf("Failed to load system translations")
}
l4g.Info(translations("utils.i18n.loaded"), locale, locales[locale])
- return translations
+ return translations, nil
}
func GetUserTranslations(locale string) i18n.TranslateFunc {
diff --git a/utils/license.go b/utils/license.go
index c0a17bf79..03a9d7ab3 100644
--- a/utils/license.go
+++ b/utils/license.go
@@ -152,7 +152,8 @@ func GetLicenseFileFromDisk(fileName string) []byte {
func GetLicenseFileLocation(fileLocation string) string {
if fileLocation == "" {
- return FindDir("config") + "mattermost.mattermost-license"
+ configDir, _ := FindDir("config")
+ return configDir + "mattermost.mattermost-license"
} else {
return fileLocation
}