From 765566d593bea3fa6091eab5f3aa0960053eb4fe 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/src/helsinki.at/rhimport/session.go b/src/helsinki.at/rhimport/session.go index 71251f1..94ce66a 100644 --- a/src/helsinki.at/rhimport/session.go +++ b/src/helsinki.at/rhimport/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/src/helsinki.at/rhimport/session_store.go b/src/helsinki.at/rhimport/session_store.go index 43b5d23..b7ea3d9 100644 --- a/src/helsinki.at/rhimport/session_store.go +++ b/src/helsinki.at/rhimport/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 { diff --git a/src/helsinki.at/rhimportd/main.go b/src/helsinki.at/rhimportd/main.go index 362e8dc..8a212f9 100644 --- a/src/helsinki.at/rhimportd/main.go +++ b/src/helsinki.at/rhimportd/main.go @@ -27,13 +27,13 @@ package main import ( "flag" "fmt" + "helsinki.at/rhimport" "log" + "net/http" "os" "os/signal" "sync" "time" - - "helsinki.at/rhimport" // "io/ioutil" ) @@ -86,15 +86,15 @@ func session_test(conf *rhimport.Config, rddb *rhimport.RdDbChan) { ctx.ClearShowCarts = true ctx.SourceUri = "http://www.tonycuffe.com/mp3/tail%20toddle.mp3" - id, _, err := store.New(ctx) - if err != nil { - rhl.Printf("MAIN: Error SessionStore.New(): %s", err) + id, _, code, errstring := store.New(ctx) + if code != http.StatusOK { + rhl.Printf("MAIN: Error SessionStore.New(): %s", errstring) return } var session *rhimport.SessionChan - if session, err = store.Get(ctx.UserName, id); err != nil { - rhl.Printf("MAIN: Error SessionStore.Get(): %s", err) + if session, code, errstring = store.Get(ctx.UserName, id); code != http.StatusOK { + rhl.Printf("MAIN: Error SessionStore.Get(): %s", errstring) return } @@ -104,24 +104,24 @@ func session_test(conf *rhimport.Config, rddb *rhimport.RdDbChan) { return } - // pch := make(chan string, 10) - // if err = session.AddProgressHandler((chan<- string)(pch), session_test_progress1); err != nil { - // rhl.Printf("MAIN: Error Session.AddProgressHandler(): %s", err) - // } - // go func() { - // time.Sleep(3 * time.Second) - // if err = session.AddProgressHandler((chan<- string)(pch), session_test_progress2); err != nil { - // rhl.Printf("MAIN: Error Session.AddProgressHandler(): %s", err) - // } - // }() + pch := make(chan string, 10) + if err = session.AddProgressHandler((chan<- string)(pch), session_test_progress1); err != nil { + rhl.Printf("MAIN: Error Session.AddProgressHandler(): %s", err) + } + go func() { + time.Sleep(3 * time.Second) + if err = session.AddProgressHandler((chan<- string)(pch), session_test_progress2); err != nil { + rhl.Printf("MAIN: Error Session.AddProgressHandler(): %s", err) + } + }() rhl.Printf("MAIN: calling run for %s/%s", ctx.UserName, id) - session.Run(10 * time.Second) + session.Run(30 * time.Second) for { select { - // case p := <-pch: - // fmt.Println(p) + case p := <-pch: + fmt.Println(p) case r := <-dch: fmt.Printf("Import finished: %+v\n", r) break @@ -129,8 +129,8 @@ func session_test(conf *rhimport.Config, rddb *rhimport.RdDbChan) { } rhl.Printf("MAIN: calling remove for %s/%s", ctx.UserName, id) - if err = store.Remove(ctx.UserName, id); err != nil { - rhl.Printf("MAIN: Error SessionStore.Remove(): %s", err) + if code, errstring = store.Remove(ctx.UserName, id); code != http.StatusOK { + rhl.Printf("MAIN: Error SessionStore.Remove(): %s", errstring) } rhl.Printf("MAIN: remove done") @@ -164,7 +164,7 @@ func main() { } defer rddb.Cleanup() - // go session_test(conf, rddb.GetInterface()) + go session_test(conf, rddb.GetInterface()) var wg sync.WaitGroup -- cgit v0.10.2