[MM-45991] Check and return JSON errors (#20735)

Этот коммит содержится в:
Tim Scheuermann
2022-08-10 11:56:58 +03:00
коммит произвёл GitHub
родитель fdee1df6fe
Коммит 1738bd6e92
65 изменённых файлов: 2068 добавлений и 1938 удалений

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

@@ -91,23 +91,21 @@ func (o *Preference) IsValid() *AppError {
if o.Category == PreferenceCategoryTheme {
var unused map[string]string
if err := json.NewDecoder(strings.NewReader(o.Value)).Decode(&unused); err != nil {
return NewAppError("Preference.IsValid", "model.preference.is_valid.theme.app_error", nil, "value="+o.Value, http.StatusBadRequest)
return NewAppError("Preference.IsValid", "model.preference.is_valid.theme.app_error", nil, "value="+o.Value, http.StatusBadRequest).Wrap(err)
}
}
return nil
}
var preUpdateColorPattern = regexp.MustCompile(`^#[0-9a-fA-F]{3}([0-9a-fA-F]{3})?$`)
func (o *Preference) PreUpdate() {
if o.Category == PreferenceCategoryTheme {
// decode the value of theme (a map of strings to string) and eliminate any invalid values
var props map[string]string
if err := json.NewDecoder(strings.NewReader(o.Value)).Decode(&props); err != nil {
// just continue, the invalid preference value should get caught by IsValid before saving
return
}
colorPattern := regexp.MustCompile(`^#[0-9a-fA-F]{3}([0-9a-fA-F]{3})?$`)
// just continue, the invalid preference value should get caught by IsValid before saving
json.NewDecoder(strings.NewReader(o.Value)).Decode(&props)
// blank out any invalid theme values
for name, value := range props {
@@ -115,7 +113,7 @@ func (o *Preference) PreUpdate() {
continue
}
if !colorPattern.MatchString(value) {
if !preUpdateColorPattern.MatchString(value) {
props[name] = "#ffffff"
}
}