diff --git a/README.md b/README.md index 6f02361..df89b7f 100644 --- a/README.md +++ b/README.md @@ -9,21 +9,25 @@ Recover middleware for elton, it can get panic error to avoid application crash. package main import ( + "errors" + "github.com/vicanso/elton" recover "github.com/vicanso/elton-recover" ) func main() { - d := elton.New() + e := elton.New() - d.Use(recover.New()) + e.Use(recover.New()) - d.GET("/", func(c *elton.Context) (err error) { - panic("abcd") + e.GET("/", func(c *elton.Context) (err error) { + panic(errors.New("abcd")) }) - d.ListenAndServe(":3000") + err := e.ListenAndServe(":3000") + if err != nil { + panic(err) + } } - ``` \ No newline at end of file diff --git a/example/main.go b/example/main.go index 0359d52..299450b 100644 --- a/example/main.go +++ b/example/main.go @@ -1,21 +1,23 @@ package main import ( + "errors" + "github.com/vicanso/elton" recover "github.com/vicanso/elton-recover" ) func main() { - d := elton.New() + e := elton.New() - d.Use(recover.New()) + e.Use(recover.New()) - d.GET("/", func(c *elton.Context) (err error) { - panic("abcd") + e.GET("/", func(c *elton.Context) (err error) { + panic(errors.New("abcd")) }) - err := d.ListenAndServe(":3000") + err := e.ListenAndServe(":3000") if err != nil { panic(err) } diff --git a/go.mod b/go.mod index 8bb4cfd..e7887a5 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,6 @@ go 1.12 require ( github.com/stretchr/testify v1.4.0 - github.com/vicanso/elton v0.2.2 + github.com/vicanso/elton v0.2.3 github.com/vicanso/hes v0.2.1 ) diff --git a/go.sum b/go.sum index 5cd9e0b..be84897 100644 --- a/go.sum +++ b/go.sum @@ -8,8 +8,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/vicanso/elton v0.2.2 h1:MZ5nfJFKBWDWnFPO8wRyPat8kZz3KoNBY0scemo7RFQ= -github.com/vicanso/elton v0.2.2/go.mod h1:QFZ+Un4LLBANtl0mExkqLD4uqw3JLA2ZCWUHaCsHOUg= +github.com/vicanso/elton v0.2.3 h1:XQskGFtw/hhtNXRU7dLX0OFcpG64pK4PMXh9CVjHVbA= +github.com/vicanso/elton v0.2.3/go.mod h1:QFZ+Un4LLBANtl0mExkqLD4uqw3JLA2ZCWUHaCsHOUg= github.com/vicanso/hes v0.2.1 h1:jRFEADmiQ30koVY/sKwlkhyXM5B3QbVVizLqrjNJgPw= github.com/vicanso/hes v0.2.1/go.mod h1:QcxOFmFfBQMhASTaLgnFayXYCgevdSeBVprt+o+3eKo= github.com/vicanso/intranet-ip v0.0.1 h1:cYS+mExFsKqewWSuHtFwAqw/CO66GsheB/P1BPmSTx0= diff --git a/recover_test.go b/recover_test.go index 640e318..bd37fca 100644 --- a/recover_test.go +++ b/recover_test.go @@ -15,9 +15,9 @@ func TestRecover(t *testing.T) { t.Run("response text", func(t *testing.T) { assert := assert.New(t) var ctx *elton.Context - d := elton.New() - d.Use(New()) - d.GET("/", func(c *elton.Context) error { + e := elton.New() + e.Use(New()) + e.GET("/", func(c *elton.Context) error { ctx = c panic("abc") }) @@ -34,11 +34,11 @@ func TestRecover(t *testing.T) { } catchError := false - d.OnError(func(_ *elton.Context, _ error) { + e.OnError(func(_ *elton.Context, _ error) { catchError = true }) - d.ServeHTTP(resp, req) + e.ServeHTTP(resp, req) assert.Equal(resp.Code, http.StatusInternalServerError) assert.Equal(resp.Body.String(), "category=elton-recover, message=abc") assert.True(ctx.Committed) @@ -50,15 +50,15 @@ func TestRecover(t *testing.T) { t.Run("response json", func(t *testing.T) { assert := assert.New(t) - d := elton.New() - d.Use(New()) - d.GET("/", func(c *elton.Context) error { + e := elton.New() + e.Use(New()) + e.GET("/", func(c *elton.Context) error { panic("abc") }) req := httptest.NewRequest("GET", "https://aslant.site/", nil) req.Header.Set("Accept", "application/json, text/plain, */*") resp := httptest.NewRecorder() - d.ServeHTTP(resp, req) + e.ServeHTTP(resp, req) assert.Equal(500, resp.Code) assert.Equal(elton.MIMEApplicationJSON, resp.Header().Get(elton.HeaderContentType)) assert.NotEmpty(resp.Body.Bytes())