Move DBusException and wrapErrors to separate module

Also improved its implemention.
This prepares for new exception types to be added in later commits.
This commit is contained in:
Harry T. Vennik 2017-07-08 15:56:31 +02:00
parent 8410fc21a8
commit 12b3b61c1b
2 changed files with 39 additions and 17 deletions

37
source/ddbus/exception.d Normal file
View file

@ -0,0 +1,37 @@
module ddbus.exception;
import ddbus.c_lib;
package T wrapErrors(T)(
T delegate(DBusError *err) del,
string file = __FILE__,
size_t line = __LINE__,
Throwable next = null
) {
DBusError error;
dbus_error_init(&error);
T ret = del(&error);
if(dbus_error_is_set(&error)) {
auto ex = new DBusException(&error, file, line, next);
dbus_error_free(&error);
throw ex;
}
return ret;
}
/++
Thrown when a DBus error code was returned by libdbus.
+/
class DBusException : Exception {
private this(
scope DBusError *err,
string file = __FILE__,
size_t line = __LINE__,
Throwable next = null
) pure nothrow {
import std.string : fromStringz;
super(err.message.fromStringz().idup, file, line, next);
}
}

View file

@ -12,23 +12,8 @@ import std.conv;
import std.range; import std.range;
import std.algorithm; import std.algorithm;
class DBusException : Exception { // This import is public for backwards compatibility
this(DBusError *err) { public import ddbus.exception : wrapErrors, DBusException;
super(err.message.fromStringz().idup);
}
}
T wrapErrors(T)(T delegate(DBusError *err) del) {
DBusError error;
dbus_error_init(&error);
T ret = del(&error);
if(dbus_error_is_set(&error)) {
auto ex = new DBusException(&error);
dbus_error_free(&error);
throw ex;
}
return ret;
}
struct ObjectPath { struct ObjectPath {
private string _value; private string _value;