Adding helpful error messages to your Ruby code
def map_pattr(node, fieldx, valuex) begin parameter = node.root.elements["parameter[@field='#{fieldx}']"] parameter.add_attribute('value', valuex) parameter rescue puts 'feedpopulated.rb: map_attr() the field ' + fieldx + ' was not found in params.' raise end end
Notice that a raise statement is used to ensure that the system error message is raised and any further code execution is halted.
Without adding a customized helpful message I would be left scratching my head trying to work out what the following system error message meant.
./feedpopulated.rb:27:in `map_pattr': undefined method `add_attribute' for nil:NilClass (NoMethodError) from ./feedpopulated.rb:51:in `create_record' from ./feedpopulated.rb:49:in `each' from ./feedpopulated.rb:49:in `create_record' from ./recordx.rb:91:in `call_create' from ./s3fileuploader_handler.rb:14:in `call' from ./s3fileuploader_handler.rb:40:in `invoke' from ./uploadtwitteraudio.rb:22:in `initialize' from /usr/lib/ruby/1.8/rexml/element.rb:890:in `each' from /usr/lib/ruby/1.8/rexml/xpath.rb:53:in `each' from /usr/lib/ruby/1.8/rexml/element.rb:890:in `each' from ./uploadtwitteraudio.rb:18:in `initialize' from ./uploadtwitteraudio.rb:72:in `new' from ./uploadtwitteraudio.rb:72
Reference: Programming Ruby: The Pragmatic Programmer's Guide - Exceptions, Catch, and Throw [ruby-doc.org]