Skip to content

Commit caab7e8

Browse files
Merge pull request #370 from supertokens/no-panic
fix: no panic in middleware
2 parents a311ca0 + 9257488 commit caab7e8

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
- It defaults to `60` or the value set in the cache-control header returned by the core
1616
- This is optional (so you are not required to update your overrides). Returning undefined means that the header is not set.
1717
- Handle AWS Public URLs (ending with `.amazonaws.com`) separately while extracting TLDs for SameSite attribute.
18+
- Return `500` status instead of panic when `supertokens.Middleware` is used without initializing the SDK.
1819
- Updates fiber adaptor package in the fiber example.
1920

2021
## [0.14.0] - 2023-09-11
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package emailpassword
2+
3+
import (
4+
"io/ioutil"
5+
"net/http"
6+
"net/http/httptest"
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
"github.com/supertokens/supertokens-golang/supertokens"
11+
)
12+
13+
func TestAPIWithSupertokensMiddlewareButNotInitialized(t *testing.T) {
14+
BeforeEach()
15+
defer AfterEach()
16+
17+
mux := http.NewServeMux()
18+
testServer := httptest.NewServer(supertokens.Middleware(mux))
19+
defer testServer.Close()
20+
21+
resp, err := http.Post(testServer.URL+"/auth/signup", "application/json", nil)
22+
if err != nil {
23+
t.Error(err.Error())
24+
}
25+
26+
assert.Equal(t, 500, resp.StatusCode)
27+
defer resp.Body.Close()
28+
bodyBytes, err := ioutil.ReadAll(resp.Body)
29+
assert.NoError(t, err)
30+
31+
bodyStr := string(bodyBytes)
32+
assert.Equal(t, "initialisation not done. Did you forget to call the SuperTokens.init function?\n", bodyStr)
33+
}

supertokens/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ func Init(config TypeInput) error {
3434
func Middleware(theirHandler http.Handler) http.Handler {
3535
instance, err := GetInstanceOrThrowError()
3636
if err != nil {
37-
panic("Please call supertokens.Init function before using the Middleware")
37+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
38+
http.Error(w, err.Error(), http.StatusInternalServerError)
39+
})
3840
}
3941
return instance.middleware(theirHandler)
4042
}

0 commit comments

Comments
 (0)