Skip to content

Commit

Permalink
Add errorHandler to MTProto and ClientConfig for improved error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
AmarnathCJD committed Dec 18, 2024
1 parent 4fa3649 commit b018076
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
16 changes: 13 additions & 3 deletions mtproto.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ type MTProto struct {

serverRequestHandlers []func(i any) bool
floodHandler func(err error) bool
errorHandler func(err error)
exported bool
}

Expand All @@ -89,7 +90,9 @@ type Config struct {
SessionStorage session.SessionLoader
MemorySession bool
AppID int32
FloodHandler func(err error) bool

FloodHandler func(err error) bool
ErrorHandler func(err error)

ServerHost string
PublicKey *rsa.PublicKey
Expand Down Expand Up @@ -142,6 +145,7 @@ func NewMTProto(c Config) (*MTProto, error) {
appID: c.AppID,
proxy: c.Proxy,
floodHandler: func(err error) bool { return false },
errorHandler: func(err error) {},
mode: parseTransportMode(c.Mode),
IpV6: c.Ipv6,
}
Expand All @@ -164,6 +168,10 @@ func NewMTProto(c Config) (*MTProto, error) {
mtproto.floodHandler = c.FloodHandler
}

if c.ErrorHandler != nil {
mtproto.errorHandler = c.ErrorHandler
}

return mtproto, nil
}

Expand Down Expand Up @@ -409,8 +417,8 @@ func (m *MTProto) makeRequest(data tl.Object, expectedTypes ...reflect.Type) (an

resp, _, err := m.sendPacket(data, expectedTypes...)
if err != nil {
if strings.Contains(err.Error(), "use of closed network connection") || strings.Contains(err.Error(), "transport is closed") || strings.Contains(err.Error(), "connection was forcibly closed") {
m.Logger.Info("connection closed due to broken tcp, reconnecting to [" + m.Addr + "]" + " - <Tcp> ...")
if strings.Contains(err.Error(), "use of closed network connection") || strings.Contains(err.Error(), "transport is closed") || strings.Contains(err.Error(), "connection was forcibly closed") || strings.Contains(err.Error(), "connection reset by peer") || strings.Contains(err.Error(), "broken pipe") {
m.Logger.Info(fmt.Sprintf("connection closed due to broken tcp, reconnecting to [%s] - <%s> ...", m.Addr, utils.Vtcp(m.IpV6)))
err = m.Reconnect(false)
if err != nil {
return nil, errors.Wrap(err, "reconnecting")
Expand All @@ -429,6 +437,8 @@ func (m *MTProto) makeRequest(data tl.Object, expectedTypes ...reflect.Type) (an
return m.makeRequest(data, expectedTypes...)
}
}
m.errorHandler(RpcErrorToNative(r))

return nil, RpcErrorToNative(r)

case *errorSessionConfigsChanged:
Expand Down
3 changes: 3 additions & 0 deletions telegram/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ type ClientConfig struct {
Cache *CACHE
TransportMode string
FloodHandler func(err error) bool
ErrorHandler func(err error)
}

type Session struct {
Expand Down Expand Up @@ -172,6 +173,8 @@ func (c *Client) setupMTProto(config ClientConfig) error {
MemorySession: config.MemorySession,
Ipv6: config.ForceIPv6,
CustomHost: customHost,
FloodHandler: config.FloodHandler,
ErrorHandler: config.ErrorHandler,
})
if err != nil {
return errors.Wrap(err, "creating mtproto client")
Expand Down

0 comments on commit b018076

Please sign in to comment.