From 8d7f48ea75f97886e6dd9ef62f455d8b9b48c661 Mon Sep 17 00:00:00 2001 From: Ming Deng Date: Sun, 29 Nov 2020 21:16:53 +0800 Subject: [PATCH] expose more error code in web module --- adapter/controller.go | 3 +- adapter/session/session.go | 2 +- client/orm/utils.go | 4 +- client/orm/utils_test.go | 2 +- server/web/context/output.go | 8 ++-- server/web/controller.go | 71 ++++++++++++++++++++++------------- server/web/session/session.go | 26 ++++++++++--- 7 files changed, 74 insertions(+), 42 deletions(-) diff --git a/adapter/controller.go b/adapter/controller.go index 14dc9b97..a56d1743 100644 --- a/adapter/controller.go +++ b/adapter/controller.go @@ -212,7 +212,8 @@ func (c *Controller) ServeFormatted(encoding ...bool) { // Input returns the input data map from POST or PUT request body and query string. func (c *Controller) Input() url.Values { - return (*web.Controller)(c).Input() + val, _ := (*web.Controller)(c).Input() + return val } // ParseForm maps input data map to obj struct. diff --git a/adapter/session/session.go b/adapter/session/session.go index d8b151b7..72eeb080 100644 --- a/adapter/session/session.go +++ b/adapter/session/session.go @@ -141,7 +141,7 @@ func (manager *Manager) GC() { // SessionRegenerateID Regenerate a session id for this SessionStore who's id is saving in http request. func (manager *Manager) SessionRegenerateID(w http.ResponseWriter, r *http.Request) Store { - s := (*session.Manager)(manager).SessionRegenerateID(w, r) + s, _ := (*session.Manager)(manager).SessionRegenerateID(w, r) return &NewToOldStoreAdapter{ delegate: s, } diff --git a/client/orm/utils.go b/client/orm/utils.go index 3ff76772..d6c0a8e8 100644 --- a/client/orm/utils.go +++ b/client/orm/utils.go @@ -49,12 +49,12 @@ func (f *StrTo) Set(v string) { // Clear string func (f *StrTo) Clear() { - *f = StrTo(0x1E) + *f = StrTo(rune(0x1E)) } // Exist check string exist func (f StrTo) Exist() bool { - return string(f) != string(0x1E) + return string(f) != string(rune(0x1E)) } // Bool string to bool diff --git a/client/orm/utils_test.go b/client/orm/utils_test.go index 7d94cada..fa75258a 100644 --- a/client/orm/utils_test.go +++ b/client/orm/utils_test.go @@ -67,4 +67,4 @@ func TestSnakeStringWithAcronym(t *testing.T) { t.Error("Unit Test Fail:", v, res, answer[v]) } } -} +} \ No newline at end of file diff --git a/server/web/context/output.go b/server/web/context/output.go index a6e83681..28ff2819 100644 --- a/server/web/context/output.go +++ b/server/web/context/output.go @@ -261,15 +261,15 @@ func (output *BeegoOutput) XML(data interface{}, hasIndent bool) error { } // ServeFormatted serves YAML, XML or JSON, depending on the value of the Accept header -func (output *BeegoOutput) ServeFormatted(data interface{}, hasIndent bool, hasEncode ...bool) { +func (output *BeegoOutput) ServeFormatted(data interface{}, hasIndent bool, hasEncode ...bool) error { accept := output.Context.Input.Header("Accept") switch accept { case ApplicationYAML: - output.YAML(data) + return output.YAML(data) case ApplicationXML, TextXML: - output.XML(data, hasIndent) + return output.XML(data, hasIndent) default: - output.JSON(data, hasIndent, len(hasEncode) > 0 && hasEncode[0]) + return output.JSON(data, hasIndent, len(hasEncode) > 0 && hasEncode[0]) } } diff --git a/server/web/controller.go b/server/web/controller.go index 3a1b9837..f363c655 100644 --- a/server/web/controller.go +++ b/server/web/controller.go @@ -16,6 +16,7 @@ package web import ( "bytes" + context2 "context" "errors" "fmt" "html/template" @@ -250,13 +251,16 @@ func (c *Controller) Render() error { // RenderString returns the rendered template string. Do not send out response. func (c *Controller) RenderString() (string, error) { b, e := c.RenderBytes() + if e != nil { + return "", e + } return string(b), e } // RenderBytes returns the bytes of rendered template string. Do not send out response. func (c *Controller) RenderBytes() ([]byte, error) { buf, err := c.renderTemplate() - //if the controller has set layout, then first get the tplName's content set the content to the layout + // if the controller has set layout, then first get the tplName's content set the content to the layout if err == nil && c.Layout != "" { c.Data["LayoutContent"] = template.HTML(buf.String()) @@ -276,7 +280,7 @@ func (c *Controller) RenderBytes() ([]byte, error) { } buf.Reset() - ExecuteViewPathTemplate(&buf, c.Layout, c.viewPath(), c.Data) + err = ExecuteViewPathTemplate(&buf, c.Layout, c.viewPath(), c.Data) } return buf.Bytes(), err } @@ -373,50 +377,57 @@ func (c *Controller) URLFor(endpoint string, values ...interface{}) string { } // ServeJSON sends a json response with encoding charset. -func (c *Controller) ServeJSON(encoding ...bool) { +func (c *Controller) ServeJSON(encoding ...bool) error { var ( hasIndent = BConfig.RunMode != PROD hasEncoding = len(encoding) > 0 && encoding[0] ) - c.Ctx.Output.JSON(c.Data["json"], hasIndent, hasEncoding) + return c.Ctx.Output.JSON(c.Data["json"], hasIndent, hasEncoding) } // ServeJSONP sends a jsonp response. -func (c *Controller) ServeJSONP() { +func (c *Controller) ServeJSONP() error { hasIndent := BConfig.RunMode != PROD - c.Ctx.Output.JSONP(c.Data["jsonp"], hasIndent) + return c.Ctx.Output.JSONP(c.Data["jsonp"], hasIndent) } // ServeXML sends xml response. -func (c *Controller) ServeXML() { +func (c *Controller) ServeXML() error { hasIndent := BConfig.RunMode != PROD - c.Ctx.Output.XML(c.Data["xml"], hasIndent) + return c.Ctx.Output.XML(c.Data["xml"], hasIndent) } // ServeYAML sends yaml response. -func (c *Controller) ServeYAML() { - c.Ctx.Output.YAML(c.Data["yaml"]) +func (c *Controller) ServeYAML() error { + return c.Ctx.Output.YAML(c.Data["yaml"]) } // ServeFormatted serve YAML, XML OR JSON, depending on the value of the Accept header -func (c *Controller) ServeFormatted(encoding ...bool) { +func (c *Controller) ServeFormatted(encoding ...bool) error { hasIndent := BConfig.RunMode != PROD hasEncoding := len(encoding) > 0 && encoding[0] - c.Ctx.Output.ServeFormatted(c.Data, hasIndent, hasEncoding) + return c.Ctx.Output.ServeFormatted(c.Data, hasIndent, hasEncoding) } // Input returns the input data map from POST or PUT request body and query string. -func (c *Controller) Input() url.Values { +func (c *Controller) Input() (url.Values, error) { if c.Ctx.Request.Form == nil { - c.Ctx.Request.ParseForm() + err := c.Ctx.Request.ParseForm() + if err != nil { + return nil, err + } } - return c.Ctx.Request.Form + return c.Ctx.Request.Form, nil } // ParseForm maps input data map to obj struct. func (c *Controller) ParseForm(obj interface{}) error { - return ParseForm(c.Input(), obj) + form, err := c.Input() + if err != nil { + return err + } + return ParseForm(form, obj) } // GetString returns the input value by key string or the default value while it's present and input is blank @@ -438,7 +449,7 @@ func (c *Controller) GetStrings(key string, def ...[]string) []string { defv = def[0] } - if f := c.Input(); f == nil { + if f, err := c.Input(); f == nil || err != nil { return defv } else if vs := f[key]; len(vs) > 0 { return vs @@ -618,11 +629,11 @@ func (c *Controller) StartSession() session.Store { } // SetSession puts value into session. -func (c *Controller) SetSession(name interface{}, value interface{}) { +func (c *Controller) SetSession(name interface{}, value interface{}) error { if c.CruSession == nil { c.StartSession() } - c.CruSession.Set(nil, name, value) + return c.CruSession.Set(context2.Background(), name, value) } // GetSession gets value from session. @@ -630,32 +641,38 @@ func (c *Controller) GetSession(name interface{}) interface{} { if c.CruSession == nil { c.StartSession() } - return c.CruSession.Get(nil, name) + return c.CruSession.Get(context2.Background(), name) } // DelSession removes value from session. -func (c *Controller) DelSession(name interface{}) { +func (c *Controller) DelSession(name interface{}) error { if c.CruSession == nil { c.StartSession() } - c.CruSession.Delete(nil, name) + return c.CruSession.Delete(context2.Background(), name) } // SessionRegenerateID regenerates session id for this session. // the session data have no changes. -func (c *Controller) SessionRegenerateID() { +func (c *Controller) SessionRegenerateID() error { if c.CruSession != nil { - c.CruSession.SessionRelease(nil, c.Ctx.ResponseWriter) + c.CruSession.SessionRelease(context2.Background(), c.Ctx.ResponseWriter) } - c.CruSession = GlobalSessions.SessionRegenerateID(c.Ctx.ResponseWriter, c.Ctx.Request) + var err error + c.CruSession, err = GlobalSessions.SessionRegenerateID(c.Ctx.ResponseWriter, c.Ctx.Request) c.Ctx.Input.CruSession = c.CruSession + return err } // DestroySession cleans session data and session cookie. -func (c *Controller) DestroySession() { - c.Ctx.Input.CruSession.Flush(nil) +func (c *Controller) DestroySession() error { + err := c.Ctx.Input.CruSession.Flush(nil) + if err != nil { + return err + } c.Ctx.Input.CruSession = nil GlobalSessions.SessionDestroy(c.Ctx.ResponseWriter, c.Ctx.Request) + return nil } // IsAjax returns this request is ajax or not. diff --git a/server/web/session/session.go b/server/web/session/session.go index bb7e5bd6..60fc0e00 100644 --- a/server/web/session/session.go +++ b/server/web/session/session.go @@ -298,15 +298,21 @@ func (manager *Manager) GC() { } // SessionRegenerateID Regenerate a session id for this SessionStore who's id is saving in http request. -func (manager *Manager) SessionRegenerateID(w http.ResponseWriter, r *http.Request) (session Store) { +func (manager *Manager) SessionRegenerateID(w http.ResponseWriter, r *http.Request) (Store, error) { sid, err := manager.sessionID() if err != nil { - return + return nil, err } + + var session Store + cookie, err := r.Cookie(manager.config.CookieName) if err != nil || cookie.Value == "" { //delete old cookie - session, _ = manager.provider.SessionRead(nil, sid) + session, err = manager.provider.SessionRead(nil, sid) + if err != nil { + return nil, err + } cookie = &http.Cookie{Name: manager.config.CookieName, Value: url.QueryEscape(sid), Path: "/", @@ -315,8 +321,16 @@ func (manager *Manager) SessionRegenerateID(w http.ResponseWriter, r *http.Reque Domain: manager.config.Domain, } } else { - oldsid, _ := url.QueryUnescape(cookie.Value) - session, _ = manager.provider.SessionRegenerate(nil, oldsid, sid) + oldsid, err := url.QueryUnescape(cookie.Value) + if err != nil { + return nil, err + } + + session, err = manager.provider.SessionRegenerate(nil, oldsid, sid) + if err != nil { + return nil, err + } + cookie.Value = url.QueryEscape(sid) cookie.HttpOnly = true cookie.Path = "/" @@ -335,7 +349,7 @@ func (manager *Manager) SessionRegenerateID(w http.ResponseWriter, r *http.Reque w.Header().Set(manager.config.SessionNameInHTTPHeader, sid) } - return + return session, nil } // GetActiveSession Get all active sessions count number.