@@ -201,29 +201,47 @@ def validate_references
201201 end
202202 return if missing . empty?
203203
204- # At this point there is at least one broken/missing reference. Format an error message and
205- # raise. Error message will look like this:
206- # ---
207- # Catalog has broken references: exec[subscribe caller 1] -> subscribe[Exec[subscribe target]];
208- # exec[subscribe caller 2] -> subscribe[Exec[subscribe target]]; exec[subscribe caller 2] ->
209- # subscribe[Exec[subscribe target 2]]
210- # ---
204+ # At this point there is at least one broken/missing reference. Format an error message and raise.
205+ errors = format_missing_references ( missing )
206+ plural = errors =~ /;/ ? 's' : ''
207+ raise OctocatalogDiff ::Errors ::ReferenceValidationError , "Catalog has broken reference#{ plural } : #{ errors } "
208+ end
209+
210+ private
211+
212+ # Private method: Format the missing references into human-readable text
213+ # Error message will look like this:
214+ # ---
215+ # Catalog has broken references: exec[subscribe caller 1](file:line) -> subscribe[Exec[subscribe target]];
216+ # exec[subscribe caller 2](file:line) -> subscribe[Exec[subscribe target]]; exec[subscribe caller 2](file:line) ->
217+ # subscribe[Exec[subscribe target 2]]
218+ # ---
219+ # @param missing [Array] Array of missing references
220+ # @return [String] Formatted references
221+ def format_missing_references ( missing )
222+ unless missing . is_a? ( Array ) && missing . any?
223+ raise ArgumentError , 'format_missing_references() requires a non-empty array as input'
224+ end
225+
211226 formatted_references = missing . map do |obj |
212227 # obj[:target_value] can be a string or an array. If it's an array, create a
213228 # separate error message per element of that array. This allows the total number
214229 # of errors to be correct.
215- src = "#{ obj [ :source ] [ 'type' ] . downcase } [#{ obj [ :source ] [ 'title' ] } ]"
230+ src_ref = "#{ obj [ :source ] [ 'type' ] . downcase } [#{ obj [ :source ] [ 'title' ] } ]"
231+ src_file = if obj [ :source ] [ 'file' ] . nil? || obj [ :source ] [ 'file' ] . empty?
232+ ''
233+ elsif compilation_dir && obj [ :source ] [ 'file' ] . start_with? ( compilation_dir )
234+ "(#{ obj [ :source ] [ 'file' ] [ compilation_dir . length ..-1 ] } :#{ obj [ :source ] [ 'line' ] } )"
235+ else
236+ "(#{ obj [ :source ] [ 'file' ] } :#{ obj [ :source ] [ 'line' ] } )"
237+ end
238+
216239 target_val = obj [ :target_value ] . is_a? ( Array ) ? obj [ :target_value ] : [ obj [ :target_value ] ]
217- target_val . map { |tv | "#{ src } -> #{ obj [ :target_type ] . downcase } [#{ tv } ]" }
218- end
219- formatted_references . flatten!
220- plural = formatted_references . size == 1 ? '' : 's'
221- errors = formatted_references . join ( '; ' )
222- raise OctocatalogDiff ::Errors ::ReferenceValidationError , "Catalog has broken reference#{ plural } : #{ errors } "
240+ target_val . map { |tv | "#{ src_ref } #{ src_file } -> #{ obj [ :target_type ] . downcase } [#{ tv } ]" }
241+ end . flatten
242+ formatted_references . join ( '; ' )
223243 end
224244
225- private
226-
227245 # Private method: Given a list of resources to check, return the references from
228246 # that list that are missing from the catalog. (An empty array returned would indicate
229247 # all references are present in the catalog.)
0 commit comments