From 46ae2f5c6db5897c00d75ce8d29cc0ad55b7b613 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Tue, 29 Dec 2015 02:38:25 +0100 Subject: improved error handling for session store diff --git a/session.go b/session.go index 71251f1..94ce66a 100644 --- a/session.go +++ b/session.go @@ -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 { -- cgit v0.10.2