From 75007767474334e054f67a515c23563d71412fe5 Mon Sep 17 00:00:00 2001 From: Jorge Rasillo Date: Fri, 8 Nov 2024 08:50:26 -0600 Subject: [PATCH] add error handling --- bot/cmd/main.go | 8 +++++++- bot/pkg/mastodon/mastodon.go | 19 +++++++++++++------ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/bot/cmd/main.go b/bot/cmd/main.go index 9c3550c..e86502e 100644 --- a/bot/cmd/main.go +++ b/bot/cmd/main.go @@ -63,7 +63,13 @@ func main() { cancel() }() - go mastodonClient.Run(ctx, cancel, errs) + go func(){ + err := mastodonClient.Run(ctx, cancel, errs) + if err != nil { + errs <- err + } + }() + go mastodonClient.Write(ctx) go bskyClient.Run(errs) diff --git a/bot/pkg/mastodon/mastodon.go b/bot/pkg/mastodon/mastodon.go index 090b7b1..4fdc9a8 100644 --- a/bot/pkg/mastodon/mastodon.go +++ b/bot/pkg/mastodon/mastodon.go @@ -71,17 +71,21 @@ func NewClient(logger *slog.Logger, ch chan interface{}, gsheetsClient *gsheets. }, nil } -func (c *Client) Run(ctx context.Context, cancel context.CancelFunc, errs chan error) { +func (c *Client) Run(ctx context.Context, cancel context.CancelFunc, errs chan error) error { streamCh := make(chan mastodon.Event) // stream from public and then iterate to the known supported tags // to use the hashtag api events, err := c.mastodonClient.StreamingPublic(ctx, false) - sendToStream(streamCh, errs, events, err) - for _, tag := range post.GetHashtagsFromTypes() { - ch, err := c.mastodonClient.StreamingHashtag(ctx, tag, false) - sendToStream(streamCh, errs, ch, err) + if err != nil { + c.logger.Error("unable to send a request to mastodon API", "err", err) + return err } + sendToStream(streamCh, errs, events, err) + // for _, tag := range post.GetHashtagsFromTypes() { + // ch, err := c.mastodonClient.StreamingHashtag(ctx, tag, false) + // sendToStream(streamCh, errs, ch, err) + // } for { select { @@ -118,12 +122,15 @@ func (c *Client) Run(ctx context.Context, cancel context.CancelFunc, errs chan e c.logger.Error("Unable to parse post", "err", err) } + case *mastodon.ErrorEvent: + c.logger.Error("Unable to handle event", "err", e.Error()) default: // How should we handle this? + c.logger.Error("Unable to handle event", "err", err) } case <-ctx.Done(): c.logger.Info("Context cancelled, shutting down Mastodon client...") - return + return nil } } }