Bug 1443420

Summary: [RFE] Add a mechanism to check the HTTP result code of errors
Product: [oVirt] ovirt-engine-sdk-ruby Reporter: Juan Hernández <juan.hernandez>
Component: CoreAssignee: Juan Hernández <juan.hernandez>
Status: CLOSED CURRENTRELEASE QA Contact: Aleksei Slaikovskii <aslaikov>
Severity: high Docs Contact:
Priority: unspecified    
Version: 4.1.5CC: aslaikov, bugs, lsvaty, mgoldboi, stirabos
Target Milestone: ovirt-4.1.3Keywords: FutureFeature
Target Release: 4.1.6Flags: rule-engine: ovirt-4.1+
lsvaty: testing_plan_complete-
mgoldboi: planning_ack+
juan.hernandez: devel_ack+
lsvaty: testing_ack+
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: 4.1.6 Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2017-07-06 13:24:59 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: Infra RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Juan Hernández 2017-04-19 08:41:27 UTC
Currently when the SDK raises an exception to indicate an unexpected HTTP result code the details are embedded in the error message. For example, when trying to retrieve a VM that doesn't exist the SDK generates the following error message:

  HTTP response code is 404.

But there isn't a simple way to extract that code, there is no `code` method in the OvirtSDK4::Error class. This means that the application that uses the SDK doesn't have a simple way to handle differently different kinds of errors.

This should be improved, adding a `code` method to the Error class:

  begin
    ...
  rescue OvirtSDK4::Error => e
    if e.code == 404
      ...
    else
      raise
    end
  end

Comment 1 Aleksei Slaikovskii 2017-06-19 08:48:48 UTC
Verified on rubygem-ovirt-engine-sdk4-4.1.6-1.el7ev.x86_64

$ cat 1443420.rb
require 'logger'
require 'ovirtsdk4'

connection = OvirtSDK4::Connection.new(
  url: 'https://engine/ovirt-engine/api',
  username: 'admin@internal',
  password: '123456',
  ca_file: 'ca.crt',
  debug: true,
  log: Logger.new('example.log')
)

begin
 vms_service = connection.system_service.vms_service
  vm_service = vms_service.vm_service('123')
  vm_service.get
rescue OvirtSDK4::Error => e
  if e.code == 404
    puts 'Got 404'
  else
    raise
  end
end

connection.close

$ ruby 1443420.rb
Got 404