Skip to content

Commit

Permalink
feat(updates): handle auth errors
Browse files Browse the repository at this point in the history
Ref: #1458
  • Loading branch information
ernado committed Nov 29, 2024
1 parent a21b7e1 commit aaba7cb
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions telegram/updates/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ import (
"go.uber.org/zap"
"golang.org/x/sync/errgroup"

"github.com/gotd/td/exchange"
"github.com/gotd/td/telegram"
"github.com/gotd/td/telegram/auth"
"github.com/gotd/td/tg"
"github.com/gotd/td/tgerr"
)

const (
Expand Down Expand Up @@ -137,6 +140,21 @@ func (s *internalState) Push(ctx context.Context, u tg.UpdatesClass) error {
}
}

// isFatalError returns true if error is fatal so we should stop updates handler.
func isFatalError(err error) bool {
// See https://github.com/gotd/td/issues/1458.
if errors.Is(err, exchange.ErrKeyFingerprintNotFound) {
return true
}
if tgerr.Is(err, "AUTH_KEY_UNREGISTERED", "SESSION_EXPIRED") {
return true
}
if auth.IsUnauthorized(err) {
return true
}
return false
}

func (s *internalState) Run(ctx context.Context) error {
if s == nil {
return errors.New("invalid: nil internalState")
Expand All @@ -159,11 +177,17 @@ func (s *internalState) Run(ctx context.Context) error {
ctx := trace.ContextWithSpanContext(ctx, u.span)
if err := s.handleUpdates(ctx, u.update); err != nil {
s.log.Error("Handle updates error", zap.Error(err))
if isFatalError(err) {
return errors.Wrap(err, "fatal error")
}
}
case u := <-s.internalQueue:
ctx := trace.ContextWithSpanContext(ctx, u.span)
if err := s.handleUpdates(ctx, u.update); err != nil {
s.log.Error("Handle updates error", zap.Error(err))
if isFatalError(err) {
return errors.Wrap(err, "fatal error")
}
}
case <-s.pts.gapTimeout.C:
s.log.Debug("Pts gap timeout")
Expand Down

0 comments on commit aaba7cb

Please sign in to comment.