Module: Xqsr3::Diagnostics::Exceptions::WithCause
- Defined in:
- lib/xqsr3/diagnostics/exceptions/with_cause.rb
Overview
This inclusion module adds to an exception class the means to chain a cause (aka inner-exception), which is then exposed with the cause
attribute
Examples:
Passing an exception cause as a parameter
Constant Summary collapse
- INSPECT_HIDDEN_FIELDS =
Array of hidden fields
[ 'has_implicit_message', 'uses_cause_message' ]
Instance Attribute Summary collapse
-
#cause ⇒ Object
readonly
The cause / inner-exception, if any, specified to the instance initialiser.
-
#options ⇒ Object
readonly
The options passed to the initialiser, with
:cause
removed, if present.
Instance Method Summary collapse
-
#chained_backtrace ⇒ Object
A combination of the backtrace(s) of all chained exception(s).
-
#chained_message(**options) ⇒ Object
Message obtained by concatenation of all chained exceptions’ messages.
-
#chainees ⇒ Object
An array of exceptions in the chain, excluding
self
. -
#exceptions ⇒ Object
An array of exceptions in the chain, including
self
. -
#initialize(*args, **options) ⇒ Object
Defines an initializer for an exception class that allows a cause (aka an inner exception) to be specified, either as the first or last argument or as a
:cause
option.
Instance Attribute Details
#cause ⇒ Object (readonly)
The cause / inner-exception, if any, specified to the instance initialiser
139 140 141 |
# File 'lib/xqsr3/diagnostics/exceptions/with_cause.rb', line 139 def cause @cause end |
#options ⇒ Object (readonly)
The options passed to the initialiser, with :cause
removed, if present
143 144 145 |
# File 'lib/xqsr3/diagnostics/exceptions/with_cause.rb', line 143 def @options end |
Instance Method Details
#chained_backtrace ⇒ Object
A combination of the backtrace(s) of all chained exception(s)
190 191 192 193 194 195 196 197 198 199 |
# File 'lib/xqsr3/diagnostics/exceptions/with_cause.rb', line 190 def chained_backtrace b = backtrace return b unless cause cb = cause.respond_to?(:chained_backtrace) ? cause.chained_backtrace : cause.backtrace (cb - b) + b end |
#chained_message(**options) ⇒ Object
Message obtained by concatenation of all chained exceptions’ messages
Signature
-
Parameters:
- +options+ Options hash
-
Options:
- +:separator+ (String) A string used to separate each chained exception message. Defaults to ": "
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/xqsr3/diagnostics/exceptions/with_cause.rb', line 154 def ** return unless cause return if @uses_cause_message m = c = cause cm = c.respond_to?(:chained_message) ? c.(**) : c. return m if (cm || '').empty? return cm if (m || '').empty? sep = [:separator] || ': ' "#{m}#{sep}#{cm}" end |
#chainees ⇒ Object
An array of exceptions in the chain, excluding self
172 173 174 175 176 177 178 179 180 181 |
# File 'lib/xqsr3/diagnostics/exceptions/with_cause.rb', line 172 def chainees return [] unless cause r = [ cause ] r += cause.chainees if cause.respond_to? :chainees r end |
#exceptions ⇒ Object
An array of exceptions in the chain, including self
184 185 186 187 |
# File 'lib/xqsr3/diagnostics/exceptions/with_cause.rb', line 184 def exceptions [ self ] + chainees end |
#initialize(*args, **options) ⇒ Object
Defines an initializer for an exception class that allows a cause (aka an inner exception) to be specified, either as the first or last argument or as a :cause
option
Signature
-
Parameters:
-
args
0+ arguments passed through to theinclude
-ing class’ initialiser -
options
Options hash -
Options:
-
:cause
- The exception to be used as a cause, and ensures that that is not inferred from the arguments. May benil
to ensure that no cause is inferred
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/xqsr3/diagnostics/exceptions/with_cause.rb', line 84 def initialize(*args, **) @uses_cause_message = false cz = [:cause] if cz = .reject { |k, v| k == :cause } @has_implicit_message = args.empty? super(*args) warn 'unexpected implicit message' if @has_implicit_message && self. != self.class.to_s @cause = cz else cz_ix = args.index { |arg| ::Exception === arg } if cz_ix args = args.dup cz = args.delete_at cz_ix if args.empty? if !(cz. || '').empty? && cz.class.to_s != cz. @uses_cause_message = true args = [ cz. ] end end else cz = $! end @has_implicit_message = args.empty? super(*args) warn 'unexpected implicit message' if @has_implicit_message && self. != self.class.to_s @cause = cz end @options = end |