-
Notifications
You must be signed in to change notification settings - Fork 476
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
middleware: implement Recover, add docs
- Loading branch information
Showing
8 changed files
with
136 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package middleware | ||
|
||
import ( | ||
"errors" | ||
|
||
tele "gopkg.in/telebot.v3" | ||
) | ||
|
||
// AutoRespond returns a middleware that automatically responds | ||
// to every callback. | ||
func AutoRespond() tele.MiddlewareFunc { | ||
return func(next tele.HandlerFunc) tele.HandlerFunc { | ||
return func(c tele.Context) error { | ||
if c.Callback() != nil { | ||
defer c.Respond() | ||
} | ||
return next(c) | ||
} | ||
} | ||
} | ||
|
||
// IgnoreVia returns a middleware that ignores all the | ||
// "sent via" messages. | ||
func IgnoreVia() tele.MiddlewareFunc { | ||
return func(next tele.HandlerFunc) tele.HandlerFunc { | ||
return func(c tele.Context) error { | ||
if msg := c.Message(); msg != nil && msg.Via != nil { | ||
return nil | ||
} | ||
return next(c) | ||
} | ||
} | ||
} | ||
|
||
// Recover returns a middleware that recovers a panic happened in | ||
// the handler. | ||
func Recover(onError ...func(error)) tele.MiddlewareFunc { | ||
return func(next tele.HandlerFunc) tele.HandlerFunc { | ||
return func(c tele.Context) error { | ||
var f func(error) | ||
if len(onError) > 0 { | ||
f = onError[0] | ||
} else { | ||
f = func(err error) { | ||
c.Bot().OnError(err, nil) | ||
} | ||
} | ||
|
||
defer func() { | ||
if r := recover(); r != nil { | ||
if err, ok := r.(error); ok { | ||
f(err) | ||
} else if s, ok := r.(string); ok { | ||
f(errors.New(s)) | ||
} | ||
} | ||
}() | ||
|
||
return next(c) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package middleware | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
tele "gopkg.in/telebot.v3" | ||
) | ||
|
||
var b, _ = tele.NewBot(tele.Settings{Offline: true}) | ||
|
||
func TestRecover(t *testing.T) { | ||
onError := func(err error) { | ||
require.Error(t, err, "recover test") | ||
} | ||
|
||
h := func(c tele.Context) error { | ||
panic("recover test") | ||
} | ||
|
||
assert.Panics(t, func() { | ||
h(nil) | ||
}) | ||
|
||
assert.NotPanics(t, func() { | ||
Recover(onError)(h)(nil) | ||
}) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters