Skip to content

Commit

Permalink
SwMgmt.init - use exceptions for indicating errors
Browse files Browse the repository at this point in the history
  • Loading branch information
lslezak committed Sep 7, 2015
1 parent 057a3a0 commit a4742db
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 50 deletions.
5 changes: 1 addition & 4 deletions src/clients/scc.rb
Expand Up @@ -48,10 +48,7 @@ def main
Wizard.CreateDialog

begin
if !::Registration::SwMgmt.init
Report.Error(Pkg.LastError)
return :abort
end
::Registration::SwMgmt.init

return WFM.call("inst_scc")
ensure
Expand Down
16 changes: 11 additions & 5 deletions src/lib/registration/sw_mgmt.rb
Expand Up @@ -62,14 +62,16 @@ class SwMgmt
def self.init(load_packages = false)
# false = do not allow continuing without the libzypp lock
lock = PackageLock.Connect(false)
return false unless lock["connected"]
raise pkg_exception unless lock["connected"]

# display progress when refreshing repositories
PackageCallbacks.InitPackageCallbacks
Pkg.TargetInitialize(Installation.destdir)
Pkg.TargetLoad
Pkg.SourceRestore
load_packages ? Pkg.SourceLoad : true

raise pkg_exception unless Pkg.TargetInitialize(Installation.destdir)
raise pkg_exception unless Pkg.TargetLoad
raise pkg_exception unless Pkg.SourceRestore

raise pkg_exception if load_packages && !Pkg.SourceLoad

This comment has been minimized.

Copy link
@jreidinger

jreidinger Sep 7, 2015

Member

to be honest I think it make sense to have method raise_exception or raise_pkg_exception or raise_error to avoid this raise duplication. I see no additional value in raising outside of method.

end

# during installation /etc/zypp directory is not writable (mounted on
Expand Down Expand Up @@ -471,6 +473,10 @@ def self.get_release_type(product)
product["register_release"]
end

def self.pkg_exception
PkgError.new(Pkg.LastError)
end

private_class_method :each_repo, :get_release_type
end
end
5 changes: 1 addition & 4 deletions src/lib/registration/ui/autoyast_config_workflow.rb
Expand Up @@ -93,10 +93,7 @@ def select_addons
# download the addons from SCC, let the user select addons to install
# @return [Symbol] the user input
def select_remote_addons
if !SwMgmt.init
Report.Error(Pkg.LastError)
return :abort
end
SwMgmt.init

url = UrlHelpers.registration_url
registration = ::Registration::Registration.new(url)
Expand Down
15 changes: 10 additions & 5 deletions src/lib/registration/ui/media_addon_workflow.rb
Expand Up @@ -86,7 +86,15 @@ def run
}

log.info "Starting registering media add-on sequence"
Sequencer.Run(aliases, sequence)

begin
Sequencer.Run(aliases, sequence)
rescue => e
log.error "Caught error: #{e.class}: #{e.message.inspect}, #{e.backtrace}"
# TRANSLATORS: error message, %s are details
Yast::Report.Error(_("Internal error: %s") % e.message)
:abort
end
end

private
Expand All @@ -96,10 +104,7 @@ def run
# check if the add-on repository provides a product resolvable
# @return [Symbol] workflow symbol (:next, :finish or :abort)
def find_products
if !SwMgmt.init(true)
Report.Error(Pkg.LastError)
return :abort
end
SwMgmt.init(true)

self.products = SwMgmt.products_from_repo(repo_id)

Expand Down
19 changes: 7 additions & 12 deletions src/lib/registration/ui/migration_finish_workflow.rb
Expand Up @@ -43,17 +43,12 @@ def initialize
# - restore the saved repository states (i.e. enable the Updates
# repositories when they were disabled during migration)
def run
ret = nil
begin
ret = run_sequence
rescue => e
log.error "Caught error: #{e.class}: #{e.message.inspect}, #{e.backtrace}"
# TRANSLATORS: error message, %s are details
Yast::Report.Error(_("Internal error: %s") % e.message)
ret = :abort
end

ret
run_sequence
rescue => e
log.error "Caught error: #{e.class}: #{e.message.inspect}, #{e.backtrace}"
# TRANSLATORS: error message, %s are details
Yast::Report.Error(_("Internal error: %s") % e.message)
return :abort
end

