Skip to content

Commit

Permalink
[api] move the parsing of the activexml exception into the exception …
Browse files Browse the repository at this point in the history
…itself

No idea why I haven't done this 2008 :)
  • Loading branch information
coolo committed Nov 13, 2012
1 parent 1fad595 commit 38f181d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 35 deletions.
59 changes: 41 additions & 18 deletions src/activexml/transport.rb
Expand Up @@ -9,7 +9,46 @@ def self.setup_transport(schema, host, port)

class Transport

class Error < StandardError; end
class Error < StandardError

def parse!
return @xml if @xml

#Rails.logger.debug "extract #{exception.class} #{exception.message}"
begin
@xml = Xmlhash.parse( exception.message )
rescue TypeError
Rails.logger.error "Couldn't parse error xml: #{self.message[0..120]}"
@xml = {'summary' => self.message[0..120], 'code' => '500'}
return
end
if api_error
message = api_error.value('summary')
api_exception = api_error.value('exception')
else

end
end

def api_exception
parse!
return @xml['exception']
end

def summary
parse!
if @xml.has_key? 'summary'
return @xml['summary']
else
return self.message
end
end

def code
parse!
return @xml['code']
end

class ConnectionError < Error; end
class UnauthorizedError < Error; end
class ForbiddenError < Error; end
Expand Down Expand Up @@ -363,23 +402,7 @@ def handle_response( http_response )
end

def self.extract_error_message exception
message = exception.message[0..120]
code = nil
api_exception = nil
#Rails.logger.debug "extract #{exception.class} #{exception.message}"
begin
api_error = Xmlhash.parse( exception.message )
rescue TypeError
raise exception
end
if api_error
code = api_error['code']
message = api_error.value('summary')
api_exception = api_error.value('exception')
else
Rails.logger.error "Couldn't parse error xml: #{exception.class} #{exception.message[0..120]}"
end
return message, code, api_exception
return exception.summary, exception.code, exception.api_exception
end

end
Expand Down
13 changes: 5 additions & 8 deletions src/api/app/models/bs_request_action.rb
Expand Up @@ -316,8 +316,8 @@ def sourcediff(opts = {})
action_diff += ActiveXML.transport.direct_http(URI(path), method: "POST", timeout: 10)
rescue Timeout::Error
raise DiffError.new("Timeout while diffing #{path}")
rescue ActiveXML::Transport::Error, Suse::Backend::HTTPError
raise DiffError.new("The diff call for #{path} failed")
rescue ActiveXML::Transport::Error => e
raise DiffError.new("The diff call for #{path} failed: #{e.summary}")
end
path = nil # reset
end
Expand All @@ -330,8 +330,8 @@ def sourcediff(opts = {})
action_diff += ActiveXML.transport.direct_http(URI(path), method: "POST", timeout: 10)
rescue Timeout::Error
raise DiffError.new("Timeout while diffing #{path}")
rescue ActiveXML::Transport::Error, Suse::Backend::HTTPError
raise DiffError.new("The diff call for #{path} failed")
rescue ActiveXML::Transport::Error => e
raise DiffError.new("The diff call for #{path} failed: #{e.summary}")
end
elsif self.target_repository
# no source diff
Expand All @@ -347,10 +347,7 @@ def sourcediff(opts = {})
def webui_infos
begin
sd = self.sourcediff(view: 'xml', withissues: true)
rescue DiffError => e
# array of error hash
return [ { error: e.summary } ]
rescue Project::UnknownObjectError, Package::UnknownObjectError => e
rescue DiffError, Project::UnknownObjectError, Package::UnknownObjectError => e
return [ { error: e.message } ]
end
return {} if sd.blank?
Expand Down
9 changes: 0 additions & 9 deletions src/api/lib/opensuse/backend.rb
Expand Up @@ -13,15 +13,6 @@ def to_s
@resp.body
end

def summary
ret = self.to_s
xml = Xmlhash.parse(ret)
if xml.has_key? 'summary'
return xml['summary']
else
return ret
end
end
end

class NotFoundError < HTTPError
Expand Down

0 comments on commit 38f181d

Please sign in to comment.