db/DatabaseError: rename enum db_error and make strictly-typed

This commit is contained in:
Max Kellermann 2016-02-07 01:01:10 +01:00
parent f097952b42
commit 0d8e44a079
6 changed files with 30 additions and 22 deletions

View File

@ -37,7 +37,8 @@ Database *
Instance::GetDatabase(Error &error) Instance::GetDatabase(Error &error)
{ {
if (database == nullptr) if (database == nullptr)
error.Set(db_domain, DB_DISABLED, "No database"); error.Set(db_domain, int(DatabaseErrorCode::DISABLED),
"No database");
return database; return database;
} }

View File

@ -129,12 +129,12 @@ ToAck(const Error &error)
return (enum ack)error.GetCode(); return (enum ack)error.GetCode();
#ifdef ENABLE_DATABASE #ifdef ENABLE_DATABASE
} else if (error.IsDomain(db_domain)) { } else if (error.IsDomain(db_domain)) {
switch ((enum db_error)error.GetCode()) { switch ((DatabaseErrorCode)error.GetCode()) {
case DB_DISABLED: case DatabaseErrorCode::DISABLED:
case DB_NOT_FOUND: case DatabaseErrorCode::NOT_FOUND:
return ACK_ERROR_NO_EXIST; return ACK_ERROR_NO_EXIST;
case DB_CONFLICT: case DatabaseErrorCode::CONFLICT:
return ACK_ERROR_ARG; return ACK_ERROR_ARG;
} }
#endif #endif

View File

@ -22,16 +22,16 @@
class Domain; class Domain;
enum db_error { enum class DatabaseErrorCode {
/** /**
* The database is disabled, i.e. none is configured in this * The database is disabled, i.e. none is configured in this
* MPD instance. * MPD instance.
*/ */
DB_DISABLED, DISABLED,
DB_NOT_FOUND, NOT_FOUND,
DB_CONFLICT, CONFLICT,
}; };
extern const Domain db_domain; extern const Domain db_domain;

View File

@ -518,7 +518,8 @@ ProxyDatabase::GetSong(const char *uri, Error &error) const
} }
if (song == nullptr) { if (song == nullptr) {
error.Format(db_domain, DB_NOT_FOUND, "No such song: %s", uri); error.Format(db_domain, (int)DatabaseErrorCode::NOT_FOUND,
"No such song: %s", uri);
return nullptr; return nullptr;
} }

View File

