2012-08-07 22:26:14 +02:00
|
|
|
/*
|
2015-01-01 19:48:13 +01:00
|
|
|
* Copyright (C) 2003-2015 The Music Player Daemon Project
|
2012-08-07 22:26:14 +02:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
2012-09-25 12:10:12 +02:00
|
|
|
#include "CommandError.hxx"
|
2014-01-24 16:18:50 +01:00
|
|
|
#include "db/DatabaseError.hxx"
|
2015-10-22 09:29:02 +02:00
|
|
|
#include "LocateUri.hxx"
|
2015-08-06 22:10:25 +02:00
|
|
|
#include "client/Response.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
|
2015-12-15 23:17:34 +01:00
|
|
|
#include <system_error>
|
|
|
|
|
2012-08-07 22:26:14 +02:00
|
|
|
#include <assert.h>
|
2013-12-15 18:30:25 +01:00
|
|
|
#include <string.h>
|
2012-08-07 22:26:14 +02:00
|
|
|
#include <errno.h>
|
|
|
|
|
2015-11-11 19:49:17 +01:00
|
|
|
gcc_const
|
|
|
|
static enum ack
|
|
|
|
ToAck(PlaylistResult result)
|
|
|
|
{
|
|
|
|
switch (result) {
|
|
|
|
case PlaylistResult::SUCCESS:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PlaylistResult::DENIED:
|
|
|
|
return ACK_ERROR_PERMISSION;
|
|
|
|
|
|
|
|
case PlaylistResult::NO_SUCH_SONG:
|
|
|
|
case PlaylistResult::NO_SUCH_LIST:
|
|
|
|
return ACK_ERROR_NO_EXIST;
|
|
|
|
|
|
|
|
case PlaylistResult::LIST_EXISTS:
|
|
|
|
return ACK_ERROR_EXIST;
|
|
|
|
|
|
|
|
case PlaylistResult::BAD_NAME:
|
|
|
|
case PlaylistResult::BAD_RANGE:
|
|
|
|
return ACK_ERROR_ARG;
|
|
|
|
|
|
|
|
case PlaylistResult::NOT_PLAYING:
|
|
|
|
return ACK_ERROR_PLAYER_SYNC;
|
|
|
|
|
|
|
|
case PlaylistResult::TOO_LARGE:
|
|
|
|
return ACK_ERROR_PLAYLIST_MAX;
|
|
|
|
|
|
|
|
case PlaylistResult::DISABLED:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ACK_ERROR_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2015-08-06 22:10:25 +02:00
|
|
|
print_playlist_result(Response &r, PlaylistResult result)
|
2012-08-07 22:26:14 +02:00
|
|
|
{
|
|
|
|
switch (result) {
|
2013-10-19 19:50:54 +02:00
|
|
|
case PlaylistResult::SUCCESS:
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::OK;
|
2012-08-07 22:26:14 +02:00
|
|
|
|
2013-10-19 19:50:54 +02:00
|
|
|
case PlaylistResult::DENIED:
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_PERMISSION, "Access denied");
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::ERROR;
|
2012-08-07 22:26:14 +02:00
|
|
|
|
2013-10-19 19:50:54 +02:00
|
|
|
case PlaylistResult::NO_SUCH_SONG:
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_NO_EXIST, "No such song");
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::ERROR;
|
2012-08-07 22:26:14 +02:00
|
|
|
|
2013-10-19 19:50:54 +02:00
|
|
|
case PlaylistResult::NO_SUCH_LIST:
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_NO_EXIST, "No such playlist");
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::ERROR;
|
2012-08-07 22:26:14 +02:00
|
|
|
|
2013-10-19 19:50:54 +02:00
|
|
|
case PlaylistResult::LIST_EXISTS:
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_EXIST, "Playlist already exists");
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::ERROR;
|
2012-08-07 22:26:14 +02:00
|
|
|
|
2013-10-19 19:50:54 +02:00
|
|
|
case PlaylistResult::BAD_NAME:
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_ARG,
|
|
|
|
"playlist name is invalid: "
|
|
|
|
"playlist names may not contain slashes,"
|
|
|
|
" newlines or carriage returns");
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::ERROR;
|
2012-08-07 22:26:14 +02:00
|
|
|
|
2013-10-19 19:50:54 +02:00
|
|
|
case PlaylistResult::BAD_RANGE:
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_ARG, "Bad song index");
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::ERROR;
|
2012-08-07 22:26:14 +02:00
|
|
|
|
2013-10-19 19:50:54 +02:00
|
|
|
case PlaylistResult::NOT_PLAYING:
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_PLAYER_SYNC, "Not playing");
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::ERROR;
|
2012-08-07 22:26:14 +02:00
|
|
|
|
2013-10-19 19:50:54 +02:00
|
|
|
case PlaylistResult::TOO_LARGE:
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_PLAYLIST_MAX,
|
|
|
|
"playlist is at the max size");
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::ERROR;
|
2012-08-07 22:26:14 +02:00
|
|
|
|
2013-10-19 19:50:54 +02:00
|
|
|
case PlaylistResult::DISABLED:
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_UNKNOWN,
|
|
|
|
"stored playlist support is disabled");
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::ERROR;
|
2012-08-07 22:26:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(0);
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::ERROR;
|
2012-08-07 22:26:14 +02:00
|
|
|
}
|
|
|
|
|
2015-11-11 19:49:17 +01:00
|
|
|
gcc_pure
|
|
|
|
static enum ack
|
|
|
|
ToAck(const Error &error)
|
2012-08-07 22:26:14 +02:00
|
|
|
{
|
2013-08-10 18:02:44 +02:00
|
|
|
if (error.IsDomain(playlist_domain)) {
|
2015-11-11 19:49:17 +01:00
|
|
|
return ToAck((PlaylistResult)error.GetCode());
|
2013-08-10 18:02:44 +02:00
|
|
|
} else if (error.IsDomain(ack_domain)) {
|
2015-11-11 19:49:17 +01:00
|
|
|
return (enum ack)error.GetCode();
|
2014-01-30 20:29:48 +01:00
|
|
|
#ifdef ENABLE_DATABASE
|
2013-08-10 18:02:44 +02:00
|
|
|
} else if (error.IsDomain(db_domain)) {
|
|
|
|
switch ((enum db_error)error.GetCode()) {
|
2012-08-07 22:26:14 +02:00
|
|
|
case DB_DISABLED:
|
|
|
|
case DB_NOT_FOUND:
|
2015-11-11 19:49:17 +01:00
|
|
|
return ACK_ERROR_NO_EXIST;
|
2014-02-26 08:39:44 +01:00
|
|
|
|
|
|
|
case DB_CONFLICT:
|
2015-11-11 19:49:17 +01:00
|
|
|
return ACK_ERROR_ARG;
|
2012-08-07 22:26:14 +02:00
|
|
|
}
|
2014-01-30 20:29:48 +01:00
|
|
|
#endif
|
2015-10-22 09:29:02 +02:00
|
|
|
} else if (error.IsDomain(locate_uri_domain)) {
|
2015-11-11 19:49:17 +01:00
|
|
|
return ACK_ERROR_ARG;
|
2013-08-10 18:02:44 +02:00
|
|
|
} else if (error.IsDomain(errno_domain)) {
|
2015-11-11 19:49:17 +01:00
|
|
|
return ACK_ERROR_SYSTEM;
|
2012-08-07 22:26:14 +02:00
|
|
|
}
|
|
|
|
|
2015-11-11 19:49:17 +01:00
|
|
|
return ACK_ERROR_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
CommandResult
|
|
|
|
print_error(Response &r, const Error &error)
|
|
|
|
{
|
|
|
|
assert(error.IsDefined());
|
|
|
|
|
|
|
|
LogError(error);
|
|
|
|
|
|
|
|
r.Error(ToAck(error), error.GetMessage());
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::ERROR;
|
2012-08-07 22:26:14 +02:00
|
|
|
}
|
2015-12-15 23:17:34 +01:00
|
|
|
|
2015-12-28 06:53:52 +01:00
|
|
|
gcc_pure
|
|
|
|
static enum ack
|
|
|
|
ToAck(std::exception_ptr ep)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
std::rethrow_exception(ep);
|
|
|
|
} catch (const ProtocolError &pe) {
|
|
|
|
return pe.GetCode();
|
|
|
|
} catch (const PlaylistError &pe) {
|
|
|
|
return ToAck(pe.GetCode());
|
|
|
|
} catch (const std::system_error &e) {
|
|
|
|
return ACK_ERROR_SYSTEM;
|
|
|
|
} catch (...) {
|
|
|
|
return ACK_ERROR_UNKNOWN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-15 23:17:34 +01:00
|
|
|
void
|
2015-12-27 06:43:55 +01:00
|
|
|
PrintError(Response &r, std::exception_ptr ep)
|
2015-12-15 23:17:34 +01:00
|
|
|
{
|
2015-12-27 06:43:55 +01:00
|
|
|
try {
|
|
|
|
std::rethrow_exception(ep);
|
|
|
|
} catch (const std::exception &e) {
|
|
|
|
LogError(e);
|
|
|
|
} catch (...) {
|
|
|
|
}
|
2015-12-15 23:17:34 +01:00
|
|
|
|
|
|
|
try {
|
2015-12-27 06:43:55 +01:00
|
|
|
std::rethrow_exception(ep);
|
|
|
|
} catch (const std::exception &e) {
|
2015-12-28 06:53:52 +01:00
|
|
|
r.Error(ToAck(ep), e.what());
|
2015-12-27 06:43:55 +01:00
|
|
|
} catch (...) {
|
|
|
|
r.Error(ACK_ERROR_UNKNOWN, "Unknown error");
|
2015-12-15 23:17:34 +01:00
|
|
|
}
|
|
|
|
}
|