diff options
author | Christian Pointner <equinox@helsinki.at> | 2015-12-29 01:38:25 (GMT) |
---|---|---|
committer | Christian Pointner <equinox@helsinki.at> | 2015-12-29 01:38:25 (GMT) |
commit | 46ae2f5c6db5897c00d75ce8d29cc0ad55b7b613 (patch) | |
tree | 3c9d594f75339d53f454269a92873db73068e3bb | |
parent | 3a6efe6d413c24b914d2b7889eb520b3f1a0820f (diff) |
improved error handling for session store
-rw-r--r-- | session.go | 2 | ||||
-rw-r--r-- | session_store.go | 52 |
2 files changed, 31 insertions, 23 deletions
@@ -190,7 +190,7 @@ func (self *Session) dispatchRequests() { self.cancel() } self.state = SESSION_TIMEOUT - r := &ImportResult{500, "timeout", 0, 0} + r := &ImportResult{500, "session timed out", 0, 0} self.callDoneHandler(r) if self.removeFunc != nil { self.removeFunc() diff --git a/session_store.go b/session_store.go index 43b5d23..b7ea3d9 100644 --- a/session_store.go +++ b/session_store.go @@ -28,13 +28,15 @@ import ( "encoding/base32" "fmt" "github.com/satori/go.uuid" + "net/http" "strings" ) type newSessionResponse struct { - id string - session *SessionChan - err error + id string + session *SessionChan + responsecode int + errorstring string } type newSessionRequest struct { @@ -43,8 +45,9 @@ type newSessionRequest struct { } type getSessionResponse struct { - session *SessionChan - err error + session *SessionChan + responsecode int + errorstring string } type getSessionRequest struct { @@ -54,7 +57,8 @@ type getSessionRequest struct { } type removeSessionResponse struct { - err error + responsecode int + errorstring string } type removeSessionRequest struct { @@ -73,12 +77,16 @@ type SessionStore struct { } func (self *SessionStore) new(ctx *ImportContext) (resp newSessionResponse) { + resp.responsecode = http.StatusOK + resp.errorstring = "OK" if !ctx.Trusted { if ok, err := ctx.rddb.CheckPassword(ctx.UserName, ctx.Password); err != nil { - resp.err = err + resp.responsecode = http.StatusInternalServerError + resp.errorstring = err.Error() return } else if !ok { - resp.err = fmt.Errorf("invalid username and/or password") + resp.responsecode = http.StatusUnauthorized + resp.errorstring = "invalid username and/or password" return } } @@ -94,15 +102,20 @@ func (self *SessionStore) new(ctx *ImportContext) (resp newSessionResponse) { } func (self *SessionStore) get(user, id string) (resp getSessionResponse) { + resp.responsecode = http.StatusOK + resp.errorstring = "OK" if session, exists := self.store[user][id]; exists { resp.session = session.getInterface() } else { - resp.err = fmt.Errorf("SessionStore: session '%s/%s' not found", user, id) + resp.responsecode = http.StatusNotFound + resp.errorstring = fmt.Sprintf("SessionStore: session '%s/%s' not found", user, id) } return } func (self *SessionStore) remove(user, id string) (resp removeSessionResponse) { + resp.responsecode = http.StatusOK + resp.errorstring = "OK" if session, exists := self.store[user][id]; exists { go session.Cleanup() // cleanup could take a while -> don't block all the other stuff delete(self.store[user], id) @@ -114,7 +127,8 @@ func (self *SessionStore) remove(user, id string) (resp removeSessionResponse) { } } } else { - resp.err = fmt.Errorf("SessionStore: session '%s/%s' not found", user, id) + resp.responsecode = http.StatusNotFound + resp.errorstring = fmt.Sprintf("SessionStore: session '%s/%s' not found", user, id) } return } @@ -144,7 +158,7 @@ type SessionStoreChan struct { removeChan chan<- removeSessionRequest } -func (self *SessionStoreChan) New(ctx *ImportContext) (string, *SessionChan, error) { +func (self *SessionStoreChan) New(ctx *ImportContext) (string, *SessionChan, int, string) { res_ch := make(chan newSessionResponse) req := newSessionRequest{} req.ctx = ctx @@ -152,13 +166,10 @@ func (self *SessionStoreChan) New(ctx *ImportContext) (string, *SessionChan, err self.newChan <- req res := <-res_ch - if res.err != nil { - return "", nil, res.err - } - return res.id, res.session, nil + return res.id, res.session, res.responsecode, res.errorstring } -func (self *SessionStoreChan) Get(user, id string) (*SessionChan, error) { +func (self *SessionStoreChan) Get(user, id string) (*SessionChan, int, string) { res_ch := make(chan getSessionResponse) req := getSessionRequest{} req.user = user @@ -167,13 +178,10 @@ func (self *SessionStoreChan) Get(user, id string) (*SessionChan, error) { self.getChan <- req res := <-res_ch - if res.err != nil { - return nil, res.err - } - return res.session, nil + return res.session, res.responsecode, res.errorstring } -func (self *SessionStoreChan) Remove(user, id string) error { +func (self *SessionStoreChan) Remove(user, id string) (int, string) { res_ch := make(chan removeSessionResponse) req := removeSessionRequest{} req.user = user @@ -182,7 +190,7 @@ func (self *SessionStoreChan) Remove(user, id string) error { self.removeChan <- req res := <-res_ch - return res.err + return res.responsecode, res.errorstring } func (self *SessionStore) GetInterface() *SessionStoreChan { |