Skip to content

Commit

Permalink
process errors from ldpath
Browse files Browse the repository at this point in the history
  • Loading branch information
elrayle committed Feb 18, 2019
1 parent 0df08a6 commit e333fc3
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 16 deletions.
14 changes: 8 additions & 6 deletions app/models/qa/linked_data/config/context_property_map.rb
Expand Up @@ -5,11 +5,10 @@ module Qa
module LinkedData
module Config
class ContextPropertyMap
PARSE_ERROR_VALUE = 'PARSE ERROR'.freeze
PARSE_LOGGER_ERROR = 'LDPath failed to parse during LDPath program creation.'.freeze
VALUE_ON_ERROR = [].freeze

attr_reader :group_id, # id that identifies which group the property should be in
:label # plain text label extracted from locales or using the default
:label

attr_reader :property_map, :ldpath, :expansion_label_ldpath, :expansion_id_ldpath, :prefixes
private :property_map, :ldpath, :expansion_label_ldpath, :expansion_id_ldpath, :prefixes
Expand Down Expand Up @@ -116,14 +115,17 @@ def ldpath_program(ldpath)
program_code << "property = #{ldpath} \;"
Ldpath::Program.parse program_code
rescue => e
Rails.logger.warn("WARNING: #{PARSE_LOGGER_ERROR} (ldpath='#{ldpath}')\n cause: #{e.message}")
nil
Rails.logger.warn("WARNING: #{I18n.t('qa.linked_data.ldpath.parse_logger_error')} (ldpath='#{ldpath}')\n cause: #{e.message}")
raise StandardError, I18n.t('qa.linked_data.ldpath.parse_error')
end

def ldpath_evaluate(program, graph, subject_uri)
return PARSE_ERROR_VALUE if program.blank?
return VALUE_ON_ERROR if program.blank?
output = program.evaluate subject_uri, graph
output.present? ? output['property'].uniq : nil
rescue => e
Rails.logger.warn("WARNING: #{I18n.t('qa.linked_data.ldpath.evaluate_logger_error')} (ldpath='#{ldpath}')\n cause: #{e.message}")
raise StandardError, I18n.t('qa.linked_data.ldpath.evaluate_error')
end

def expansion_label(graph, uri)
Expand Down
16 changes: 12 additions & 4 deletions app/services/qa/linked_data/mapper/context_mapper_service.rb
Expand Up @@ -21,22 +21,30 @@ class << self
def map_context(graph:, context_map:, subject_uri:)
context = []
context_map.properties.each do |property_map|
values = property_values(property_map, graph, subject_uri)
next if values.blank?
context << populate_property_map(context_map, property_map, values)
populated_property_map = populate_property_map(context_map, property_map, graph, subject_uri)
next if populated_property_map.blank?
context << populated_property_map
end
context
end

private

def populate_property_map(context_map, property_map, values)
def populate_property_map(context_map, property_map, graph, subject_uri)
begin
values = property_values(property_map, graph, subject_uri)
rescue => e
values = Qa::LinkedData::Config::ContextPropertyMap::VALUE_ON_ERROR
error = e.message
end

property_info = {}
property_info["group"] = context_map.group_label(property_map.group_id) if property_map.group?
property_info["property"] = property_map.label
property_info["values"] = values
property_info["selectable"] = property_map.selectable?
property_info["drillable"] = property_map.drillable?
property_info["error"] = error if error.present?
property_info
end

Expand Down
9 changes: 9 additions & 0 deletions config/locales/qa.en.yml
@@ -0,0 +1,9 @@
---
en:
qa:
linked_data:
ldpath:
evaluate_error: LDPATH EVALUATION ERROR (See log for more information)
evaluate_logger_error: LDPath failed to evaluate the graph with the provided ldpath.
parse_error: LDPATH PARSE ERROR (See log for more information)
parse_logger_error: LDPath failed to parse during ldpath program creation.
26 changes: 20 additions & 6 deletions spec/models/linked_data/config/context_property_map_spec.rb
Expand Up @@ -210,17 +210,31 @@
expect(subject.values(graph, subject_uri)).to match_array coordinates
end

context 'when program gets parse error' do
context 'when ldpath_program gets parse error' do
let(:ldpath) { property_map[:ldpath] }
let(:cause) { "undefined method `ascii_tree' for nil:NilClass" }
let(:warning) { Qa::LinkedData::Config::ContextPropertyMap::PARSE_LOGGER_ERROR }
let(:warning) { I18n.t('qa.linked_data.ldpath.parse_logger_error') }
let(:log_message) { "WARNING: #{warning} (ldpath='#{ldpath}')\n cause: #{cause}" }
before do
allow(Ldpath::Program).to receive(:parse).with(anything).and_raise(cause)

before { allow(Ldpath::Program).to receive(:parse).with(anything).and_raise(cause) }

it 'logs error and returns PARSE ERROR as the value' do
expect(Rails.logger).to receive(:warn).with(log_message)
expect { subject.values(graph, subject_uri) }.to raise_error StandardError, I18n.t('qa.linked_data.ldpath.parse_error')
end
end

context 'when ldpath_evaluate gets parse error' do
let(:ldpath) { property_map[:ldpath] }
let(:cause) { "unknown cause" }
let(:warning) { I18n.t('qa.linked_data.ldpath.evaluate_logger_error') }
let(:log_message) { "WARNING: #{warning} (ldpath='#{ldpath}')\n cause: #{cause}" }

before { allow(program).to receive(:evaluate).with(subject_uri, graph).and_raise(cause) }

it 'logs error and returns PARSE ERROR as the value' do
Rails.logger.should_receive(:warn).with(log_message)
expect(subject.values(graph, subject_uri)).to eq Qa::LinkedData::Config::ContextPropertyMap::PARSE_ERROR_VALUE
expect(Rails.logger).to receive(:warn).with(log_message)
expect { subject.values(graph, subject_uri) }.to raise_error StandardError, I18n.t('qa.linked_data.ldpath.evaluate_error')
end
end
end
Expand Down
11 changes: 11 additions & 0 deletions spec/services/linked_data/mapper/context_mapper_service_spec.rb
Expand Up @@ -114,6 +114,17 @@
expect(result['selectable']).to be true
end
end

context 'when error occurs' do
let(:cause) { I18n.t('qa.linked_data.ldpath.parse_error') }
before { allow(occupation_property_map).to receive(:values).with(graph, subject_uri).and_raise(cause) }
it 'includes error message and empty value array' do
result = find_property_to_test(subject, 'Occupation')
expect(result.key?('error')).to be true
expect(result['error']).to eq cause
expect(result['values']).to match_array([])
end
end
end

def find_property_to_test(results, label)
Expand Down

0 comments on commit e333fc3

Please sign in to comment.