From 12b3b61c1b2e535191d3a9d5583f416e3cad50bd Mon Sep 17 00:00:00 2001 From: "Harry T. Vennik" Date: Sat, 8 Jul 2017 15:56:31 +0200 Subject: [PATCH] Move DBusException and wrapErrors to separate module Also improved its implemention. This prepares for new exception types to be added in later commits. --- source/ddbus/exception.d | 37 +++++++++++++++++++++++++++++++++++++ source/ddbus/thin.d | 19 ++----------------- 2 files changed, 39 insertions(+), 17 deletions(-) create mode 100644 source/ddbus/exception.d 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;