Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(telegram): add OnSelfError #1478

Merged
merged 1 commit into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading