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 static void
playlist_errno(Error &error) ThrowPlaylistErrno()
{ {
switch (errno) { switch (errno) {
case ENOENT: case ENOENT:
error.Set(playlist_domain, int(PlaylistResult::NO_SUCH_LIST), throw PlaylistError(PlaylistResult::NO_SUCH_LIST,
"No such playlist"); "No such playlist");
break;
default: default:
error.SetErrno(); throw std::system_error(std::error_code(errno,
break; std::system_category()),
"Error");
} }
} }
@ -303,37 +303,31 @@ spl_move_index(const char *utf8path, unsigned src, unsigned dest)
idle_add(IDLE_STORED_PLAYLIST); idle_add(IDLE_STORED_PLAYLIST);
} }
bool void
spl_clear(const char *utf8path, Error &error) spl_clear(const char *utf8path)
{ {
const auto path_fs = spl_map_to_fs(utf8path); const auto path_fs = spl_map_to_fs(utf8path);
assert(!path_fs.IsNull()); assert(!path_fs.IsNull());
FILE *file = FOpen(path_fs, FOpenMode::WriteText); FILE *file = FOpen(path_fs, FOpenMode::WriteText);
if (file == nullptr) { if (file == nullptr)
playlist_errno(error); ThrowPlaylistErrno();
return false;
}
fclose(file); fclose(file);
idle_add(IDLE_STORED_PLAYLIST); idle_add(IDLE_STORED_PLAYLIST);
return true;
} }
bool void
spl_delete(const char *name_utf8, Error &error) spl_delete(const char *name_utf8)
{ {
const auto path_fs = spl_map_to_fs(name_utf8); const auto path_fs = spl_map_to_fs(name_utf8);
assert(!path_fs.IsNull()); assert(!path_fs.IsNull());
if (!RemoveFile(path_fs)) { if (!RemoveFile(path_fs))
playlist_errno(error); ThrowPlaylistErrno();
return false;
}
idle_add(IDLE_STORED_PLAYLIST); idle_add(IDLE_STORED_PLAYLIST);
return true;
} }
void void
@ -389,33 +383,25 @@ spl_append_uri(const char *utf8file,
return true; return true;
} }
static bool static void
spl_rename_internal(Path from_path_fs, Path to_path_fs, spl_rename_internal(Path from_path_fs, Path to_path_fs)
Error &error)
{ {
if (!FileExists(from_path_fs)) { if (!FileExists(from_path_fs))
error.Set(playlist_domain, int(PlaylistResult::NO_SUCH_LIST), throw PlaylistError(PlaylistResult::NO_SUCH_LIST,
"No such playlist"); "No such playlist");
return false;
}
if (FileExists(to_path_fs)) { if (FileExists(to_path_fs))
error.Set(playlist_domain, int(PlaylistResult::LIST_EXISTS), throw PlaylistError(PlaylistResult::LIST_EXISTS,
"Playlist exists already"); "Playlist exists already");
return false;
}
if (!RenameFile(from_path_fs, to_path_fs)) { if (!RenameFile(from_path_fs, to_path_fs))
playlist_errno(error); ThrowPlaylistErrno();
return false;
}
idle_add(IDLE_STORED_PLAYLIST); idle_add(IDLE_STORED_PLAYLIST);
return true;
} }
bool void
spl_rename(const char *utf8from, const char *utf8to, Error &error) spl_rename(const char *utf8from, const char *utf8to)
{ {
const auto from_path_fs = spl_map_to_fs(utf8from); const auto from_path_fs = spl_map_to_fs(utf8from);
assert(!from_path_fs.IsNull()); 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); const auto to_path_fs = spl_map_to_fs(utf8to);
assert(!to_path_fs.IsNull()); 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 void
spl_move_index(const char *utf8path, unsigned src, unsigned dest); spl_move_index(const char *utf8path, unsigned src, unsigned dest);
bool void
spl_clear(const char *utf8path, Error &error); spl_clear(const char *utf8path);
bool void
spl_delete(const char *name_utf8, Error &error); spl_delete(const char *name_utf8);
void void
spl_remove_index(const char *utf8path, unsigned pos); 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, const SongLoader &loader, const char *uri_utf8,
Error &error); Error &error);
bool void
spl_rename(const char *utf8from, const char *utf8to, Error &error); spl_rename(const char *utf8from, const char *utf8to);
#endif #endif

View File

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