Skip to content

Commit

Permalink
Merge pull request martinhaefner#12 from AudioStreamingPlatform/upstr…
Browse files Browse the repository at this point in the history
…eam/error-handling

Better error handling
  • Loading branch information
martinhaefner authored Mar 31, 2019
2 parents be58081 + 4ea15a0 commit 3f22a24
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 12 deletions.
38 changes: 38 additions & 0 deletions include/simppl/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include <exception>
#include <cstdint>
#include <stdexcept>
#include <sstream>

#include "simppl/detail/constants.h"
#include "simppl/types.h"
Expand Down Expand Up @@ -54,6 +56,42 @@ struct Error : public std::exception
uint32_t serial_;
};

class RuntimeError : public std::runtime_error
{
public:
RuntimeError(const char* action, ::DBusError&& error)
: std::runtime_error(format_what(action, error))
{
dbus_error_init(&error_);
dbus_move_error(&error, &error_);
}

virtual ~RuntimeError()
{
dbus_error_free(&error_);
}

const char* name() const noexcept
{
return error_.name;
}

const char* message() const noexcept
{
return error_.message;
}

private:
static std::string format_what(const char* action, const ::DBusError& error)
{
std::ostringstream ss;
ss << action << ": " << error.name << error.message;
return ss.str();
}

private:
::DBusError error_;
};

} // namespace simppl

Expand Down
28 changes: 17 additions & 11 deletions src/dispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,29 +409,35 @@ void Dispatcher::init(int have_introspection, const char* busname)

assert(!busname || !strncmp(busname, "bus:", 4));

const char* action = "connect";
if (!busname || !strcmp(busname, "bus:session"))
{
action = "dbus_bus_get_private";
conn_ = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
}
else
{
if (!strcmp(busname, "bus:system"))
{
action = "dbus_bus_get_private";
conn_ = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err);
}
else
{
action = "dbus_connection_open_private";
conn_ = dbus_connection_open_private(busname, &err);

if (conn_)
{
dbus_error_init(&err);
action = "dbus_bus_register";
dbus_bus_register(conn_, &err);
}
}
}

assert(!dbus_error_is_set(&err));
if (dbus_error_is_set(&err))
throw RuntimeError(action, std::move(err));
dbus_error_free(&err);

dbus_connection_add_filter(conn_, &signal_filter, this, 0);
Expand All @@ -440,7 +446,8 @@ void Dispatcher::init(int have_introspection, const char* busname)
// response is (name, old, new)
dbus_error_init(&err);
dbus_bus_add_match(conn_, "type='signal',interface='org.freedesktop.DBus',member='NameOwnerChanged',path='/org/freedesktop/DBus',sender='org.freedesktop.DBus'", &err);
assert(!dbus_error_is_set(&err));
if (dbus_error_is_set(&err))
throw RuntimeError("dbus_bus_add_match", std::move(err));
dbus_error_free(&err);

dbus_error_init(&err);
Expand All @@ -449,7 +456,8 @@ void Dispatcher::init(int have_introspection, const char* busname)
<< "type='signal',interface='org.simppl.dispatcher',member='notify_client',path='/org/simppl/dispatcher/" << ::getpid() << '/' << this << "'";

dbus_bus_add_match(conn_, match_string.str().c_str(), &err);
assert(!dbus_error_is_set(&err));
if (dbus_error_is_set(&err))
throw RuntimeError("dbus_bus_add_match", std::move(err));
dbus_error_free(&err);

// call ListNames to get list of available services on the bus
Expand Down Expand Up @@ -507,9 +515,7 @@ void Dispatcher::add_server(SkeletonBase& serv)

if (dbus_error_is_set(&err))
{
// FIXME make exception classes
std::cerr << "dbus_bus_request_name - DBus error: " << err.name << ": " << err.message << std::endl;
dbus_error_free(&err);
throw RuntimeError("dbus_bus_request_name", std::move(err));
}

// register same path as busname, just with / instead of .
Expand All @@ -520,8 +526,7 @@ void Dispatcher::add_server(SkeletonBase& serv)

if (dbus_error_is_set(&err))
{
std::cerr << "dbus_connection_register_object_path - DBus error: " << err.name << ": " << err.message << std::endl;
dbus_error_free(&err);
throw RuntimeError("dbus_connection_register_object_path", std::move(err));
}

serv.disp_ = this;
Expand Down Expand Up @@ -568,8 +573,8 @@ void Dispatcher::register_signal_match(const std::string& match_string)
dbus_error_init(&err);

dbus_bus_add_match(conn_, match_string.c_str(), &err);
assert(!dbus_error_is_set(&err));

if (dbus_error_is_set(&err))
throw RuntimeError("dbus_bus_add_match", std::move(err));
dbus_error_free(&err);

d->signal_matches_[match_string] = 1;
Expand All @@ -591,7 +596,8 @@ void Dispatcher::unregister_signal_match(const std::string& match_string)
dbus_error_init(&err);

dbus_bus_remove_match(conn_, match_string.c_str(), &err);
assert(!dbus_error_is_set(&err));
if (dbus_error_is_set(&err))
throw RuntimeError("dbus_bus_remove_match", std::move(err));

dbus_error_free(&err);
}
Expand Down
2 changes: 1 addition & 1 deletion src/skeletonbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ DBusHandlerResult SkeletonBase::handle_request(DBusMessage* msg)
pm = pm->next_;
}

std::cerr << "method '" << method << "' unknown" << std::endl;
// std::cerr << "method '" << method << "' unknown" << std::endl;
}

return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
Expand Down

0 comments on commit 3f22a24

Please sign in to comment.