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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#causeObject (readonly)

The cause / inner-exception, if any, specified to the instance initialiser



138
139
140
# File 'lib/xqsr3/diagnostics/exceptions/with_cause.rb', line 138

def cause
  @cause
end

#optionsObject (readonly)

The options passed to the initialiser, with :cause removed, if present



142
143
144
# File 'lib/xqsr3/diagnostics/exceptions/with_cause.rb', line 142

def options
  @options
end

Instance Method Details

#chained_backtraceObject

A combination of the backtrace(s) of all chained exception(s)



180
181
182
183
184
185
186
187
188
189
# File 'lib/xqsr3/diagnostics/exceptions/with_cause.rb', line 180

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



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/xqsr3/diagnostics/exceptions/with_cause.rb', line 144

def chained_message **options

	return message unless cause
	return message if @uses_cause_message

	m	=	message
	c	=	cause
	cm	=	c.respond_to?(:chained_message) ? c.chained_message(**options) : c.message

	return m if (cm || '').empty?
	return cm if (m || '').empty?

	sep	=	options[:separator] || ': '

	"#{m}#{sep}#{cm}"
end

#chaineesObject

An array of exceptions in the chain, excluding self



162
163
164
165
166
167
168
169
170
171
# File 'lib/xqsr3/diagnostics/exceptions/with_cause.rb', line 162

def chainees

	return [] unless cause

	r = [ cause ]

	r += cause.chainees if cause.respond_to? :chainees

	r
end

#exceptionsObject

An array of exceptions in the chain, including self



174
175
176
177
# File 'lib/xqsr3/diagnostics/exceptions/with_cause.rb', line 174

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:

- 
- +option+::
  • Options:

  • :cause - The exception to be used as a cause, and ensures that that is not inferred from the arguments. May be nil to ensure that no cause is inferred



83
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
# File 'lib/xqsr3/diagnostics/exceptions/with_cause.rb', line 83

def initialize(*args, **options)

	@uses_cause_message = false

	cz = options[:cause]

	if cz

		options = options.reject { |k, v| k == :cause }

		@has_implicit_message = args.empty?

		super *args

		warn 'unexpected implicit message' if @has_implicit_message && self.message != 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.message || '').empty? && cz.class.to_s != cz.message

					@uses_cause_message = true

					args = [ cz.message ]
				end
			end
		else

			cz = $!
		end

		@has_implicit_message = args.empty?

		super *args

		warn 'unexpected implicit message' if @has_implicit_message && self.message != self.class.to_s

		@cause = cz
	end

	@options = options
end