From c263c72f9b6915d28270bf6baa1f908e9a55571b Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Sun, 30 Jul 2023 14:03:55 +0200 Subject: [PATCH] [core] Improve GetCurrentStreamInfo lib function Signed-off-by: Knut Ahlers --- pkg/twitch/streams.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/pkg/twitch/streams.go b/pkg/twitch/streams.go index eef0360..0ef751d 100644 --- a/pkg/twitch/streams.go +++ b/pkg/twitch/streams.go @@ -28,6 +28,10 @@ type ( } ) +// ErrNoStreamsFound allows to differntiate between an HTTP error and +// the fact there just is no stream found +var ErrNoStreamsFound = errors.New("no streams found") + func (c *Client) GetCurrentStreamInfo(username string) (*StreamInfo, error) { cacheKey := []string{"currentStreamInfo", username} if si := c.apiCache.Get(cacheKey); si != nil { @@ -54,8 +58,15 @@ func (c *Client) GetCurrentStreamInfo(username string) (*StreamInfo, error) { return nil, errors.Wrap(err, "request channel info") } - if l := len(payload.Data); l != 1 { - return nil, errors.Errorf("unexpected number of users returned: %d", l) + switch l := len(payload.Data); l { + case 0: + return nil, ErrNoStreamsFound + + case 1: + // That's expected + + default: + return nil, errors.Errorf("unexpected number of streams returned: %d", l) } // Stream-info can be changed at any moment, cache for a short period of time