private
Expand All @@ -74,7 +69,7 @@ def run_sequence
}

ui = Yast::Sequencer.Run(aliases, WORKFLOW_SEQUENCE)
log.info "User input: #{ui}"
log.info "Workflow result: #{ui}"
ui
end

Expand Down
5 changes: 1 addition & 4 deletions src/lib/registration/ui/migration_repos_workflow.rb
Expand Up @@ -157,10 +157,7 @@ def run_sequence
def find_products
log.info "Loading installed products"

if !SwMgmt.init(true)
Yast::Report.Error(Yast::Pkg.LastError)
return :abort
end
SwMgmt.init(true)

self.products = ::Registration::SwMgmt.installed_products.map do |product|
::Registration::SwMgmt.remote_product(product)
Expand Down
8 changes: 4 additions & 4 deletions test/media_addon_workflow_spec.rb
Expand Up @@ -17,14 +17,16 @@

before do
# SwMgmt initialization
allow(Registration::SwMgmt).to receive(:init).and_return swmgmt_init
allow(Registration::SwMgmt).to receive(:init)
# List of products
allow(Registration::SwMgmt).to receive(:products_from_repo)
.and_return(products_from_repo)
end

context "if package management initialization fails" do
let(:swmgmt_init) { false }
before do
allow(Registration::SwMgmt).to receive(:init).and_raise(Registration::PkgError, "error")
end

it "aborts" do
allow(Yast::Pkg).to receive(:LastError)
Expand All @@ -34,8 +36,6 @@
end

context "if package management initialization success" do
let(:swmgmt_init) { true }

before do
# Load source information
allow(Yast::Pkg).to receive(:SourceLoad)
Expand Down
5 changes: 2 additions & 3 deletions test/migration_repos_workflow_spec.rb
Expand Up @@ -19,8 +19,7 @@

it "aborts if package management initialization fails" do
msg = "Initialization failed"
expect(Registration::SwMgmt).to receive(:init).and_return(false)
expect(Yast::Pkg).to receive(:LastError).and_return(msg)
expect(Registration::SwMgmt).to receive(:init).and_raise(Registration::PkgError, msg)

expect(subject.run).to eq(:abort)
end
Expand All @@ -37,7 +36,7 @@
let(:migration_service) { load_yaml_fixture("migration_service.yml") }

before do
expect(Registration::SwMgmt).to receive(:init).at_least(1).and_return(true)
expect(Registration::SwMgmt).to receive(:init).at_least(1)
allow_any_instance_of(Registration::RepoStateStorage).to receive(:write)
end

Expand Down
2 changes: 1 addition & 1 deletion test/scc_test.rb
Expand Up @@ -16,7 +16,7 @@
expect(Yast::Wizard).to receive(:CreateDialog)
expect(Yast::Wizard).to receive(:CloseDialog)

expect(Registration::SwMgmt).to receive(:init).and_return(true)
expect(Registration::SwMgmt).to receive(:init)
expect(Registration::SwMgmt).to receive(:find_base_product).and_return("name" => "SLES")
end

Expand Down
15 changes: 7 additions & 8 deletions test/sw_mgmt_spec.rb
Expand Up @@ -55,23 +55,22 @@

context "when the libzypp lock can be obtained" do
let(:connected) { true }
let(:source_restore_result) { true }

it "initializes package management and returns Pkg.:SourceRestore result" do
it "initializes package management" do
expect(Yast::PackageCallbacks).to receive(:InitPackageCallbacks)
expect(Yast::Pkg).to receive(:TargetInitialize)
expect(Yast::Pkg).to receive(:TargetLoad)
expect(Yast::Pkg).to receive(:SourceRestore).and_return(source_restore_result)
expect(Yast::Pkg).to receive(:TargetInitialize).and_return(true)
expect(Yast::Pkg).to receive(:TargetLoad).and_return(true)
expect(Yast::Pkg).to receive(:SourceRestore).and_return(true)

expect(subject.init).to eq(source_restore_result)
subject.init
end
end

context "when the libzypp lock cannot be obtained" do
let(:connected) { false }

it "returns false" do
expect(subject.init).to eq(false)
it "raises an PkgError exception" do
expect { subject.init }.to raise_error(Registration::PkgError)
end
end
end
Expand Down

0 comments on commit a4742db

Please sign in to comment.