mirror of
https://github.com/astaxie/beego.git
synced 2024-11-23 18:10:54 +00:00
expose more error code in web module
This commit is contained in:
parent
4afa9d2d25
commit
8d7f48ea75
@ -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.
|
// 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 {
|
||||||
return (*web.Controller)(c).Input()
|
val, _ := (*web.Controller)(c).Input()
|
||||||
|
return val
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseForm maps input data map to obj struct.
|
// ParseForm maps input data map to obj struct.
|
||||||
|
@ -141,7 +141,7 @@ func (manager *Manager) GC() {
|
|||||||
|
|
||||||
// SessionRegenerateID Regenerate a session id for this SessionStore who's id is saving in http request.
|
// 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 {
|
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{
|
return &NewToOldStoreAdapter{
|
||||||
delegate: s,
|
delegate: s,
|
||||||
}
|
}
|
||||||
|
@ -49,12 +49,12 @@ func (f *StrTo) Set(v string) {
|
|||||||
|
|
||||||
// Clear string
|
// Clear string
|
||||||
func (f *StrTo) Clear() {
|
func (f *StrTo) Clear() {
|
||||||
*f = StrTo(0x1E)
|
*f = StrTo(rune(0x1E))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Exist check string exist
|
// Exist check string exist
|
||||||
func (f StrTo) Exist() bool {
|
func (f StrTo) Exist() bool {
|
||||||
return string(f) != string(0x1E)
|
return string(f) != string(rune(0x1E))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bool string to bool
|
// Bool string to bool
|
||||||
|
@ -67,4 +67,4 @@ func TestSnakeStringWithAcronym(t *testing.T) {
|
|||||||
t.Error("Unit Test Fail:", v, res, answer[v])
|
t.Error("Unit Test Fail:", v, res, answer[v])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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
|
// 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")
|
accept := output.Context.Input.Header("Accept")
|
||||||
switch accept {
|
switch accept {
|
||||||
case ApplicationYAML:
|
case ApplicationYAML:
|
||||||
output.YAML(data)
|
return output.YAML(data)
|
||||||
case ApplicationXML, TextXML:
|
case ApplicationXML, TextXML:
|
||||||
output.XML(data, hasIndent)
|
return output.XML(data, hasIndent)
|
||||||
default:
|
default:
|
||||||
output.JSON(data, hasIndent, len(hasEncode) > 0 && hasEncode[0])
|
return output.JSON(data, hasIndent, len(hasEncode) > 0 && hasEncode[0])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,6 +16,7 @@ package web
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
context2 "context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
@ -250,13 +251,16 @@ func (c *Controller) Render() error {
|
|||||||
// RenderString returns the rendered template string. Do not send out response.
|
// RenderString returns the rendered template string. Do not send out response.
|
||||||
func (c *Controller) RenderString() (string, error) {
|
func (c *Controller) RenderString() (string, error) {
|
||||||
b, e := c.RenderBytes()
|
b, e := c.RenderBytes()
|
||||||
|
if e != nil {
|
||||||
|
return "", e
|
||||||
|
}
|
||||||
return string(b), e
|
return string(b), e
|
||||||
}
|
}
|
||||||
|
|
||||||
// RenderBytes returns the bytes of rendered template string. Do not send out response.
|
// RenderBytes returns the bytes of rendered template string. Do not send out response.
|
||||||
func (c *Controller) RenderBytes() ([]byte, error) {
|
func (c *Controller) RenderBytes() ([]byte, error) {
|
||||||
buf, err := c.renderTemplate()
|
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 != "" {
|
if err == nil && c.Layout != "" {
|
||||||
c.Data["LayoutContent"] = template.HTML(buf.String())
|
c.Data["LayoutContent"] = template.HTML(buf.String())
|
||||||
|
|
||||||
@ -276,7 +280,7 @@ func (c *Controller) RenderBytes() ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
buf.Reset()
|
buf.Reset()
|
||||||
ExecuteViewPathTemplate(&buf, c.Layout, c.viewPath(), c.Data)
|
err = ExecuteViewPathTemplate(&buf, c.Layout, c.viewPath(), c.Data)
|
||||||
}
|
}
|
||||||
return buf.Bytes(), err
|
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.
|
// ServeJSON sends a json response with encoding charset.
|
||||||
func (c *Controller) ServeJSON(encoding ...bool) {
|
func (c *Controller) ServeJSON(encoding ...bool) error {
|
||||||
var (
|
var (
|
||||||
hasIndent = BConfig.RunMode != PROD
|
hasIndent = BConfig.RunMode != PROD
|
||||||
hasEncoding = len(encoding) > 0 && encoding[0]
|
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.
|
// ServeJSONP sends a jsonp response.
|
||||||
func (c *Controller) ServeJSONP() {
|
func (c *Controller) ServeJSONP() error {
|
||||||
hasIndent := BConfig.RunMode != PROD
|
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.
|
// ServeXML sends xml response.
|
||||||
func (c *Controller) ServeXML() {
|
func (c *Controller) ServeXML() error {
|
||||||
hasIndent := BConfig.RunMode != PROD
|
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.
|
// ServeYAML sends yaml response.
|
||||||
func (c *Controller) ServeYAML() {
|
func (c *Controller) ServeYAML() error {
|
||||||
c.Ctx.Output.YAML(c.Data["yaml"])
|
return c.Ctx.Output.YAML(c.Data["yaml"])
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServeFormatted serve YAML, XML OR JSON, depending on the value of the Accept header
|
// 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
|
hasIndent := BConfig.RunMode != PROD
|
||||||
hasEncoding := len(encoding) > 0 && encoding[0]
|
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.
|
// 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 {
|
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.
|
// ParseForm maps input data map to obj struct.
|
||||||
func (c *Controller) ParseForm(obj interface{}) error {
|
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
|
// 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]
|
defv = def[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
if f := c.Input(); f == nil {
|
if f, err := c.Input(); f == nil || err != nil {
|
||||||
return defv
|
return defv
|
||||||
} else if vs := f[key]; len(vs) > 0 {
|
} else if vs := f[key]; len(vs) > 0 {
|
||||||
return vs
|
return vs
|
||||||
@ -618,11 +629,11 @@ func (c *Controller) StartSession() session.Store {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SetSession puts value into session.
|
// 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 {
|
if c.CruSession == nil {
|
||||||
c.StartSession()
|
c.StartSession()
|
||||||
}
|
}
|
||||||
c.CruSession.Set(nil, name, value)
|
return c.CruSession.Set(context2.Background(), name, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetSession gets value from session.
|
// GetSession gets value from session.
|
||||||
@ -630,32 +641,38 @@ func (c *Controller) GetSession(name interface{}) interface{} {
|
|||||||
if c.CruSession == nil {
|
if c.CruSession == nil {
|
||||||
c.StartSession()
|
c.StartSession()
|
||||||
}
|
}
|
||||||
return c.CruSession.Get(nil, name)
|
return c.CruSession.Get(context2.Background(), name)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DelSession removes value from session.
|
// DelSession removes value from session.
|
||||||
func (c *Controller) DelSession(name interface{}) {
|
func (c *Controller) DelSession(name interface{}) error {
|
||||||
if c.CruSession == nil {
|
if c.CruSession == nil {
|
||||||
c.StartSession()
|
c.StartSession()
|
||||||
}
|
}
|
||||||
c.CruSession.Delete(nil, name)
|
return c.CruSession.Delete(context2.Background(), name)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SessionRegenerateID regenerates session id for this session.
|
// SessionRegenerateID regenerates session id for this session.
|
||||||
// the session data have no changes.
|
// the session data have no changes.
|
||||||
func (c *Controller) SessionRegenerateID() {
|
func (c *Controller) SessionRegenerateID() error {
|
||||||
if c.CruSession != nil {
|
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
|
c.Ctx.Input.CruSession = c.CruSession
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// DestroySession cleans session data and session cookie.
|
// DestroySession cleans session data and session cookie.
|
||||||
func (c *Controller) DestroySession() {
|
func (c *Controller) DestroySession() error {
|
||||||
c.Ctx.Input.CruSession.Flush(nil)
|
err := c.Ctx.Input.CruSession.Flush(nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
c.Ctx.Input.CruSession = nil
|
c.Ctx.Input.CruSession = nil
|
||||||
GlobalSessions.SessionDestroy(c.Ctx.ResponseWriter, c.Ctx.Request)
|
GlobalSessions.SessionDestroy(c.Ctx.ResponseWriter, c.Ctx.Request)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsAjax returns this request is ajax or not.
|
// IsAjax returns this request is ajax or not.
|
||||||
|
@ -298,15 +298,21 @@ func (manager *Manager) GC() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SessionRegenerateID Regenerate a session id for this SessionStore who's id is saving in http request.
|
// 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()
|
sid, err := manager.sessionID()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var session Store
|
||||||
|
|
||||||
cookie, err := r.Cookie(manager.config.CookieName)
|
cookie, err := r.Cookie(manager.config.CookieName)
|
||||||
if err != nil || cookie.Value == "" {
|
if err != nil || cookie.Value == "" {
|
||||||
//delete old cookie
|
//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,
|
cookie = &http.Cookie{Name: manager.config.CookieName,
|
||||||
Value: url.QueryEscape(sid),
|
Value: url.QueryEscape(sid),
|
||||||
Path: "/",
|
Path: "/",
|
||||||
@ -315,8 +321,16 @@ func (manager *Manager) SessionRegenerateID(w http.ResponseWriter, r *http.Reque
|
|||||||
Domain: manager.config.Domain,
|
Domain: manager.config.Domain,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
oldsid, _ := url.QueryUnescape(cookie.Value)
|
oldsid, err := url.QueryUnescape(cookie.Value)
|
||||||
session, _ = manager.provider.SessionRegenerate(nil, oldsid, sid)
|
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.Value = url.QueryEscape(sid)
|
||||||
cookie.HttpOnly = true
|
cookie.HttpOnly = true
|
||||||
cookie.Path = "/"
|
cookie.Path = "/"
|
||||||
@ -335,7 +349,7 @@ func (manager *Manager) SessionRegenerateID(w http.ResponseWriter, r *http.Reque
|
|||||||
w.Header().Set(manager.config.SessionNameInHTTPHeader, sid)
|
w.Header().Set(manager.config.SessionNameInHTTPHeader, sid)
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return session, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetActiveSession Get all active sessions count number.
|
// GetActiveSession Get all active sessions count number.
|
||||||
|
Loading…
Reference in New Issue
Block a user