CommandError.cxx 3.54 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 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"
23
#include "LocateUri.hxx"
24
#include "client/Response.hxx"
25
#include "util/Error.hxx"
26
#include "Log.hxx"
27

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

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

39
	case PlaylistResult::ERRNO:
40
		r.Error(ACK_ERROR_SYSTEM, strerror(errno));
41
		return CommandResult::ERROR;
42

43
	case PlaylistResult::DENIED:
44
		r.Error(ACK_ERROR_PERMISSION, "Access denied");
45
		return CommandResult::ERROR;
46

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

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

55
	case PlaylistResult::LIST_EXISTS:
56
		r.Error(ACK_ERROR_EXIST, "Playlist already exists");
57
		return CommandResult::ERROR;
58

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

66
	case PlaylistResult::BAD_RANGE:
67
		r.Error(ACK_ERROR_ARG, "Bad song index");
68
		return CommandResult::ERROR;
69

70
	case PlaylistResult::NOT_PLAYING:
71
		r.Error(ACK_ERROR_PLAYER_SYNC, "Not playing");
72
		return CommandResult::ERROR;
73

74
	case PlaylistResult::TOO_LARGE:
75 76
		r.Error(ACK_ERROR_PLAYLIST_MAX,
			"playlist is at the max size");
77
		return CommandResult::ERROR;
78

79
	case PlaylistResult::DISABLED:
80 81
		r.Error(ACK_ERROR_UNKNOWN,
			"stored playlist support is disabled");
82
		return CommandResult::ERROR;
83 84 85
	}

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

89
CommandResult
90
print_error(Response &r, const Error &error)
91
{
92
	assert(error.IsDefined());
93

94
	LogError(error);
95

96
	if (error.IsDomain(playlist_domain)) {
97
		return print_playlist_result(r,
98
					     PlaylistResult(error.GetCode()));
99
	} else if (error.IsDomain(ack_domain)) {
100
		r.Error((ack)error.GetCode(), error.GetMessage());
101
		return CommandResult::ERROR;
102
#ifdef ENABLE_DATABASE
103 104
	} else if (error.IsDomain(db_domain)) {
		switch ((enum db_error)error.GetCode()) {
105
		case DB_DISABLED:
106
			r.Error(ACK_ERROR_NO_EXIST, error.GetMessage());
107
			return CommandResult::ERROR;
108 109

		case DB_NOT_FOUND:
110
			r.Error(ACK_ERROR_NO_EXIST, "Not found");
111
			return CommandResult::ERROR;
Max Kellermann's avatar
Max Kellermann committed
112 113

		case DB_CONFLICT:
114
			r.Error(ACK_ERROR_ARG, "Conflict");
Max Kellermann's avatar
Max Kellermann committed
115
			return CommandResult::ERROR;
116
		}
117
#endif
118 119 120
	} else if (error.IsDomain(locate_uri_domain)) {
		r.Error(ACK_ERROR_ARG, error.GetMessage());
		return CommandResult::ERROR;
121
	} else if (error.IsDomain(errno_domain)) {
122
		r.Error(ACK_ERROR_SYSTEM, strerror(error.GetCode()));
123
		return CommandResult::ERROR;
124 125
	}

126
	r.Error(ACK_ERROR_UNKNOWN, "error");
127
	return CommandResult::ERROR;
128
}