[core] Improve GetCurrentStreamInfo lib function

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2023-07-30 14:03:55 +02:00
parent 17ad71f233
commit c263c72f9b
Signed by: luzifer
GPG key ID: D91C3E91E4CAD6F5

View file

@ -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) { func (c *Client) GetCurrentStreamInfo(username string) (*StreamInfo, error) {
cacheKey := []string{"currentStreamInfo", username} cacheKey := []string{"currentStreamInfo", username}
if si := c.apiCache.Get(cacheKey); si != nil { 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") return nil, errors.Wrap(err, "request channel info")
} }
if l := len(payload.Data); l != 1 { switch l := len(payload.Data); l {
return nil, errors.Errorf("unexpected number of users returned: %d", 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 // Stream-info can be changed at any moment, cache for a short period of time