Module: Xqsr3::Diagnostics::ExceptionUtilities
- Defined in:
- lib/xqsr3/doc_.rb,
lib/xqsr3/diagnostics/exception_utilities.rb
Overview
Exception-related utilities
Components of interest
-
::Xqsr3::Diagnostics::ExceptionUtilities::raise_with_options
Class Method Summary collapse
-
.raise_with_options(*args, **options) ⇒ Object
Raises an instance of a named exception that takes options in its constructor, as in:.
Class Method Details
.raise_with_options(*args, **options) ⇒ Object
Raises an instance of a named exception that takes options in its constructor, as in:
class ArgumentErrorWithOptions < ArgumentError
def initialize(message = nil, **options)
. . .
end
attr_reader :options
end
begin
raise_with_options ArgumentErrorWithOptions, "message-1", opt1: :val1, opt2: 'val2'
rescue => x
$stderr.puts x.options # => {:opt1=>:val1, :opt2=>"val2"}
end
It can also be used with full compatibility with Kernel#raise
, as in:
begin
ArgumentError, "message-2"
rescue => x
$stderr.puts x.class # => ArgumenError
$stderr.puts x. # => "message-2"
end
and:
begin
"message-3"
rescue => x
$stderr.puts x.class # => RuntimeError
$stderr.puts x. # => "message-3"
end
Parameters
args
-
0 or more arguments
options
-
An options hash
Parameter Interpretation
If the first argument is a class, it is assumed to be the exception type that will be raised; if not, RuntimeError is raised as is normal with Kernel#raise. The remaining arguments are passed - via the *
operator - to the class’s constructor.
All options passed to this method are respread - by the **
operator - and passed to the underlying Kernel#raise
call.
Exceptions
-
An instance of
arg[0]
ifargs[0].is_a?(::Class)
; or -
An instance of
RuntimeError
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/xqsr3/diagnostics/exception_utilities.rb', line 118 def self. *args, ** # special handling in case called indirectly called_indirectly = [:called_indirectly_06d353cb_5a6c_47ca_8dbe_ff76359c7e96] case called_indirectly when nil, false called_indirectly = 0 when true called_indirectly = 1 when ::Integer ; else abort "indirect-call option (#{called_indirectly}) has invalid type (#{called_indirectly.class})" end .delete :called_indirectly_06d353cb_5a6c_47ca_8dbe_ff76359c7e96 if called_indirectly # Use cases: # # 1. No options # 2. Non-class and options # 3. Class and options # 3.a Class and options # 3.b Class and options and message # 3.c Class and options and message and call-stack class_given = args.size > 0 && args[0].is_a?(::Class) if class_given && !.empty? = args.shift # 3. Class and options # 3.a Class and options # 3.b Class and options and message # 3.c Class and options and message and call-stack xargs = [] xargs << args.shift unless args.empty? x = .new *xargs, ** rargs = [] rargs << x rargs += [ nil, args.shift ] unless args.empty? # now need to trim backtrace begin Kernel.raise *rargs rescue => x bt = x.backtrace (0..called_indirectly).each { bt.shift } Kernel.raise x, x., bt end end unless .empty? # 2. Non-class and options warn "cannot utilise options in raise_with_options when first argument is non-class" else # 1. No options end # now need to trim backtrace begin Kernel.raise *args rescue => x bt = x.backtrace (0..called_indirectly).each { bt.shift } Kernel.raise x, x., bt end end |