diff --git a/README.md b/README.md index e0eede4..5a9c889 100644 --- a/README.md +++ b/README.md @@ -1,25 +1,25 @@ -# cod-recover +# elton-recover -[![Build Status](https://img.shields.io/travis/vicanso/cod-recover.svg?label=linux+build)](https://travis-ci.org/vicanso/cod-recover) +[![Build Status](https://img.shields.io/travis/vicanso/elton-recover.svg?label=linux+build)](https://travis-ci.org/vicanso/elton-recover) -Recover middleware for cod, it can get panic error to avoid application crash. +Recover middleware for elton, it can get panic error to avoid application crash. ```go package main import ( - "github.com/vicanso/cod" + "github.com/vicanso/elton" - recover "github.com/vicanso/cod-recover" + recover "github.com/vicanso/elton-recover" ) func main() { - d := cod.New() + d := elton.New() d.Use(recover.New()) - d.GET("/", func(c *cod.Context) (err error) { + d.GET("/", func(c *elton.Context) (err error) { panic("abcd") }) diff --git a/go.mod b/go.mod index e1c5110..9f87297 100644 --- a/go.mod +++ b/go.mod @@ -1,9 +1,9 @@ -module github.com/vicanso/cod-recover +module github.com/vicanso/elton-recover go 1.12 require ( github.com/stretchr/testify v1.3.0 - github.com/vicanso/cod v0.1.5 + github.com/vicanso/elton v0.2.0 github.com/vicanso/hes v0.2.1 ) diff --git a/go.sum b/go.sum index e250e52..c2e96a9 100644 --- a/go.sum +++ b/go.sum @@ -7,9 +7,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/vicanso/cod v0.1.5 h1:KqdTAo1TzUa+iYbdzg1INEAZCG4pvxHkwmwKac5RLz8= -github.com/vicanso/cod v0.1.5/go.mod h1:T5GOazXuYrwwE1qMA0G3zka7NVwwkoL2fYIWNfeEpJw= -github.com/vicanso/hes v0.1.4/go.mod h1:bG0UJ3EihDKObFcLLwJYjxHHr9saFllsFcepyDIvFlo= +github.com/vicanso/elton v0.2.0 h1:QlXgmq6m+9wZN7FeLD25/EhBkl8blzppviVY5U5PTm0= +github.com/vicanso/elton v0.2.0/go.mod h1:ynAUOSkZQ+pFaUsxlG5hYnJFjPpMwz8YyEBPzNh0pSg= 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/keygrip v0.1.0 h1:/zYzoVIbREAvaxSM7bo3/oSXuuYztaP71dPBfhRoNkM= diff --git a/recover.go b/recover.go index c4d1255..47e42c4 100644 --- a/recover.go +++ b/recover.go @@ -20,17 +20,17 @@ import ( "github.com/vicanso/hes" - "github.com/vicanso/cod" + "github.com/vicanso/elton" ) const ( // ErrCategory recover error category - ErrCategory = "cod-recover" + ErrCategory = "elton-recover" ) // New new recover -func New() cod.Handler { - return func(c *cod.Context) error { +func New() elton.Handler { + return func(c *elton.Context) error { defer func() { // 可针对实际需求调整,如对于每个recover增加邮件通知等 if r := recover(); r != nil { @@ -40,13 +40,13 @@ func New() cod.Handler { he.Category = ErrCategory err = he } - c.Cod().EmitError(c, err) + c.Elton().EmitError(c, err) // 出错时清除部分响应头 for _, key := range []string{ - cod.HeaderETag, - cod.HeaderLastModified, - cod.HeaderContentEncoding, - cod.HeaderContentLength, + elton.HeaderETag, + elton.HeaderLastModified, + elton.HeaderContentEncoding, + elton.HeaderContentLength, } { c.SetHeader(key, "") } diff --git a/recover_test.go b/recover_test.go index 3605ca6..1c87498 100644 --- a/recover_test.go +++ b/recover_test.go @@ -8,38 +8,38 @@ import ( "testing" "github.com/stretchr/testify/assert" - "github.com/vicanso/cod" + "github.com/vicanso/elton" ) func TestRecover(t *testing.T) { assert := assert.New(t) - var ctx *cod.Context - d := cod.New() + var ctx *elton.Context + d := elton.New() d.Use(New()) - d.GET("/", func(c *cod.Context) error { + d.GET("/", func(c *elton.Context) error { ctx = c panic("abc") }) req := httptest.NewRequest("GET", "https://aslant.site/", nil) resp := httptest.NewRecorder() keys := []string{ - cod.HeaderETag, - cod.HeaderLastModified, - cod.HeaderContentEncoding, - cod.HeaderContentLength, + elton.HeaderETag, + elton.HeaderLastModified, + elton.HeaderContentEncoding, + elton.HeaderContentLength, } for _, key := range keys { resp.Header().Set(key, "a") } catchError := false - d.OnError(func(_ *cod.Context, _ error) { + d.OnError(func(_ *elton.Context, _ error) { catchError = true }) d.ServeHTTP(resp, req) assert.Equal(resp.Code, http.StatusInternalServerError) - assert.Equal(resp.Body.String(), "category=cod-recover, message=abc") + assert.Equal(resp.Body.String(), "category=elton-recover, message=abc") assert.True(ctx.Committed) assert.True(catchError) for _, key := range keys {