db/simple: throw DatabaseError on error

This commit is contained in:
Max Kellermann 2016-02-28 11:19:28 +01:00
parent de938eb621
commit 5b8dce7456
3 changed files with 34 additions and 41 deletions

View File

@ -237,9 +237,14 @@ handle_mount(Client &client, Request args, Response &r)
if (_db != nullptr && _db->IsPlugin(simple_db_plugin)) { if (_db != nullptr && _db->IsPlugin(simple_db_plugin)) {
SimpleDatabase &db = *(SimpleDatabase *)_db; SimpleDatabase &db = *(SimpleDatabase *)_db;
if (!db.Mount(local_uri, remote_uri, error)) { try {
if (!db.Mount(local_uri, remote_uri, error)) {
composite.Unmount(local_uri);
return print_error(r, error);
}
} catch (...) {
composite.Unmount(local_uri); composite.Unmount(local_uri);
return print_error(r, error); throw;
} }
// TODO: call Instance::OnDatabaseModified()? // TODO: call Instance::OnDatabaseModified()?

View File

@ -269,27 +269,21 @@ SimpleDatabase::GetSong(const char *uri, Error &error) const
return prefixed_light_song; return prefixed_light_song;
} }
if (r.uri == nullptr) { if (r.uri == nullptr)
/* it's a directory */ /* it's a directory */
error.Format(db_domain, (int)DatabaseErrorCode::NOT_FOUND, throw DatabaseError(DatabaseErrorCode::NOT_FOUND,
"No such song: %s", uri); "No such song");
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, (int)DatabaseErrorCode::NOT_FOUND, throw DatabaseError(DatabaseErrorCode::NOT_FOUND,
"No such song: %s", uri); "No such song");
return nullptr;
}
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, (int)DatabaseErrorCode::NOT_FOUND, throw DatabaseError(DatabaseErrorCode::NOT_FOUND,
"No such song: %s", uri); "No such song");
return nullptr;
}
light_song = song->Export(); light_song = song->Export();
@ -351,9 +345,8 @@ SimpleDatabase::Visit(const DatabaseSelection &selection,
} }
} }
error.Set(db_domain, (int)DatabaseErrorCode::NOT_FOUND, throw DatabaseError(DatabaseErrorCode::NOT_FOUND,
"No such directory"); "No such directory");
return false;
} }
bool bool
@ -421,8 +414,8 @@ SimpleDatabase::Save()
mtime = fi.GetModificationTime(); mtime = fi.GetModificationTime();
} }
bool void
SimpleDatabase::Mount(const char *uri, Database *db, Error &error) SimpleDatabase::Mount(const char *uri, Database *db)
{ {
#if !CLANG_CHECK_VERSION(3,6) #if !CLANG_CHECK_VERSION(3,6)
/* disabled on clang due to -Wtautological-pointer-compare */ /* disabled on clang due to -Wtautological-pointer-compare */
@ -434,21 +427,16 @@ SimpleDatabase::Mount(const char *uri, Database *db, Error &error)
ScopeDatabaseLock protect; ScopeDatabaseLock protect;
auto r = root->LookupDirectory(uri); auto r = root->LookupDirectory(uri);
if (r.uri == nullptr) { if (r.uri == nullptr)
error.Format(db_domain, (int)DatabaseErrorCode::CONFLICT, throw DatabaseError(DatabaseErrorCode::CONFLICT,
"Already exists: %s", uri); "Already exists");
return false;
}
if (strchr(r.uri, '/') != nullptr) { if (strchr(r.uri, '/') != nullptr)
error.Format(db_domain, (int)DatabaseErrorCode::NOT_FOUND, throw DatabaseError(DatabaseErrorCode::NOT_FOUND,
"Parent not found: %s", uri); "Parent not found");
return false;
}
Directory *mnt = r.directory->CreateChild(r.uri); Directory *mnt = r.directory->CreateChild(r.uri);
mnt->mounted_database = db; mnt->mounted_database = db;
return true;
} }
static constexpr bool static constexpr bool
@ -467,11 +455,9 @@ bool
SimpleDatabase::Mount(const char *local_uri, const char *storage_uri, 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, (int)DatabaseErrorCode::NOT_FOUND, throw DatabaseError(DatabaseErrorCode::NOT_FOUND,
"No 'cache_directory' configured"); "No 'cache_directory' configured");
return false;
}
std::string name(storage_uri); std::string name(storage_uri);
std::replace_if(name.begin(), name.end(), IsUnsafeChar, '_'); std::replace_if(name.begin(), name.end(), IsUnsafeChar, '_');
@ -493,10 +479,12 @@ SimpleDatabase::Mount(const char *local_uri, const char *storage_uri,
// TODO: update the new database instance? // TODO: update the new database instance?
if (!Mount(local_uri, db, error)) { try {
Mount(local_uri, db);
} catch (...) {
db->Close(); db->Close();
delete db; delete db;
return false; throw;
} }
return true; return true;

View File

@ -97,7 +97,7 @@ public:
* success, this object gains ownership of the given #Database * success, this object gains ownership of the given #Database
*/ */
gcc_nonnull_all gcc_nonnull_all
bool Mount(const char *uri, Database *db, Error &error); void Mount(const char *uri, Database *db);
gcc_nonnull_all gcc_nonnull_all
bool Mount(const char *local_uri, const char *storage_uri, bool Mount(const char *local_uri, const char *storage_uri,