CommandError.cxx 3.62 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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"
21
#include "CommandError.hxx"
Max Kellermann's avatar
Max Kellermann committed
22
#include "db/DatabaseError.hxx"
Max Kellermann's avatar
Max Kellermann committed
23
#include "protocol/Result.hxx"
24
#include "util/Error.hxx"
25
#include "Log.hxx"
26

27
#include <assert.h>
28
#include <string.h>
29 30
#include <errno.h>

31
CommandResult
32
print_playlist_result(Client &client, PlaylistResult result)
33 34
{
	switch (result) {
35
	case PlaylistResult::SUCCESS:
36
		return CommandResult::OK;
37

38
	case PlaylistResult::ERRNO:
39
		command_error(client, ACK_ERROR_SYSTEM, "%s",
40
			      strerror(errno));
41
		return CommandResult::ERROR;
42

43
	case PlaylistResult::DENIED:
44
		command_error(client, ACK_ERROR_PERMISSION, "Access denied");
45
		return CommandResult::ERROR;
46

47
	case PlaylistResult::NO_SUCH_SONG:
48
		command_error(client, ACK_ERROR_NO_EXIST, "No such song");
49
		return CommandResult::ERROR;
50

51
	case PlaylistResult::NO_SUCH_LIST:
52
		command_error(client, ACK_ERROR_NO_EXIST, "No such playlist");
53
		return CommandResult::ERROR;
54

55
	case PlaylistResult::LIST_EXISTS:
56 57
		command_error(client, ACK_ERROR_EXIST,
			      "Playlist already exists");
58
		return CommandResult::ERROR;
59

60
	case PlaylistResult::BAD_NAME:
61 62 63 64
		command_error(client, ACK_ERROR_ARG,
			      "playlist name is invalid: "
			      "playlist names may not contain slashes,"
			      " newlines or carriage returns");
65
		return CommandResult::ERROR;
66

67
	case PlaylistResult::BAD_RANGE:
68
		command_error(client, ACK_ERROR_ARG, "Bad song index");
69
		return CommandResult::ERROR;
70

71
	case PlaylistResult::NOT_PLAYING:
72
		command_error(client, ACK_ERROR_PLAYER_SYNC, "Not playing");
73
		return CommandResult::ERROR;
74

75
	case PlaylistResult::TOO_LARGE:
76 77
		command_error(client, ACK_ERROR_PLAYLIST_MAX,
			      "playlist is at the max size");
78
		return CommandResult::ERROR;
79

80
	case PlaylistResult::DISABLED:
81 82
		command_error(client, ACK_ERROR_UNKNOWN,
			      "stored playlist support is disabled");
83
		return CommandResult::ERROR;
84 85 86
	}

	assert(0);
87
	return CommandResult::ERROR;
88 89
}

90
CommandResult
91
print_error(Client &client, const Error &error)
92
{
93
	assert(error.IsDefined());
94

95
	LogError(error);
96

97 98
	if (error.IsDomain(playlist_domain)) {
		return print_playlist_result(client,
99
					     PlaylistResult(error.GetCode()));
100 101 102
	} else if (error.IsDomain(ack_domain)) {
		command_error(client, (ack)error.GetCode(),
			      "%s", error.GetMessage());
103
		return CommandResult::ERROR;
104
#ifdef ENABLE_DATABASE
105 106
	} else if (error.IsDomain(db_domain)) {
		switch ((enum db_error)error.GetCode()) {
107 108
		case DB_DISABLED:
			command_error(client, ACK_ERROR_NO_EXIST, "%s",
109
				      error.GetMessage());
110
			return CommandResult::ERROR;
111 112 113

		case DB_NOT_FOUND:
			command_error(client, ACK_ERROR_NO_EXIST, "Not found");
114
			return CommandResult::ERROR;
115
		}
116
#endif
117
	} else if (error.IsDomain(errno_domain)) {
118
		command_error(client, ACK_ERROR_SYSTEM, "%s",
119
			      strerror(error.GetCode()));
120
		return CommandResult::ERROR;
121 122 123
	}

	command_error(client, ACK_ERROR_UNKNOWN, "error");
124
	return CommandResult::ERROR;
125
}