Skip to content

Commit 7c390dc

Browse files
committed
Fix SystemCallError message when errno is unknown
1 parent 8b010d5 commit 7c390dc

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

core/exception/system_call_error_spec.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,17 @@ def initialize
9292

9393
it "treats nil custom message as if it is not passed at all" do
9494
exc = SystemCallError.new(nil, @example_errno)
95-
exc.should be_an_instance_of(@example_errno_class)
96-
exc.errno.should == @example_errno
9795
exc.message.should == 'Invalid argument'
9896
end
9997

98+
it "sets an 'unknown error' message when an unknown error number" do
99+
SystemCallError.new(-1).message.should =~ /Unknown error(:)? -1/
100+
end
101+
102+
it "adds a custom error message to an 'unknown error' message when an unknown error number and a custom message specified" do
103+
SystemCallError.new("custom message", -1).message.should =~ /Unknown error(:)? -1 - custom message/
104+
end
105+
100106
it "converts to Integer if errno is a Complex convertible to Integer" do
101107
SystemCallError.new('foo', Complex(2.9, 0)).should == SystemCallError.new('foo', 2)
102108
end

optional/capi/kernel_spec.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,27 +187,39 @@ class CApiKernelSpecs::Exc < StandardError
187187
it "raises an exception from the given error" do
188188
-> do
189189
@s.rb_syserr_fail(Errno::EINVAL::Errno, "additional info")
190-
end.should raise_error(Errno::EINVAL, /additional info/)
190+
end.should raise_error(Errno::EINVAL, "Invalid argument - additional info")
191191
end
192192

193193
it "can take a NULL message" do
194194
-> do
195195
@s.rb_syserr_fail(Errno::EINVAL::Errno, nil)
196-
end.should raise_error(Errno::EINVAL)
196+
end.should raise_error(Errno::EINVAL, "Invalid argument")
197+
end
198+
199+
it "uses an 'unknown error' message when errno is unknown" do
200+
-> do
201+
@s.rb_syserr_fail(-10, nil)
202+
end.should raise_error(SystemCallError, /Unknown error(:)? -1/) # a new class Errno::E-01 is generated on the fly
197203
end
198204
end
199205

200206
describe "rb_syserr_fail_str" do
201207
it "raises an exception from the given error" do
202208
-> do
203209
@s.rb_syserr_fail_str(Errno::EINVAL::Errno, "additional info")
204-
end.should raise_error(Errno::EINVAL, /additional info/)
210+
end.should raise_error(Errno::EINVAL, "Invalid argument - additional info")
205211
end
206212

207213
it "can take nil as a message" do
208214
-> do
209215
@s.rb_syserr_fail_str(Errno::EINVAL::Errno, nil)
210-
end.should raise_error(Errno::EINVAL)
216+
end.should raise_error(Errno::EINVAL, "Invalid argument")
217+
end
218+
219+
it "uses an 'unknown error' message when errno is unknown" do
220+
-> do
221+
@s.rb_syserr_fail_str(-1, nil)
222+
end.should raise_error(SystemCallError, /Unknown error(:)? -1/) # a new class Errno::E-01 is generated on the fly
211223
end
212224
end
213225

0 commit comments

Comments
 (0)