Skip to content

Commit

Permalink
Merge pull request #1478 from gotd/feat/on-self-error
Browse files Browse the repository at this point in the history
feat(telegram): add OnSelfError
  • Loading branch information
ernado authored Nov 29, 2024
2 parents aaba7cb + afb63d6 commit 3c6b9ec
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions telegram/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ type Client struct {

// onTransfer is called in transfer.
onTransfer AuthTransferHandler

// onSelfError is called on error calling Self().
onSelfError func(ctx context.Context, err error) error
}

// NewClient creates new unstarted client.
Expand Down Expand Up @@ -169,6 +172,7 @@ func NewClient(appID int, appHash string, opt Options) *Client {
noUpdatesMode: opt.NoUpdates,
mw: opt.Middlewares,
onTransfer: opt.OnTransfer,
onSelfError: opt.OnSelfError,
}
if opt.TracerProvider != nil {
client.tracer = opt.TracerProvider.Tracer(oteltg.Name)
Expand Down
6 changes: 6 additions & 0 deletions telegram/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ func (c *Client) runUntilRestart(ctx context.Context) error {
if !auth.IsUnauthorized(err) {
c.log.Warn("Got error on self", zap.Error(err))
}
if h := c.onSelfError; h != nil {
// Help with https://github.com/gotd/td/issues/1458.
if err := h(ctx, err); err != nil {
return errors.Wrap(err, "onSelfError")
}
}
return nil
}

Expand Down
8 changes: 8 additions & 0 deletions telegram/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ type Options struct {
// OnTransfer is called during authorization transfer.
// See [AuthTransferHandler] for details.
OnTransfer AuthTransferHandler

// OnSelfError is called when client receives error calling Self() on connect.
// Return error to stop reconnection.
//
// NB: this method is called immediately after connection, so it's not expected to be
// non-nil error on first connection before auth, so it's safe to return nil until
// first successful auth.
OnSelfError func(ctx context.Context, err error) error
}

func (opt *Options) setDefaults() {
Expand Down

0 comments on commit 3c6b9ec

Please sign in to comment.