Config: Show user-friendly error message instead of stack trace (#16564)

Fixes #16283
This commit is contained in:
Hofls
2019-04-25 08:29:07 +02:00
committed by Carl Bergquist
parent afd55cb411
commit b3bfbc6f77
3 changed files with 266 additions and 57 deletions
+27
View File
@@ -1,7 +1,9 @@
package setting
import (
"gopkg.in/ini.v1"
"os"
"path"
"path/filepath"
"runtime"
"testing"
@@ -189,4 +191,29 @@ func TestLoadingSettings(t *testing.T) {
So(cfg.RendererCallbackUrl, ShouldEqual, "http://myserver/renderer/")
})
})
Convey("Test reading string values from .ini file", t, func() {
iniFile, err := ini.Load(path.Join(HomePath, "pkg/setting/testdata/invalid.ini"))
So(err, ShouldBeNil)
Convey("If key is found - should return value from ini file", func() {
value, err := valueAsString(iniFile.Section("server"), "alt_url", "")
So(err, ShouldBeNil)
So(value, ShouldEqual, "https://grafana.com/")
})
Convey("If key is not found - should return default value", func() {
value, err := valueAsString(iniFile.Section("server"), "extra_url", "default_url_val")
So(err, ShouldBeNil)
So(value, ShouldEqual, "default_url_val")
})
Convey("In case of panic - should return user-friendly error", func() {
value, err := valueAsString(iniFile.Section("server"), "root_url", "")
So(err.Error(), ShouldEqual, "Invalid value for key 'root_url' in configuration file")
So(value, ShouldEqual, "")
})
})
}