PlaylistError: add exception class wrapping enum PlaylistResult

This commit is contained in:
Max Kellermann 2015-12-18 09:21:11 +01:00
parent 7562c5751c
commit 8bb5a565cd
8 changed files with 132 additions and 134 deletions

View File

@ -63,12 +63,12 @@ struct Partition final : private PlayerListener, private MixerListener {
return playlist.AppendURI(pc, loader, uri_utf8, error); return playlist.AppendURI(pc, loader, uri_utf8, error);
} }
PlaylistResult DeletePosition(unsigned position) { void DeletePosition(unsigned position) {
return playlist.DeletePosition(pc, position); playlist.DeletePosition(pc, position);
} }
PlaylistResult DeleteId(unsigned id) { void DeleteId(unsigned id) {
return playlist.DeleteId(pc, id); playlist.DeleteId(pc, id);
} }
/** /**
@ -77,8 +77,8 @@ struct Partition final : private PlayerListener, private MixerListener {
* @param start the position of the first song to delete * @param start the position of the first song to delete
* @param end the position after the last song to delete * @param end the position after the last song to delete
*/ */
PlaylistResult DeleteRange(unsigned start, unsigned end) { void DeleteRange(unsigned start, unsigned end) {
return playlist.DeleteRange(pc, start, end); playlist.DeleteRange(pc, start, end);
} }
#ifdef ENABLE_DATABASE #ifdef ENABLE_DATABASE
@ -93,53 +93,50 @@ struct Partition final : private PlayerListener, private MixerListener {
playlist.Shuffle(pc, start, end); playlist.Shuffle(pc, start, end);
} }
PlaylistResult MoveRange(unsigned start, unsigned end, int to) { void MoveRange(unsigned start, unsigned end, int to) {
return playlist.MoveRange(pc, start, end, to); playlist.MoveRange(pc, start, end, to);
} }
PlaylistResult MoveId(unsigned id, int to) { void MoveId(unsigned id, int to) {
return playlist.MoveId(pc, id, to); playlist.MoveId(pc, id, to);
} }
PlaylistResult SwapPositions(unsigned song1, unsigned song2) { void SwapPositions(unsigned song1, unsigned song2) {
return playlist.SwapPositions(pc, song1, song2); playlist.SwapPositions(pc, song1, song2);
} }
PlaylistResult SwapIds(unsigned id1, unsigned id2) { void SwapIds(unsigned id1, unsigned id2) {
return playlist.SwapIds(pc, id1, id2); playlist.SwapIds(pc, id1, id2);
} }
PlaylistResult SetPriorityRange(unsigned start_position, void SetPriorityRange(unsigned start_position, unsigned end_position,
unsigned end_position, uint8_t priority) {
uint8_t priority) { playlist.SetPriorityRange(pc, start_position, end_position,
return playlist.SetPriorityRange(pc, priority);
start_position, end_position,
priority);
} }
PlaylistResult SetPriorityId(unsigned song_id, void SetPriorityId(unsigned song_id, uint8_t priority) {
uint8_t priority) { playlist.SetPriorityId(pc, song_id, priority);
return playlist.SetPriorityId(pc, song_id, priority);
} }
void Stop() { void Stop() {
playlist.Stop(pc); playlist.Stop(pc);
} }
PlaylistResult PlayPosition(int position) { void PlayPosition(int position) {
return playlist.PlayPosition(pc, position); playlist.PlayPosition(pc, position);
} }
PlaylistResult PlayId(int id) { void PlayId(int id) {
return playlist.PlayId(pc, id); playlist.PlayId(pc, id);
} }
void PlayNext() { void PlayNext() {
return playlist.PlayNext(pc); playlist.PlayNext(pc);
} }
void PlayPrevious() { void PlayPrevious() {
return playlist.PlayPrevious(pc); playlist.PlayPrevious(pc);
} }
bool SeekSongPosition(unsigned song_position, bool SeekSongPosition(unsigned song_position,

View File

@ -20,6 +20,8 @@
#ifndef MPD_PLAYLIST_ERROR_HXX #ifndef MPD_PLAYLIST_ERROR_HXX
#define MPD_PLAYLIST_ERROR_HXX #define MPD_PLAYLIST_ERROR_HXX
#include <stdexcept>
class Domain; class Domain;
enum class PlaylistResult { enum class PlaylistResult {
@ -37,4 +39,26 @@ enum class PlaylistResult {
extern const Domain playlist_domain; extern const Domain playlist_domain;
class PlaylistError : public std::runtime_error {
PlaylistResult code;
public:
PlaylistError(PlaylistResult _code, const char *msg)
:std::runtime_error(msg), code(_code) {}
PlaylistResult GetCode() const {
return code;
}
static PlaylistError NoSuchSong() {
return PlaylistError(PlaylistResult::BAD_RANGE,
"No such song");
}
static PlaylistError BadRange() {
return PlaylistError(PlaylistResult::BAD_RANGE,
"Bad song index");
}
};
#endif #endif

View File

@ -165,6 +165,8 @@ PrintError(Response &r, const std::exception &e)
try { try {
throw e; throw e;
} catch (const PlaylistError &pe) {
r.Error(ToAck(pe.GetCode()), pe.what());
} catch (const std::system_error &) { } catch (const std::system_error &) {
r.Error(ACK_ERROR_SYSTEM, e.what()); r.Error(ACK_ERROR_SYSTEM, e.what());
} catch (...) { } catch (...) {

View File

@ -63,8 +63,8 @@ handle_play(Client &client, Request args, Response &r)
if (!args.ParseOptional(0, song, r)) if (!args.ParseOptional(0, song, r))
return CommandResult::ERROR; return CommandResult::ERROR;
PlaylistResult result = client.partition.PlayPosition(song); client.partition.PlayPosition(song);
return print_playlist_result(r, result); return CommandResult::OK;
} }
CommandResult CommandResult
@ -74,8 +74,8 @@ handle_playid(Client &client, Request args, Response &r)
if (!args.ParseOptional(0, id, r)) if (!args.ParseOptional(0, id, r))
return CommandResult::ERROR; return CommandResult::ERROR;
PlaylistResult result = client.partition.PlayId(id); client.partition.PlayId(id);
return print_playlist_result(r, result); return CommandResult::OK;
} }
CommandResult CommandResult

View File

@ -132,12 +132,13 @@ handle_addid(Client &client, Request args, Response &r)
if (!args.Parse(1, to, r)) if (!args.Parse(1, to, r))
return CommandResult::ERROR; return CommandResult::ERROR;
PlaylistResult result = client.partition.MoveId(added_id, to); try {
if (result != PlaylistResult::SUCCESS) { client.partition.MoveId(added_id, to);
CommandResult ret = return CommandResult::OK;
print_playlist_result(r, result); } catch (...) {
/* rollback */
client.partition.DeleteId(added_id); client.partition.DeleteId(added_id);
return ret; throw;
} }
} }
@ -205,8 +206,8 @@ handle_delete(Client &client, Request args, Response &r)
if (!args.Parse(0, range, r)) if (!args.Parse(0, range, r))
return CommandResult::ERROR; return CommandResult::ERROR;
auto result = client.partition.DeleteRange(range.start, range.end); client.partition.DeleteRange(range.start, range.end);
return print_playlist_result(r, result); return CommandResult::OK;
} }
CommandResult CommandResult
@ -216,8 +217,8 @@ handle_deleteid(Client &client, Request args, Response &r)
if (!args.Parse(0, id, r)) if (!args.Parse(0, id, r))
return CommandResult::ERROR; return CommandResult::ERROR;
PlaylistResult result = client.partition.DeleteId(id); client.partition.DeleteId(id);
return print_playlist_result(r, result); return CommandResult::OK;
} }
CommandResult CommandResult
@ -351,12 +352,8 @@ handle_prio(Client &client, Request args, Response &r)
if (!ParseCommandArg(r, range, i)) if (!ParseCommandArg(r, range, i))
return CommandResult::ERROR; return CommandResult::ERROR;
PlaylistResult result = client.partition.SetPriorityRange(range.start, range.end,
client.partition.SetPriorityRange(range.start, priority);
range.end,
priority);
if (result != PlaylistResult::SUCCESS)
return print_playlist_result(r, result);
} }
return CommandResult::OK; return CommandResult::OK;
@ -374,10 +371,7 @@ handle_prioid(Client &client, Request args, Response &r)
if (!ParseCommandArg(r, song_id, i)) if (!ParseCommandArg(r, song_id, i))
return CommandResult::ERROR; return CommandResult::ERROR;
PlaylistResult result = client.partition.SetPriorityId(song_id, priority);
client.partition.SetPriorityId(song_id, priority);
if (result != PlaylistResult::SUCCESS)
return print_playlist_result(r, result);
} }
return CommandResult::OK; return CommandResult::OK;
@ -392,9 +386,8 @@ handle_move(Client &client, Request args, Response &r)
if (!args.Parse(0, range, r) || !args.Parse(1, to, r)) if (!args.Parse(0, range, r) || !args.Parse(1, to, r))
return CommandResult::ERROR; return CommandResult::ERROR;
PlaylistResult result = client.partition.MoveRange(range.start, range.end, to);
client.partition.MoveRange(range.start, range.end, to); return CommandResult::OK;
return print_playlist_result(r, result);
} }
CommandResult CommandResult
@ -405,8 +398,8 @@ handle_moveid(Client &client, Request args, Response &r)
if (!args.Parse(0, id, r) || !args.Parse(1, to, r)) if (!args.Parse(0, id, r) || !args.Parse(1, to, r))
return CommandResult::ERROR; return CommandResult::ERROR;
PlaylistResult result = client.partition.MoveId(id, to); client.partition.MoveId(id, to);
return print_playlist_result(r, result); return CommandResult::OK;
} }
CommandResult CommandResult
@ -416,9 +409,8 @@ handle_swap(Client &client, Request args, Response &r)
if (!args.Parse(0, song1, r) || !args.Parse(1, song2, r)) if (!args.Parse(0, song1, r) || !args.Parse(1, song2, r))
return CommandResult::ERROR; return CommandResult::ERROR;
PlaylistResult result = client.partition.SwapPositions(song1, song2);
client.partition.SwapPositions(song1, song2); return CommandResult::OK;
return print_playlist_result(r, result);
} }
CommandResult CommandResult
@ -428,6 +420,6 @@ handle_swapid(Client &client, Request args, Response &r)
if (!args.Parse(0, id1, r) || !args.Parse(1, id2, r)) if (!args.Parse(0, id1, r) || !args.Parse(1, id2, r))
return CommandResult::ERROR; return CommandResult::ERROR;
PlaylistResult result = client.partition.SwapIds(id1, id2); client.partition.SwapIds(id1, id2);
return print_playlist_result(r, result); return CommandResult::OK;
} }

