diff --git a/middleware/middleware.go b/middleware/middleware.go index 91babd5..e616c1e 100644 --- a/middleware/middleware.go +++ b/middleware/middleware.go @@ -32,26 +32,28 @@ func IgnoreVia() tele.MiddlewareFunc { } } +type RecoverFunc = func(error, tele.Context) + // Recover returns a middleware that recovers a panic happened in // the handler. -func Recover(onError ...func(error)) tele.MiddlewareFunc { +func Recover(onError ...RecoverFunc) tele.MiddlewareFunc { return func(next tele.HandlerFunc) tele.HandlerFunc { return func(c tele.Context) error { - var f func(error) + var f RecoverFunc if len(onError) > 0 { f = onError[0] } else { - f = func(err error) { - c.Bot().OnError(err, nil) + f = func(err error, c tele.Context) { + c.Bot().OnError(err, c) } } defer func() { if r := recover(); r != nil { if err, ok := r.(error); ok { - f(err) + f(err, c) } else if s, ok := r.(string); ok { - f(errors.New(s)) + f(errors.New(s), c) } } }()