Skip to content

Commit

Permalink
fix: improve error message when _session_store is missing from context (
Browse files Browse the repository at this point in the history
#67)

* fix: add proper error message when _session_store is missing from context

We need to know when _session_store is missing from context and the session
middleware is not properly initialized.
  • Loading branch information
imclem authored Dec 17, 2021
1 parent ed88076 commit 9602c6b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var (
func Get(name string, c echo.Context) (*sessions.Session, error) {
s := c.Get(key)
if s == nil {
return nil, fmt.Errorf("%q session not found", name)
return nil, fmt.Errorf("%q session store not found", key)
}
store := s.(sessions.Store)
return store.Get(c.Request(), name)
Expand Down
12 changes: 12 additions & 0 deletions session/session_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package session

import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -50,4 +51,15 @@ func TestMiddleware(t *testing.T) {
h = mw(handler)
assert.NoError(t, h(c))
assert.Contains(t, rec.Header().Get(echo.HeaderSetCookie), "labstack.com")

}

func TestGetSessionMissingStore(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(echo.GET, "/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
_, err := Get("test", c)

assert.EqualError(t, err, fmt.Sprintf("%q session store not found", key))
}

0 comments on commit 9602c6b

Please sign in to comment.