View File

@ -214,15 +214,13 @@ protected:
unsigned song, const DetachedSong **queued_p); unsigned song, const DetachedSong **queued_p);
public: public:
PlaylistResult DeletePosition(PlayerControl &pc, void DeletePosition(PlayerControl &pc, unsigned position);
unsigned position);
PlaylistResult DeleteOrder(PlayerControl &pc, void DeleteOrder(PlayerControl &pc, unsigned order) {
unsigned order) { DeletePosition(pc, queue.OrderToPosition(order));
return DeletePosition(pc, queue.OrderToPosition(order));
} }
PlaylistResult DeleteId(PlayerControl &pc, unsigned id); void DeleteId(PlayerControl &pc, unsigned id);
/** /**
* Deletes a range of songs from the playlist. * Deletes a range of songs from the playlist.
@ -230,31 +228,27 @@ public:
* @param start the position of the first song to delete * @param start the position of the first song to delete
* @param end the position after the last song to delete * @param end the position after the last song to delete
*/ */
PlaylistResult DeleteRange(PlayerControl &pc, void DeleteRange(PlayerControl &pc, unsigned start, unsigned end);
unsigned start, unsigned end);
void DeleteSong(PlayerControl &pc, const char *uri); void DeleteSong(PlayerControl &pc, const char *uri);
void Shuffle(PlayerControl &pc, unsigned start, unsigned end); void Shuffle(PlayerControl &pc, unsigned start, unsigned end);
PlaylistResult MoveRange(PlayerControl &pc, void MoveRange(PlayerControl &pc, unsigned start,
unsigned start, unsigned end, int to); unsigned end, int to);
PlaylistResult MoveId(PlayerControl &pc, unsigned id, int to); void MoveId(PlayerControl &pc, unsigned id, int to);
PlaylistResult SwapPositions(PlayerControl &pc, void SwapPositions(PlayerControl &pc, unsigned song1, unsigned song2);
unsigned song1, unsigned song2);
PlaylistResult SwapIds(PlayerControl &pc, void SwapIds(PlayerControl &pc, unsigned id1, unsigned id2);
unsigned id1, unsigned id2);
PlaylistResult SetPriorityRange(PlayerControl &pc, void SetPriorityRange(PlayerControl &pc,
unsigned start_position, unsigned start_position, unsigned end_position,
unsigned end_position, uint8_t priority);
uint8_t priority);
PlaylistResult SetPriorityId(PlayerControl &pc, void SetPriorityId(PlayerControl &pc,
unsigned song_id, uint8_t priority); unsigned song_id, uint8_t priority);
/** /**
* Sets the start_time and end_time attributes on the song * Sets the start_time and end_time attributes on the song
@ -270,11 +264,11 @@ public:
void Stop(PlayerControl &pc); void Stop(PlayerControl &pc);
PlaylistResult PlayPosition(PlayerControl &pc, int position); void PlayPosition(PlayerControl &pc, int position);
void PlayOrder(PlayerControl &pc, int order); void PlayOrder(PlayerControl &pc, int order);
PlaylistResult PlayId(PlayerControl &pc, int id); void PlayId(PlayerControl &pc, int id);
void PlayNext(PlayerControl &pc); void PlayNext(PlayerControl &pc);

View File

@ -56,7 +56,7 @@ playlist::Stop(PlayerControl &pc)
} }
} }
PlaylistResult void
playlist::PlayPosition(PlayerControl &pc, int song) playlist::PlayPosition(PlayerControl &pc, int song)
{ {
pc.LockClearError(); pc.LockClearError();
@ -66,13 +66,13 @@ playlist::PlayPosition(PlayerControl &pc, int song)
/* play any song ("current" song, or the first song */ /* play any song ("current" song, or the first song */
if (queue.IsEmpty()) if (queue.IsEmpty())
return PlaylistResult::SUCCESS; return;
if (playing) { if (playing) {
/* already playing: unpause playback, just in /* already playing: unpause playback, just in
case it was paused, and return */ case it was paused, and return */
pc.LockSetPause(false); pc.LockSetPause(false);
return PlaylistResult::SUCCESS; return;
} }
/* select a song: "current" song, or the first one */ /* select a song: "current" song, or the first one */
@ -80,7 +80,7 @@ playlist::PlayPosition(PlayerControl &pc, int song)
? current ? current
: 0; : 0;
} else if (!queue.IsValidPosition(song)) } else if (!queue.IsValidPosition(song))
return PlaylistResult::BAD_RANGE; throw PlaylistError::BadRange();
if (queue.random) { if (queue.random) {
if (song >= 0) if (song >= 0)
@ -103,18 +103,19 @@ playlist::PlayPosition(PlayerControl &pc, int song)
error_count = 0; error_count = 0;
PlayOrder(pc, i); PlayOrder(pc, i);
return PlaylistResult::SUCCESS;
} }
PlaylistResult void
playlist::PlayId(PlayerControl &pc, int id) playlist::PlayId(PlayerControl &pc, int id)
{ {
if (id == -1) if (id == -1) {
return PlayPosition(pc, id); PlayPosition(pc, id);
return;
}
int song = queue.IdToPosition(id); int song = queue.IdToPosition(id);
if (song < 0) if (song < 0)
return PlaylistResult::NO_SUCH_SONG; throw PlaylistError::NoSuchSong();
return PlayPosition(pc, song); return PlayPosition(pc, song);
} }

