Skip to content

Commit 8a1c9df

Browse files
committed
fix silent error handling of all parsers failed to stream
1 parent 06b7bba commit 8a1c9df

4 files changed

Lines changed: 47 additions & 5 deletions

File tree

internal/command/music/common/status_listener.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ func ListenPlayerStatusSlash(session *discordgo.Session, event *discordgo.Intera
8888
appLog.Warn().Str("status", "added").Str("guild_id", guildID).Err(err).Msg("guild_status_update_failed")
8989
}
9090
return
91+
92+
case player.StatusError:
93+
_ = discordreply.FollowupEmbedEphemeral(session, event, &discordgo.MessageEmbed{
94+
Title: statusEmoji(player.StatusError) + " Playback Error",
95+
Description: "Could not start or continue playback (for example all stream parsers failed).",
96+
Color: discordreply.EmbedColor,
97+
})
98+
return
9199
}
92100
}
93101
}

internal/command/music/next/next.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package next
22

33
import (
4+
"errors"
45
"fmt"
56

67
"github.com/bwmarrin/discordgo"
@@ -9,6 +10,7 @@ import (
910
"github.com/keshon/melodix/internal/discord"
1011
"github.com/keshon/melodix/internal/discord/discordreply"
1112
"github.com/keshon/melodix/internal/discord/perm"
13+
musicplayer "github.com/keshon/melodix/pkg/music/player"
1214
)
1315

1416
type Next struct {
@@ -76,6 +78,10 @@ func (c *Next) Run(ctx interface{}) error {
7678

7779
player.Stop(false)
7880
if err = player.PlayNext(voiceState.ChannelID); err != nil {
81+
if errors.Is(err, musicplayer.ErrTrackStartFailed) {
82+
common.ListenPlayerStatusSlash(s, e, player, c.Bot, guildID, slashCtx.AppLog)
83+
return nil
84+
}
7985
discordreply.FollowupEmbedEphemeral(s, e, &discordgo.MessageEmbed{
8086
Title: "🎵 Playback Error",
8187
Description: fmt.Sprintf("Failed to play next track.\n\n**Error:** %v", err),
@@ -86,4 +92,3 @@ func (c *Next) Run(ctx interface{}) error {
8692
common.ListenPlayerStatusSlash(s, e, player, c.Bot, guildID, slashCtx.AppLog)
8793
return nil
8894
}
89-

internal/command/music/play/play.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/keshon/melodix/internal/discord/discordreply"
1212
"github.com/keshon/melodix/internal/discord/perm"
1313
"github.com/keshon/melodix/internal/storage"
14+
"github.com/keshon/melodix/pkg/music/player"
1415
)
1516

1617
type Play struct {
@@ -212,10 +213,28 @@ func (c *Play) Run(ctx interface{}) error {
212213
}
213214

214215
if !p.IsPlaying() {
215-
_ = p.PlayNext(voiceState.ChannelID)
216+
if err := p.PlayNext(voiceState.ChannelID); err != nil {
217+
if errors.Is(err, player.ErrTrackStartFailed) {
218+
common.ListenPlayerStatusSlash(s, e, p, c.Bot, guildID, slashCtx.AppLog)
219+
return nil
220+
}
221+
if errors.Is(err, player.ErrNoTracksInQueue) {
222+
discordreply.FollowupEmbedEphemeral(s, e, &discordgo.MessageEmbed{
223+
Title: "🎵 Queue",
224+
Description: "Nothing is in the queue to play.",
225+
Color: discordreply.EmbedColor,
226+
})
227+
return nil
228+
}
229+
discordreply.FollowupEmbedEphemeral(s, e, &discordgo.MessageEmbed{
230+
Title: "🎵 Playback Error",
231+
Description: fmt.Sprintf("%v", err),
232+
Color: discordreply.EmbedColor,
233+
})
234+
return nil
235+
}
216236
}
217237

218238
common.ListenPlayerStatusSlash(s, e, p, c.Bot, guildID, slashCtx.AppLog)
219239
return nil
220240
}
221-

pkg/music/player/player.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ const (
2727
)
2828

2929
var (
30-
ErrNoTrackPlaying = errors.New("no track is currently playing")
31-
ErrNoTracksInQueue = errors.New("no tracks in queue")
30+
ErrNoTrackPlaying = errors.New("no track is currently playing")
31+
ErrNoTracksInQueue = errors.New("no tracks in queue")
32+
// ErrTrackStartFailed is returned when a dequeued track could not start playback (e.g. all parsers failed)
33+
// and there are no further tracks in the queue.
34+
ErrTrackStartFailed = errors.New("track failed to start")
3235
ErrNoParsersForTrack = errors.New("track has no available parsers")
3336
ErrPauseNotSupported = errors.New("pause is not supported")
3437
ErrResumeNotSupported = errors.New("resume is not supported")
@@ -241,6 +244,13 @@ func (p *Player) PlayNext(target string) error {
241244

242245
if err != nil {
243246
p.log.Warn().Str("title", track.Title).Err(err).Msg("track_skipped_error")
247+
p.mu.Lock()
248+
qEmpty := len(p.queue) == 0
249+
p.mu.Unlock()
250+
if qEmpty {
251+
p.emitStatus(StatusError)
252+
return fmt.Errorf("%w: %v", ErrTrackStartFailed, err)
253+
}
244254
continue
245255
}
246256

0 commit comments

Comments
 (0)