diff --git a/source/ddbus/exception.d b/source/ddbus/exception.d new file mode 100644 index 0000000..e6e25d3 --- /dev/null +++ b/source/ddbus/exception.d @@ -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); + } +} + diff --git a/source/ddbus/thin.d b/source/ddbus/thin.d index 3268c6b..de60eba 100644 --- a/source/ddbus/thin.d +++ b/source/ddbus/thin.d @@ -12,23 +12,8 @@ import std.conv; import std.range; import std.algorithm; -class DBusException : Exception { - this(DBusError *err) { - 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; -} +// This import is public for backwards compatibility +public import ddbus.exception : wrapErrors, DBusException; struct ObjectPath { private string _value;