View File

@ -138,11 +138,11 @@ playlist::AppendURI(PlayerControl &pc, const SongLoader &loader,
return result; return result;
} }
PlaylistResult void
playlist::SwapPositions(PlayerControl &pc, unsigned song1, unsigned song2) playlist::SwapPositions(PlayerControl &pc, unsigned song1, unsigned song2)
{ {
if (!queue.IsValidPosition(song1) || !queue.IsValidPosition(song2)) if (!queue.IsValidPosition(song1) || !queue.IsValidPosition(song2))
return PlaylistResult::BAD_RANGE; throw PlaylistError::BadRange();
const DetachedSong *const queued_song = GetQueuedSong(); const DetachedSong *const queued_song = GetQueuedSong();
@ -165,35 +165,33 @@ playlist::SwapPositions(PlayerControl &pc, unsigned song1, unsigned song2)
UpdateQueuedSong(pc, queued_song); UpdateQueuedSong(pc, queued_song);
OnModified(); OnModified();
return PlaylistResult::SUCCESS;
} }
PlaylistResult void
playlist::SwapIds(PlayerControl &pc, unsigned id1, unsigned id2) playlist::SwapIds(PlayerControl &pc, unsigned id1, unsigned id2)
{ {
int song1 = queue.IdToPosition(id1); int song1 = queue.IdToPosition(id1);
int song2 = queue.IdToPosition(id2); int song2 = queue.IdToPosition(id2);
if (song1 < 0 || song2 < 0) if (song1 < 0 || song2 < 0)
return PlaylistResult::NO_SUCH_SONG; throw PlaylistError::NoSuchSong();
return SwapPositions(pc, song1, song2); SwapPositions(pc, song1, song2);
} }
PlaylistResult void
playlist::SetPriorityRange(PlayerControl &pc, playlist::SetPriorityRange(PlayerControl &pc,
unsigned start, unsigned end, unsigned start, unsigned end,
uint8_t priority) uint8_t priority)
{ {
if (start >= GetLength()) if (start >= GetLength())
return PlaylistResult::BAD_RANGE; throw PlaylistError::BadRange();
if (end > GetLength()) if (end > GetLength())
end = GetLength(); end = GetLength();
if (start >= end) if (start >= end)
return PlaylistResult::SUCCESS; return;
/* remember "current" and "queued" */ /* remember "current" and "queued" */
@ -211,21 +209,17 @@ playlist::SetPriorityRange(PlayerControl &pc,
UpdateQueuedSong(pc, queued_song); UpdateQueuedSong(pc, queued_song);
OnModified(); OnModified();
return PlaylistResult::SUCCESS;
} }
PlaylistResult void
playlist::SetPriorityId(PlayerControl &pc, playlist::SetPriorityId(PlayerControl &pc,
unsigned song_id, uint8_t priority) unsigned song_id, uint8_t priority)
{ {
int song_position = queue.IdToPosition(song_id); int song_position = queue.IdToPosition(song_id);
if (song_position < 0) if (song_position < 0)
return PlaylistResult::NO_SUCH_SONG; throw PlaylistError::NoSuchSong();
return SetPriorityRange(pc, song_position, song_position + 1,
priority);
SetPriorityRange(pc, song_position, song_position + 1, priority);
} }
void void
@ -272,11 +266,11 @@ playlist::DeleteInternal(PlayerControl &pc,
current--; current--;
} }
PlaylistResult void
playlist::DeletePosition(PlayerControl &pc, unsigned song) playlist::DeletePosition(PlayerControl &pc, unsigned song)
{ {
if (song >= queue.GetLength()) if (song >= queue.GetLength())
return PlaylistResult::BAD_RANGE; throw PlaylistError::BadRange();
const DetachedSong *queued_song = GetQueuedSong(); const DetachedSong *queued_song = GetQueuedSong();
@ -284,21 +278,19 @@ playlist::DeletePosition(PlayerControl &pc, unsigned song)
UpdateQueuedSong(pc, queued_song); UpdateQueuedSong(pc, queued_song);
OnModified(); OnModified();
return PlaylistResult::SUCCESS;
} }
PlaylistResult void
playlist::DeleteRange(PlayerControl &pc, unsigned start, unsigned end) playlist::DeleteRange(PlayerControl &pc, unsigned start, unsigned end)
{ {
if (start >= queue.GetLength()) if (start >= queue.GetLength())
return PlaylistResult::BAD_RANGE; throw PlaylistError::BadRange();
if (end > queue.GetLength()) if (end > queue.GetLength())
end = queue.GetLength(); end = queue.GetLength();
if (start >= end) if (start >= end)
return PlaylistResult::SUCCESS; return;
const DetachedSong *queued_song = GetQueuedSong(); const DetachedSong *queued_song = GetQueuedSong();
@ -308,18 +300,16 @@ playlist::DeleteRange(PlayerControl &pc, unsigned start, unsigned end)
UpdateQueuedSong(pc, queued_song); UpdateQueuedSong(pc, queued_song);
OnModified(); OnModified();
return PlaylistResult::SUCCESS;
} }
PlaylistResult void
playlist::DeleteId(PlayerControl &pc, unsigned id) playlist::DeleteId(PlayerControl &pc, unsigned id)
{ {
int song = queue.IdToPosition(id); int song = queue.IdToPosition(id);
if (song < 0) if (song < 0)
return PlaylistResult::NO_SUCH_SONG; throw PlaylistError::NoSuchSong();
return DeletePosition(pc, song); DeletePosition(pc, song);
} }
void void
@ -330,19 +320,19 @@ playlist::DeleteSong(PlayerControl &pc, const char *uri)
DeletePosition(pc, i); DeletePosition(pc, i);
} }
PlaylistResult void
playlist::MoveRange(PlayerControl &pc, unsigned start, unsigned end, int to) playlist::MoveRange(PlayerControl &pc, unsigned start, unsigned end, int to)
{ {
if (!queue.IsValidPosition(start) || !queue.IsValidPosition(end - 1)) if (!queue.IsValidPosition(start) || !queue.IsValidPosition(end - 1))
return PlaylistResult::BAD_RANGE; throw PlaylistError::BadRange();
if ((to >= 0 && to + end - start - 1 >= GetLength()) || if ((to >= 0 && to + end - start - 1 >= GetLength()) ||
(to < 0 && unsigned(abs(to)) > GetLength())) (to < 0 && unsigned(abs(to)) > GetLength()))
return PlaylistResult::BAD_RANGE; throw PlaylistError::BadRange();
if ((int)start == to) if ((int)start == to)
/* nothing happens */ /* nothing happens */
return PlaylistResult::SUCCESS; return;
const DetachedSong *const queued_song = GetQueuedSong(); const DetachedSong *const queued_song = GetQueuedSong();
@ -355,11 +345,11 @@ playlist::MoveRange(PlayerControl &pc, unsigned start, unsigned end, int to)
if (currentSong < 0) if (currentSong < 0)
/* can't move relative to current song, /* can't move relative to current song,
because there is no current song */ because there is no current song */
return PlaylistResult::BAD_RANGE; throw PlaylistError::BadRange();
if (start <= (unsigned)currentSong && (unsigned)currentSong < end) if (start <= (unsigned)currentSong && (unsigned)currentSong < end)
/* no-op, can't be moved to offset of itself */ /* no-op, can't be moved to offset of itself */
return PlaylistResult::SUCCESS; return;
to = (currentSong + abs(to)) % GetLength(); to = (currentSong + abs(to)) % GetLength();
if (start < (unsigned)to) if (start < (unsigned)to)
to--; to--;
@ -379,18 +369,16 @@ playlist::MoveRange(PlayerControl &pc, unsigned start, unsigned end, int to)
UpdateQueuedSong(pc, queued_song); UpdateQueuedSong(pc, queued_song);
OnModified(); OnModified();
return PlaylistResult::SUCCESS;
} }
PlaylistResult void
playlist::MoveId(PlayerControl &pc, unsigned id1, int to) playlist::MoveId(PlayerControl &pc, unsigned id1, int to)
{ {
int song = queue.IdToPosition(id1); int song = queue.IdToPosition(id1);
if (song < 0) if (song < 0)
return PlaylistResult::NO_SUCH_SONG; throw PlaylistError::NoSuchSong();
return MoveRange(pc, song, song + 1, to); MoveRange(pc, song, song + 1, to);
} }
void void