ddbus icon indicating copy to clipboard operation
ddbus copied to clipboard

Translate Exception to DBus error Message in MessageRouter.setHandler

Open quickfur opened this issue 5 years ago • 1 comments

There is currently no wrapper for dbus_message_new_error, and since registered handlers don't receive the request Message, the only way to implement a method that returns a DBus error is to bypass all the nice syntactic sugar ddbus provides.

I wrote a quick fix to improve this: added a DBusErrorReturn class inheriting from Exception, containing an extra field to specify the DBus error name, and added a catch block in setHandler so that if a DBusErrorReturn is caught, it translates that to dbus_message_new_error.

Would this change be acceptable upstream? Or is there a better way to do this? Just wondering before I submit a PR. (I've done a quick test and it seems to do the right thing.)

quickfur avatar Jan 12 '21 09:01 quickfur

I think having a class inheriting from Exception is good API design for ddbus and would accept that. However we might also want to think of a solution for potential betterC/nogc code support in the future.

Can you send some example code how you use it right now? I think using throw with a custom type is something we want to include for idiomatic code, but there should also be future support returning some wrapper containing error or result with a tuple which might affect how the API looks.

WebFreak001 avatar Jan 12 '21 10:01 WebFreak001