@ -263,14 +263,14 @@ SimpleDatabase::GetSong(const char *uri, Error &error) const
if (r.uri == nullptr) { if (r.uri == nullptr) {
/* it's a directory */ /* it's a directory */
error.Format(db_domain, DB_NOT_FOUND, error.Format(db_domain, (int)DatabaseErrorCode::NOT_FOUND,
"No such song: %s", uri); "No such song: %s", uri);
return nullptr; return nullptr;
} }
if (strchr(r.uri, '/') != nullptr) { if (strchr(r.uri, '/') != nullptr) {
/* refers to a URI "below" the actual song */ /* refers to a URI "below" the actual song */
error.Format(db_domain, DB_NOT_FOUND, error.Format(db_domain, (int)DatabaseErrorCode::NOT_FOUND,
"No such song: %s", uri); "No such song: %s", uri);
return nullptr; return nullptr;
} }
@ -278,7 +278,7 @@ SimpleDatabase::GetSong(const char *uri, Error &error) const
const Song *song = r.directory->FindSong(r.uri); const Song *song = r.directory->FindSong(r.uri);
protect.unlock(); protect.unlock();
if (song == nullptr) { if (song == nullptr) {
error.Format(db_domain, DB_NOT_FOUND, error.Format(db_domain, (int)DatabaseErrorCode::NOT_FOUND,
"No such song: %s", uri); "No such song: %s", uri);
return nullptr; return nullptr;
} }
@ -343,7 +343,8 @@ SimpleDatabase::Visit(const DatabaseSelection &selection,
} }
} }
error.Set(db_domain, DB_NOT_FOUND, "No such directory"); error.Set(db_domain, (int)DatabaseErrorCode::NOT_FOUND,
"No such directory");
return false; return false;
} }
@ -426,13 +427,13 @@ SimpleDatabase::Mount(const char *uri, Database *db, Error &error)
auto r = root->LookupDirectory(uri); auto r = root->LookupDirectory(uri);
if (r.uri == nullptr) { if (r.uri == nullptr) {
error.Format(db_domain, DB_CONFLICT, error.Format(db_domain, (int)DatabaseErrorCode::CONFLICT,
"Already exists: %s", uri); "Already exists: %s", uri);
return false; return false;
} }
if (strchr(r.uri, '/') != nullptr) { if (strchr(r.uri, '/') != nullptr) {
error.Format(db_domain, DB_NOT_FOUND, error.Format(db_domain, (int)DatabaseErrorCode::NOT_FOUND,
"Parent not found: %s", uri); "Parent not found: %s", uri);
return false; return false;
} }
@ -459,7 +460,7 @@ SimpleDatabase::Mount(const char *local_uri, const char *storage_uri,
Error &error) Error &error)
{ {
if (cache_path.IsNull()) { if (cache_path.IsNull()) {
error.Format(db_domain, DB_NOT_FOUND, error.Format(db_domain, (int)DatabaseErrorCode::NOT_FOUND,
"No 'cache_directory' configured"); "No 'cache_directory' configured");
return false; return false;
} }

View File

@ -217,7 +217,8 @@ UpnpDatabase::GetSong(const char *uri, Error &error) const
{ {
auto vpath = stringToTokens(uri, "/", true); auto vpath = stringToTokens(uri, "/", true);
if (vpath.size() < 2) { if (vpath.size() < 2) {
error.Format(db_domain, DB_NOT_FOUND, "No such song: %s", uri); error.Format(db_domain, (int)DatabaseErrorCode::NOT_FOUND,
"No such song: %s", uri);
return nullptr; return nullptr;
} }
@ -495,7 +496,8 @@ UpnpDatabase::Namei(const ContentDirectoryService &server,
// Look for the name in the sub-container list // Look for the name in the sub-container list
UPnPDirObject *child = dirbuf.FindObject(i->c_str()); UPnPDirObject *child = dirbuf.FindObject(i->c_str());
if (child == nullptr) { if (child == nullptr) {
error.Format(db_domain, DB_NOT_FOUND, error.Format(db_domain,
(int)DatabaseErrorCode::NOT_FOUND,
"No such object"); "No such object");
return false; return false;
} }
@ -506,7 +508,8 @@ UpnpDatabase::Namei(const ContentDirectoryService &server,
} }
if (child->type != UPnPDirObject::Type::CONTAINER) { if (child->type != UPnPDirObject::Type::CONTAINER) {
error.Format(db_domain, DB_NOT_FOUND, error.Format(db_domain,
(int)DatabaseErrorCode::NOT_FOUND,
"Not a container"); "Not a container");
return false; return false;
} }
@ -604,7 +607,8 @@ UpnpDatabase::VisitServer(const ContentDirectoryService &server,
break; break;
default: default:
error.Format(db_domain, DB_NOT_FOUND, error.Format(db_domain,
(int)DatabaseErrorCode::NOT_FOUND,
"Not found"); "Not found");
return false; return false;
} }
@ -617,7 +621,8 @@ UpnpDatabase::VisitServer(const ContentDirectoryService &server,
if (dirent.type != UPnPDirObject::Type::ITEM || if (dirent.type != UPnPDirObject::Type::ITEM ||
dirent.item_class != UPnPDirObject::ItemClass::MUSIC) { dirent.item_class != UPnPDirObject::ItemClass::MUSIC) {
error.Format(db_domain, DB_NOT_FOUND, error.Format(db_domain,
(int)DatabaseErrorCode::NOT_FOUND,
"Not found"); "Not found");
return false; return false;
} }