Skip to content

Commit 9602c6b

Browse files
authored
fix: improve error message when _session_store is missing from context (#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.
1 parent ed88076 commit 9602c6b

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

session/session.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ var (
3636
func Get(name string, c echo.Context) (*sessions.Session, error) {
3737
s := c.Get(key)
3838
if s == nil {
39-
return nil, fmt.Errorf("%q session not found", name)
39+
return nil, fmt.Errorf("%q session store not found", key)
4040
}
4141
store := s.(sessions.Store)
4242
return store.Get(c.Request(), name)

session/session_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package session
22

33
import (
4+
"fmt"
45
"net/http"
56
"net/http/httptest"
67
"testing"
@@ -50,4 +51,15 @@ func TestMiddleware(t *testing.T) {
5051
h = mw(handler)
5152
assert.NoError(t, h(c))
5253
assert.Contains(t, rec.Header().Get(echo.HeaderSetCookie), "labstack.com")
54+
55+
}
56+
57+
func TestGetSessionMissingStore(t *testing.T) {
58+
e := echo.New()
59+
req := httptest.NewRequest(echo.GET, "/", nil)
60+
rec := httptest.NewRecorder()
61+
c := e.NewContext(req, rec)
62+
_, err := Get("test", c)
63+
64+
assert.EqualError(t, err, fmt.Sprintf("%q session store not found", key))
5365
}

0 commit comments

Comments
 (0)