lib/quandl/format/dataset/load.rb in quandl_format-0.2.8 vs lib/quandl/format/dataset/load.rb in quandl_format-0.3.0

- old
+ new

@@ -87,19 +87,26 @@ # return the updated node node end def process_node(node, &block) - node = parse_node(node) - # fail on errored node - return false if node == false - # convert node to dataset - dataset = convert_node_to_dataset(node) - # do whatever we need to do with the node - block.call( dataset ) unless dataset.nil? - # success - true + begin + node = parse_node(node) + # fail on errored node + return false if node == false + # convert node to dataset + dataset = convert_node_to_dataset(node) + # do whatever we need to do with the node + block.call( dataset, nil ) unless dataset.nil? + # success + true + + rescue Exception => err + block.call( nil, err ) + false + + end end def parse_node(node) # parse attrs as yaml node[:attributes] = parse_yaml_attributes(node) @@ -119,57 +126,65 @@ attributes.symbolize_keys!.each do |key, value| attrs[key.to_s.downcase.to_sym] = value end attrs rescue Exception => err - log_yaml_parse_error(node, err) - nil + m = generate_yaml_parse_error(node, err) + raise err, m end def convert_node_to_dataset(node) dataset = Quandl::Format::Dataset.new( node[:attributes] ) dataset.data = node[:data] dataset rescue Exception => err - log_dataset_error(node, err) - nil + m = generate_dataset_error(node, err) + raise err, m end - def log_yaml_parse_error(node, err) + def generate_yaml_parse_error(node, err) message = "" if err.message == 'Unparsable input' message = "Input data is unparsable. Are you missing a colon (:) or a space after a colon?\n" - elsif err.is_a?(Psych::SyntaxError) + elsif err.is_a?(Psych::SyntaxError) && err.respond_to?(:problem) if err.problem =~ /mapping values are not allowed in this context/ - message = "Syntax error before line #{1+node[:offset] + err.line}. Are you missing a colon (:) or a space after a colon?\n" + message = "Syntax error *before* line #{1+node[:offset] + err.line}.\n" + if node[:attributes] =~ /:.+:/ # he probably has a colon in a field. + message += "You might have an illegal colon (:) in one of your fields. If so, use quotes.\n" + elsif node[:attributes] =~ /^([^:]+)$/ # he forgot the colon completely + message += "Did you forget a colon on this line:\n" + message += "#{$1}\n" + end else message += "Error parsing metadata. #{err.problem.capitalize} on line #{node[:offset] + err.line}\n" if err.problem =~ /expected ':'/ message += "Did you forget to delimit the meta data section from the data section with a one or more dashes ('#{SYNTAX[:data]}')?\n" end end + elsif err.is_a?(Psych::SyntaxError) + message = err.to_s + "\n" + node[:attributes] else message += "Attribute parse error at line #{ node[:line] + err.line } column #{err.column}. #{err.problem} (#{err.class})\n" if node.has_key?(:line) && err.respond_to?(:line) - message += "Encountered error while parsing: \n " + node[:attributes].split("\n")[err.line - 1].to_s + "\n" if err.respond_to?(:line) + message += "Encountered error while parsing: \n " + node[:attributes].split("\n")[err.line - 1].to_s if err.respond_to?(:line) end - message += "--" - Quandl::Logger.error(message) + message += "\n" + message end - def log_dataset_error( node, err ) + def generate_dataset_error( node, err ) message = '' message += node[:attributes][:source_code] + '/' if node[:attributes][:source_code].present? message += node[:attributes][:code] + ' ' # include specific line if available if err.respond_to?(:line) message += "error at line #{node[:data_line].to_i + err.line.to_i}\n" else message += "error around line #{node[:line]}\n" end # include original error - message += "#{$!} (#{err.class})\n" - message += "--" - Quandl::Logger.error(message) + message += "#{$!} (#{err.class})" + message += "\n" + message end end end \ No newline at end of file