context: pass context to the OnError middleware

pull/341/head
Demian 4 years ago
parent f1015d1754
commit 4062f24361

@ -65,11 +65,9 @@ type Bot struct {
URL string
Updates chan Update
Poller Poller
OnError func(error)
handlers map[string]HandlerFunc
middleware map[string][]MiddlewareFunc
OnError func(error, Context)
handlers map[string]HandlerFunc
synchronous bool
verbose bool
parseMode ParseMode
@ -102,10 +100,10 @@ type Settings struct {
// will be able to override the default mode by passing a new one.
ParseMode ParseMode
// OnError is a callback function that will get called
// on errors resulted from the handler. It is used as
// post-middleware function.
OnError func(error)
// OnError is a callback function that will get called on errors
// resulted from the handler. It is used as post-middleware function.
// Notice that context can be nil.
OnError func(error, Context)
// HTTP Client used to make requests to telegram api
Client *http.Client

@ -348,14 +348,15 @@ func TestBotOnError(t *testing.T) {
}
var ok bool
b.OnError = func(err error) {
b.OnError = func(err error, c Context) {
assert.Equal(t, b, c.(*nativeContext).b)
assert.NotNil(t, err)
ok = true
}
b.runHandler(func(c Context) error {
return errors.New("not nil")
}, &nativeContext{})
}, &nativeContext{b: b})
assert.True(t, ok)
}

@ -11,7 +11,7 @@ import (
func (b *Bot) debug(err error) {
err = errors.WithStack(err)
if b.OnError != nil {
b.OnError(err)
b.OnError(err, nil)
} else {
log.Printf("%+v\n", err)
}
@ -30,8 +30,10 @@ func (b *Bot) deferDebug() {
func (b *Bot) runHandler(h HandlerFunc, c Context) {
f := func() {
defer b.deferDebug()
if err := h(c); err != nil && err != ErrSkip {
b.OnError(err)
if err := h(c); err != nil {
if err != ErrSkip {
b.OnError(err, c)
}
}
}
if b.synchronous {

Loading…
Cancel
Save