D
David Garamond
I found myself using exception object (instead of ) more and more. They
are convenient for storing detailed information bits about the error
(pointer to an object structure/within an object structure, some flags,
etc).
Since RCR 21[1] (New method: Exception#message=) was rejected, can
anyone suggest a more elegant way of doing the thing below. I don't like
having to set the message everytime. The message will be the same most
of the time anyway, since more detailed information can be found in the
attributes of the exception object. So I'd like to store the message in
the exception object "template" and change them occassionally when needed.
class SomeError < StandardError
attr_accessor :foo, :bar, :baz
def initialize(foo, bar, baz)
@foo = foo
@bar = bar
@baz = baz
end
end
# a 'template' exception object
err = SomeError.new(1, 2, 3)
# do stuffs
# ...
e.foo = 123
raise e, "Invalid gux!"
# do other stuffs
# ...
e.foo = 234
e.bar = "some bar"
raise e, "Invalid gux!"
# yet other stuffs
# ...
e.baz = "some bar"
raise e, "Invalid gux!"
# yet other stuffs... this time we want a slightly different message
# ...
e.foo = -1
e.bar = "another bar"
raise e, "Totally invalid gux, dude!"
[1] http://www.rcrchive.net/rgarchive/rejected.html#rcr21
are convenient for storing detailed information bits about the error
(pointer to an object structure/within an object structure, some flags,
etc).
Since RCR 21[1] (New method: Exception#message=) was rejected, can
anyone suggest a more elegant way of doing the thing below. I don't like
having to set the message everytime. The message will be the same most
of the time anyway, since more detailed information can be found in the
attributes of the exception object. So I'd like to store the message in
the exception object "template" and change them occassionally when needed.
class SomeError < StandardError
attr_accessor :foo, :bar, :baz
def initialize(foo, bar, baz)
@foo = foo
@bar = bar
@baz = baz
end
end
# a 'template' exception object
err = SomeError.new(1, 2, 3)
# do stuffs
# ...
e.foo = 123
raise e, "Invalid gux!"
# do other stuffs
# ...
e.foo = 234
e.bar = "some bar"
raise e, "Invalid gux!"
# yet other stuffs
# ...
e.baz = "some bar"
raise e, "Invalid gux!"
# yet other stuffs... this time we want a slightly different message
# ...
e.foo = -1
e.bar = "another bar"
raise e, "Totally invalid gux, dude!"
[1] http://www.rcrchive.net/rgarchive/rejected.html#rcr21