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:
parent
8410fc21a8
commit
12b3b61c1b
37
source/ddbus/exception.d
Normal file
37
source/ddbus/exception.d
Normal 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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue