From e781e9861b4183bbdafa22b881bc13800b29eb09 Mon Sep 17 00:00:00 2001 From: Aaron Lindsay Date: Wed, 11 Oct 2017 05:49:08 -0400 Subject: [PATCH] sessions: Return deletion errors to user --- internal/handlers/sessions.go | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/internal/handlers/sessions.go b/internal/handlers/sessions.go index bbc5ee8..2cfa56a 100644 --- a/internal/handlers/sessions.go +++ b/internal/handlers/sessions.go @@ -44,12 +44,16 @@ func GetSession(db *DB, r *http.Request) (*Session, error) { return &s, nil } -func DeleteSessionIfExists(db *DB, r *http.Request) { +func DeleteSessionIfExists(db *DB, r *http.Request) error { // TODO do this in one transaction session, err := GetSession(db, r) if err == nil { - db.Delete(session) + _, err := db.Delete(session) + if err != nil { + return err + } } + return nil } func NewSessionCookie() (string, error) { @@ -116,7 +120,12 @@ func SessionHandler(w http.ResponseWriter, r *http.Request, db *DB) { return } - DeleteSessionIfExists(db, r) + err = DeleteSessionIfExists(db, r) + if err != nil { + WriteError(w, 999 /*Internal Error*/) + log.Print(err) + return + } session, err := NewSession(db, w, r, dbuser.UserId) if err != nil { @@ -139,7 +148,12 @@ func SessionHandler(w http.ResponseWriter, r *http.Request, db *DB) { s.Write(w) } else if r.Method == "DELETE" { - DeleteSessionIfExists(db, r) + err := DeleteSessionIfExists(db, r) + if err != nil { + WriteError(w, 999 /*Internal Error*/) + log.Print(err) + return + } WriteSuccess(w) } }