input/Plugin: remove InitResult::UNAVAILABLE, throw PluginUnavailable instead

This commit is contained in:
Max Kellermann 2016-09-09 15:11:52 +02:00
parent e7d327226a
commit a73688a2be
7 changed files with 57 additions and 34 deletions

View File

@ -166,6 +166,7 @@ libmpd_a_SOURCES = \
src/queue/PlaylistTag.cxx \ src/queue/PlaylistTag.cxx \
src/queue/PlaylistState.cxx src/queue/PlaylistState.hxx \ src/queue/PlaylistState.cxx src/queue/PlaylistState.hxx \
src/queue/Listener.hxx \ src/queue/Listener.hxx \
src/PluginUnavailable.hxx \
src/ReplayGainConfig.cxx src/ReplayGainConfig.hxx \ src/ReplayGainConfig.cxx src/ReplayGainConfig.hxx \
src/ReplayGainInfo.cxx src/ReplayGainInfo.hxx \ src/ReplayGainInfo.cxx src/ReplayGainInfo.hxx \
src/DetachedSong.cxx src/DetachedSong.hxx \ src/DetachedSong.cxx src/DetachedSong.hxx \

36
src/PluginUnavailable.hxx Normal file
View File

@ -0,0 +1,36 @@
/*
* Copyright 2003-2016 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.
*/
#ifndef MPD_PLUGIN_UNAVAILABLE_HXX
#define MPD_PLUGIN_UNAVAILABLE_HXX
#include <stdexcept>
/**
* An exception class which is used by plugin initializers to indicate
* that this plugin is unavailable. It will be disabled, and MPD can
* continue initialization.
*/
class PluginUnavailable final : public std::runtime_error {
public:
explicit PluginUnavailable(const char *msg)
:std::runtime_error(msg) {}
};
#endif

View File

@ -26,6 +26,7 @@
#include "config/ConfigOption.hxx" #include "config/ConfigOption.hxx"
#include "config/Block.hxx" #include "config/Block.hxx"
#include "Log.hxx" #include "Log.hxx"
#include "PluginUnavailable.hxx"
#include "util/RuntimeError.hxx" #include "util/RuntimeError.hxx"
#include <stdexcept> #include <stdexcept>
@ -59,6 +60,11 @@ input_stream_global_init(Error &error)
result = plugin->init != nullptr result = plugin->init != nullptr
? plugin->init(*block, error) ? plugin->init(*block, error)
: InputPlugin::InitResult::SUCCESS; : InputPlugin::InitResult::SUCCESS;
} catch (const PluginUnavailable &e) {
FormatError(e,
"Input plugin '%s' is unavailable",
plugin->name);
continue;
} catch (const std::runtime_error &e) { } catch (const std::runtime_error &e) {
std::throw_with_nested(FormatRuntimeError("Failed to initialize input plugin '%s'", std::throw_with_nested(FormatRuntimeError("Failed to initialize input plugin '%s'",
plugin->name)); plugin->name));
@ -73,16 +79,6 @@ input_stream_global_init(Error &error)
error.FormatPrefix("Failed to initialize input plugin '%s': ", error.FormatPrefix("Failed to initialize input plugin '%s': ",
plugin->name); plugin->name);
return false; return false;
case InputPlugin::InitResult::UNAVAILABLE:
if (error.IsDefined()) {
FormatError(error,
"Input plugin '%s' is unavailable",
plugin->name);
error.Clear();
}
break;
} }
} }

View File

