PlaylistFile: convert more APIs from Error to std::exception

This commit is contained in:
Max Kellermann 2015-12-28 07:22:10 +01:00
parent 1f184f4aec
commit 37862f0f20
3 changed files with 44 additions and 63 deletions

View File

@ -126,20 +126,20 @@ spl_map_to_fs(const char *name_utf8)
}
/**
* Create an #Error for the current errno.
* Throw an exception for the current errno.
*/
static void
playlist_errno(Error &error)
ThrowPlaylistErrno()
{
switch (errno) {
case ENOENT:
error.Set(playlist_domain, int(PlaylistResult::NO_SUCH_LIST),
"No such playlist");
break;
throw PlaylistError(PlaylistResult::NO_SUCH_LIST,
"No such playlist");
default:
error.SetErrno();
break;
throw std::system_error(std::error_code(errno,
std::system_category()),
"Error");
}
}
@ -303,37 +303,31 @@ spl_move_index(const char *utf8path, unsigned src, unsigned dest)
idle_add(IDLE_STORED_PLAYLIST);
}
bool
spl_clear(const char *utf8path, Error &error)
void
spl_clear(const char *utf8path)
{
const auto path_fs = spl_map_to_fs(utf8path);
assert(!path_fs.IsNull());
FILE *file = FOpen(path_fs, FOpenMode::WriteText);
if (file == nullptr) {
playlist_errno(error);
return false;
}
if (file == nullptr)
ThrowPlaylistErrno();
fclose(file);
idle_add(IDLE_STORED_PLAYLIST);
return true;
}
bool
spl_delete(const char *name_utf8, Error &error)
void
spl_delete(const char *name_utf8)
{
const auto path_fs = spl_map_to_fs(name_utf8);
assert(!path_fs.IsNull());
if (!RemoveFile(path_fs)) {
playlist_errno(error);
return false;
}
if (!RemoveFile(path_fs))
ThrowPlaylistErrno();
idle_add(IDLE_STORED_PLAYLIST);
return true;
}
void
@ -389,33 +383,25 @@ spl_append_uri(const char *utf8file,
return true;
}
static bool
spl_rename_internal(Path from_path_fs, Path to_path_fs,
Error &error)
static void
spl_rename_internal(Path from_path_fs, Path to_path_fs)
{
if (!FileExists(from_path_fs)) {
error.Set(playlist_domain, int(PlaylistResult::NO_SUCH_LIST),
"No such playlist");
return false;
}
if (!FileExists(from_path_fs))
throw PlaylistError(PlaylistResult::NO_SUCH_LIST,
"No such playlist");
if (FileExists(to_path_fs)) {
error.Set(playlist_domain, int(PlaylistResult::LIST_EXISTS),
"Playlist exists already");
return false;
}
if (FileExists(to_path_fs))
throw PlaylistError(PlaylistResult::LIST_EXISTS,
"Playlist exists already");
if (!RenameFile(from_path_fs, to_path_fs)) {
playlist_errno(error);
return false;
}
if (!RenameFile(from_path_fs, to_path_fs))
ThrowPlaylistErrno();
idle_add(IDLE_STORED_PLAYLIST);
return true;
}
bool
spl_rename(const char *utf8from, const char *utf8to, Error &error)
void
spl_rename(const char *utf8from, const char *utf8to)
{
const auto from_path_fs = spl_map_to_fs(utf8from);
assert(!from_path_fs.IsNull());
@ -423,5 +409,5 @@ spl_rename(const char *utf8from, const char *utf8to, Error &error)
const auto to_path_fs = spl_map_to_fs(utf8to);
assert(!to_path_fs.IsNull());
return spl_rename_internal(from_path_fs, to_path_fs, error);
spl_rename_internal(from_path_fs, to_path_fs);
}

View File

@ -62,11 +62,11 @@ LoadPlaylistFile(const char *utf8path);
void
spl_move_index(const char *utf8path, unsigned src, unsigned dest);
bool
spl_clear(const char *utf8path, Error &error);
void
spl_clear(const char *utf8path);
bool
spl_delete(const char *name_utf8, Error &error);
void
spl_delete(const char *name_utf8);
void
spl_remove_index(const char *utf8path, unsigned pos);
@ -79,7 +79,7 @@ spl_append_uri(const char *path_utf8,
const SongLoader &loader, const char *uri_utf8,
Error &error);
bool
spl_rename(const char *utf8from, const char *utf8to, Error &error);
void
spl_rename(const char *utf8from, const char *utf8to);
#endif

View File

@ -110,26 +110,22 @@ handle_listplaylistinfo(Client &client, Request args, Response &r)
}
CommandResult
handle_rm(gcc_unused Client &client, Request args, Response &r)
handle_rm(gcc_unused Client &client, Request args, gcc_unused Response &r)
{
const char *const name = args.front();
Error error;
return spl_delete(name, error)
? CommandResult::OK
: print_error(r, error);
spl_delete(name);
return CommandResult::OK;
}
CommandResult
handle_rename(gcc_unused Client &client, Request args, Response &r)
handle_rename(gcc_unused Client &client, Request args, gcc_unused Response &r)
{
const char *const old_name = args[0];
const char *const new_name = args[1];
Error error;
return spl_rename(old_name, new_name, error)
? CommandResult::OK
: print_error(r, error);
spl_rename(old_name, new_name);
return CommandResult::OK;
}
CommandResult
@ -156,14 +152,13 @@ handle_playlistmove(gcc_unused Client &client,
}
CommandResult
handle_playlistclear(gcc_unused Client &client, Request args, Response &r)
handle_playlistclear(gcc_unused Client &client,
Request args, gcc_unused Response &r)
{
const char *const name = args.front();
Error error;
return spl_clear(name, error)
? CommandResult::OK
: print_error(r, error);
spl_clear(name);
return CommandResult::OK;
}
CommandResult