CommandError.cxx 3.59 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright (C) 2003-2012 The Music Player Daemon Project
 * 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"
22
#include "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 <glib.h>
28 29 30 31

#include <assert.h>
#include <errno.h>

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

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

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

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

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

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

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

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

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

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

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

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

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

96
	LogError(error);
97

98 99
	if (error.IsDomain(playlist_domain)) {
		return print_playlist_result(client,
100
					     PlaylistResult(error.GetCode()));
101 102 103
	} else if (error.IsDomain(ack_domain)) {
		command_error(client, (ack)error.GetCode(),
			      "%s", error.GetMessage());
104
		return CommandResult::ERROR;
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
	} else if (error.IsDomain(errno_domain)) {
117
		command_error(client, ACK_ERROR_SYSTEM, "%s",
118
			      g_strerror(error.GetCode()));
119
		return CommandResult::ERROR;
120 121 122
	}

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