@ -52,13 +52,6 @@ struct InputPlugin {
* ready to be used. * ready to be used.
*/ */
SUCCESS, SUCCESS,
/**
* The plugin is not available and shall be disabled.
* The #Error may be set describing the situation (to
* be logged).
*/
UNAVAILABLE,
}; };
const char *name; const char *name;
@ -66,8 +59,8 @@ struct InputPlugin {
/** /**
* Global initialization. This method is called when MPD starts. * Global initialization. This method is called when MPD starts.
* *
* @return true on success, false if the plugin should be * Throws #PluginUnavailable if the plugin is not available
* disabled * and shall be disabled.
*/ */
InitResult (*init)(const ConfigBlock &block, Error &error); InitResult (*init)(const ConfigBlock &block, Error &error);

View File

@ -35,6 +35,7 @@
#include "util/Error.hxx" #include "util/Error.hxx"
#include "util/Domain.hxx" #include "util/Domain.hxx"
#include "Log.hxx" #include "Log.hxx"
#include "PluginUnavailable.hxx"
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
@ -533,15 +534,11 @@ CurlMulti::OnTimeout()
*/ */
static InputPlugin::InitResult static InputPlugin::InitResult
input_curl_init(const ConfigBlock &block, Error &error) input_curl_init(const ConfigBlock &block, gcc_unused Error &error)
{ {
CURLcode code = curl_global_init(CURL_GLOBAL_ALL); CURLcode code = curl_global_init(CURL_GLOBAL_ALL);
if (code != CURLE_OK) { if (code != CURLE_OK)
error.Format(curl_domain, code, throw PluginUnavailable(curl_easy_strerror(code));
"curl_global_init() failed: %s",
curl_easy_strerror(code));
return InputPlugin::InitResult::UNAVAILABLE;
}
const auto version_info = curl_version_info(CURLVERSION_FIRST); const auto version_info = curl_version_info(CURLVERSION_FIRST);
if (version_info != nullptr) { if (version_info != nullptr) {
@ -576,8 +573,7 @@ input_curl_init(const ConfigBlock &block, Error &error)
if (multi == nullptr) { if (multi == nullptr) {
curl_slist_free_all(http_200_aliases); curl_slist_free_all(http_200_aliases);
curl_global_cleanup(); curl_global_cleanup();
error.Set(curl_domain, 0, "curl_multi_init() failed"); throw PluginUnavailable("curl_multi_init() failed");
return InputPlugin::InitResult::UNAVAILABLE;
} }
curl_multi = new CurlMulti(io_thread_get(), multi); curl_multi = new CurlMulti(io_thread_get(), multi);

View File

@ -27,6 +27,7 @@
#include "lib/ffmpeg/Error.hxx" #include "lib/ffmpeg/Error.hxx"
#include "../InputStream.hxx" #include "../InputStream.hxx"
#include "../InputPlugin.hxx" #include "../InputPlugin.hxx"
#include "PluginUnavailable.hxx"
#include "util/StringCompare.hxx" #include "util/StringCompare.hxx"
#include "util/Error.hxx" #include "util/Error.hxx"
@ -73,15 +74,13 @@ input_ffmpeg_supported(void)
static InputPlugin::InitResult static InputPlugin::InitResult
input_ffmpeg_init(gcc_unused const ConfigBlock &block, input_ffmpeg_init(gcc_unused const ConfigBlock &block,
Error &error) gcc_unused Error &error)
{ {
FfmpegInit(); FfmpegInit();
/* disable this plugin if there's no registered protocol */ /* disable this plugin if there's no registered protocol */
if (!input_ffmpeg_supported()) { if (!input_ffmpeg_supported())
error.Set(ffmpeg_domain, "No protocol"); throw PluginUnavailable("No protocol");
return InputPlugin::InitResult::UNAVAILABLE;
}
return InputPlugin::InitResult::SUCCESS; return InputPlugin::InitResult::SUCCESS;
} }

View File

@ -23,6 +23,7 @@
#include "lib/smbclient/Mutex.hxx" #include "lib/smbclient/Mutex.hxx"
#include "../InputStream.hxx" #include "../InputStream.hxx"
#include "../InputPlugin.hxx" #include "../InputPlugin.hxx"
#include "PluginUnavailable.hxx"
#include "util/StringCompare.hxx" #include "util/StringCompare.hxx"
#include "util/Error.hxx" #include "util/Error.hxx"
@ -73,7 +74,8 @@ input_smbclient_init(gcc_unused const ConfigBlock &block, gcc_unused Error &erro
try { try {
SmbclientInit(); SmbclientInit();
} catch (const std::runtime_error &e) { } catch (const std::runtime_error &e) {
return InputPlugin::InitResult::UNAVAILABLE; // TODO: use std::throw_with_nested()?
throw PluginUnavailable(e.what());
} }
// TODO: create one global SMBCCTX here? // TODO: create one global SMBCCTX here?