Skip to content

Commit

Permalink
[api] use errors instead of exceptions on missing product repos
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianschroeter committed Jan 8, 2015
1 parent 2093d1a commit 05d3b1d
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions src/api/app/models/product.rb
Expand Up @@ -24,7 +24,11 @@ def set_CPE(swClass, vendor, pversion=nil)
self.cpe += ":#{pversion}" unless pversion.blank?
end

def update_from_xml(xml)
def update_from_xml!(xml)
update_from_xml(xml, true)
end

def update_from_xml(xml, ignore_errors=nil)
self.transaction do
xml.elements('productdefinition') do |pd|
# we are either an operating system or an application for CPE
Expand All @@ -46,7 +50,15 @@ def update_from_xml(xml)
self.version = pversion
# update update channel connections
p.elements('register') do |r|
_update_from_xml_register(r)
if ignore_errors
begin
_update_from_xml_register(r)
rescue NotFoundError
rescue UnknownRepository
end
else
_update_from_xml_register(r)
end
end
end
end
Expand All @@ -71,7 +83,10 @@ def _update_from_xml_register_pool(rxml)
u.elements('repository') do |repo|
next if repo['project'].blank? # it may be just a url= reference
poolRepo = Repository.find_by_project_and_repo_name(repo['project'], repo['name'])
raise UnknownRepository.new "Pool repository #{repo['project']}/#{repo['name']} does not exist" unless poolRepo
unless poolRepo
errors.add(:missing, "Pool repository #{repo['project']}/#{repo['name']} missing")
next
end
name = repo.get('medium')
arch = repo.get('arch')
key = "#{poolRepo.id}/#{name}"
Expand All @@ -82,8 +97,11 @@ def _update_from_xml_register_pool(rxml)
p = {product: self, repository: poolRepo, name: name}
unless arch.blank?
arch_filter = Architecture.find_by_name(arch)
raise NotFoundError.new("Architecture #{arch} not valid") unless arch_filter
p[:arch_filter_id] = arch_filter.id
if arch_filter
p[:arch_filter_id] = arch_filter.id
else
errors.add(:invalid, "Architecture #{arch} not valid")
end
end
self.product_media.create(p)
end
Expand All @@ -110,8 +128,11 @@ def _update_from_xml_register_update(rxml)
unless arch.blank?
key += "/#{arch}"
arch_filter = Architecture.find_by_name(arch)
raise NotFoundError.new("Architecture #{arch} not valid") unless arch_filter
p[:arch_filter_id] = arch_filter.id
if arch_filter
p[:arch_filter_id] = arch_filter.id
else
errors.add(:invalid, "Architecture #{arch} not valid")
end
end
ProductUpdateRepository.create(p) unless update[key]
update.delete(key)
Expand Down

0 comments on commit 05d3b1d

Please sign in to comment.