Skip to content

Commit 6f6b280

Browse files
committed
Mention qualified property name in Get or Set errors (TODO PR num)
Before: The caller gets a D-Bus Error which includes interface and method name and they are org.freedesktop.DBus.Properties.Get which is not enough. After: Also include the property name and its interface.
1 parent 7a5c4ad commit 6f6b280

File tree

3 files changed

+46
-19
lines changed

3 files changed

+46
-19
lines changed

lib/dbus/object.rb

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -455,11 +455,16 @@ def interfaces_and_properties
455455
property = dbus_lookup_property(interface_name, property_name)
456456

457457
if property.readable?
458-
ruby_name = property.ruby_name
459-
value = public_send(ruby_name)
460-
# may raise, DBus.error or https://ruby-doc.com/core-3.1.0/TypeError.html
461-
typed_value = Data.make_typed(property.type, value)
462-
[typed_value]
458+
begin
459+
ruby_name = property.ruby_name
460+
value = public_send(ruby_name)
461+
# may raise, DBus.error or https://ruby-doc.com/core-3.1.0/TypeError.html
462+
typed_value = Data.make_typed(property.type, value)
463+
[typed_value]
464+
rescue StandardError => e
465+
msg = "When getting '#{interface_name}.#{property_name}': " + e.message
466+
raise e.exception(msg)
467+
end
463468
else
464469
raise DBus.error("org.freedesktop.DBus.Error.PropertyWriteOnly"),
465470
"Property '#{interface_name}.#{property_name}' (on object '#{@path}') is not readable"
@@ -470,11 +475,16 @@ def interfaces_and_properties
470475
property = dbus_lookup_property(interface_name, property_name)
471476

472477
if property.writable?
473-
ruby_name_eq = "#{property.ruby_name}="
474-
# TODO: declare dbus_method :Set to take :exact argument
475-
# and type check it here before passing its :plain value
476-
# to the implementation
477-
public_send(ruby_name_eq, value)
478+
begin
479+
ruby_name_eq = "#{property.ruby_name}="
480+
# TODO: declare dbus_method :Set to take :exact argument
481+
# and type check it here before passing its :plain value
482+
# to the implementation
483+
public_send(ruby_name_eq, value)
484+
rescue StandardError => e
485+
msg = "When setting '#{interface_name}.#{property_name}': " + e.message
486+
raise e.exception(msg)
487+
end
478488
else
479489
raise DBus.error("org.freedesktop.DBus.Error.PropertyReadOnly"),
480490
"Property '#{interface_name}.#{property_name}' (on object '#{@path}') is not writable"

spec/mock-service/spaghetti-monster.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ def initialize(path)
122122
dbus_attr_reader :read_me, "s"
123123

124124
def write_me=(value)
125+
raise "We don't talk about Bruno" if value =~ /Bruno/
126+
125127
@read_me = value
126128
end
127129
dbus_writer :write_me, "s"

spec/property_spec.rb

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,36 @@
3131
expect(iface["ReadMe"]).to eq("READ ME")
3232
end
3333

34-
it "gets an error when reading a property whose implementation raises" do
35-
expect { @iface["Explosive"] }.to raise_error(DBus::Error, /Something failed/)
34+
context "when reading a property fails" do
35+
it "gets an error, mentioning the qualified property name" do
36+
expect { @iface["Explosive"] } \
37+
.to raise_error(DBus::Error, /getting.*SampleInterface.Explosive.*Something failed/)
38+
end
3639
end
3740

3841
it "tests property nonreading" do
3942
expect { @iface["WriteMe"] }.to raise_error(DBus::Error, /not readable/)
4043
end
4144

42-
it "tests property writing" do
43-
@iface["ReadOrWriteMe"] = "VALUE"
44-
expect(@iface["ReadOrWriteMe"]).to eq("VALUE")
45+
context "writing properties" do
46+
it "tests property writing" do
47+
@iface["ReadOrWriteMe"] = "VALUE"
48+
expect(@iface["ReadOrWriteMe"]).to eq("VALUE")
49+
end
50+
51+
context "when writing a read-only property" do
52+
it "gets an error, mentioning the qualified property name" do
53+
expect { @iface["ReadMe"] = "WROTE" } \
54+
.to raise_error(DBus::Error, /SampleInterface.ReadMe.*not writable/)
55+
end
56+
end
57+
58+
context "when writing a property fails" do
59+
it "gets an error, mentioning the qualified property name" do
60+
expect { @iface["WriteMe"] = "Bruno is a city in Czechia" } \
61+
.to raise_error(DBus::Error, /setting.*SampleInterface.WriteMe/)
62+
end
63+
end
4564
end
4665

4766
# https://github.com/mvidner/ruby-dbus/pull/19
@@ -54,10 +73,6 @@
5473
expect(@iface["ReadOrWriteMe"]).to eq("VALUE")
5574
end
5675

57-
it "tests property nonwriting" do
58-
expect { @iface["ReadMe"] = "WROTE" }.to raise_error(DBus::Error, /not writable/)
59-
end
60-
6176
it "tests get all" do
6277
all = @iface.all_properties
6378
expect(all.keys.sort).to eq(["MyArray", "MyByte", "MyDict", "MyStruct", "MyVariant", "ReadMe", "ReadOrWriteMe"])

0 commit comments

Comments
 (0)