From b753e06f0b984a9a191ba3da79eeae9e99821665 Mon Sep 17 00:00:00 2001 From: Livio Amstutz Date: Mon, 30 Mar 2020 07:04:21 +0200 Subject: [PATCH] improve some functions --- internal/api/html/i18n.go | 14 +++++++------- internal/api/html/renderer.go | 5 ++--- internal/api/http/cookie.go | 5 +---- 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/internal/api/html/i18n.go b/internal/api/html/i18n.go index 17f1cb4e1..79b35ccc2 100644 --- a/internal/api/html/i18n.go +++ b/internal/api/html/i18n.go @@ -98,12 +98,12 @@ func (t *Translator) localizer(langs ...string) *i18n.Localizer { func (t *Translator) langsFromRequest(r *http.Request) []string { langs := make([]string, 0) - if r != nil { - lang, err := t.cookieHandler.GetCookieValue(r, t.cookieName) - if err == nil { - langs = append(langs, lang) - } - langs = append(langs, r.Header.Get(api.AcceptLanguage)) + if r == nil { + return langs } - return langs + lang, err := t.cookieHandler.GetCookieValue(r, t.cookieName) + if err == nil { + langs = append(langs, lang) + } + return append(langs, r.Header.Get(api.AcceptLanguage)) } diff --git a/internal/api/html/renderer.go b/internal/api/html/renderer.go index 182087e31..97a684263 100644 --- a/internal/api/html/renderer.go +++ b/internal/api/html/renderer.go @@ -31,9 +31,8 @@ func NewRenderer(templatesDir string, tmplMapping map[string]string, funcs map[s func (r *Renderer) RenderTemplate(w http.ResponseWriter, req *http.Request, tmpl *template.Template, data interface{}, reqFuncs map[string]interface{}) { reqFuncs = r.registerTranslateFn(req, reqFuncs) - if err := tmpl.Funcs(reqFuncs).Execute(w, data); err != nil { - logging.Log("HTML-lF8F6w").WithError(err).WithField("template", tmpl.Name).Error("error rendering template") - } + err := tmpl.Funcs(reqFuncs).Execute(w, data) + logging.LogWithFields("HTML-lF8F6w", "template", tmpl.Name).OnError(err).Error("error rendering template") } func (r *Renderer) Localize(id string, args map[string]interface{}) string { diff --git a/internal/api/http/cookie.go b/internal/api/http/cookie.go index a28791aaf..f2e28b529 100644 --- a/internal/api/http/cookie.go +++ b/internal/api/http/cookie.go @@ -85,10 +85,7 @@ func (c *CookieHandler) GetEncryptedCookieValue(r *http.Request, name string, va if c.securecookie == nil { return errors.ThrowInternal(nil, "HTTP-X6XpnL", "securecookie not configured") } - if err := c.securecookie.Decode(name, cookie.Value, value); err != nil { - return err - } - return nil + return c.securecookie.Decode(name, cookie.Value, value) } func (c *CookieHandler) SetCookie(w http.ResponseWriter, name string, value string) {