Skip to content
This repository has been archived by the owner on Apr 11, 2020. It is now read-only.

Commit

Permalink
refactor: update module
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Aug 15, 2019
1 parent 6da20a8 commit a07c078
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 31 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -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")
})

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
)
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
18 changes: 9 additions & 9 deletions recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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, "")
}
Expand Down
20 changes: 10 additions & 10 deletions recover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit a07c078

Please sign in to comment.