Skip to content

Commit

Permalink
Handle generic exceptions and re-word MiqException::Error message
Browse files Browse the repository at this point in the history
  • Loading branch information
eclarizio committed Nov 9, 2016
1 parent b44839b commit bd8353c
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
4 changes: 2 additions & 2 deletions app/controllers/miq_ae_tools_controller.rb
Expand Up @@ -131,8 +131,8 @@ def import_via_git
git_based_domain_import_service.import(params[:git_repo_id], params[:git_branch_or_tag], current_tenant.id)

add_flash(_("Imported from git"), :info)
rescue MiqException::Error
add_flash(_("The selected branch or tag does not have valid domain object data"), :error)
rescue => error
add_flash(_("Error: import failed: %{message}") % {:message => error.message}, :error)
end

respond_to do |format|
Expand Down
3 changes: 2 additions & 1 deletion app/services/git_based_domain_import_service.rb
Expand Up @@ -77,7 +77,8 @@ def import(git_repo_id, branch_or_tag, tenant_id)
task = MiqTask.wait_for_taskid(task_id)

domain = task.task_results
raise MiqException::Error, "Domain object not available from task results" unless domain.kind_of?(MiqAeDomain)
error_message = _("Selected branch or tag does not contain a valid domain")
raise MiqException::Error, error_message unless domain.kind_of?(MiqAeDomain)
domain.update_attribute(:enabled, true)
end

Expand Down
4 changes: 2 additions & 2 deletions spec/controllers/miq_ae_tools_controller_spec.rb
Expand Up @@ -484,14 +484,14 @@
before do
tenant_id = controller.current_tenant.id
allow(git_based_domain_import_service).to receive(:import).with("123", "branch_or_tag", tenant_id).and_raise(
MiqException::Error
MiqException::Error, "kaboom"
)
end

it "responds with an error message" do
post :import_via_git, :params => params, :xhr => true
expect(response.body).to eq(
[{:message => "The selected branch or tag does not have valid domain object data", :level => :error}].to_json
[{:message => "Error: import failed: kaboom", :level => :error}].to_json
)
end
end
Expand Down
6 changes: 4 additions & 2 deletions spec/services/git_based_domain_import_service_spec.rb
Expand Up @@ -87,11 +87,13 @@
context "when import fails and the task result is nil" do
let(:git_branches) { [double("GitBranch", :name => ref_name)] }

it "raises exception" do
it "raises an exception with a message" do
allow(task).to receive(:task_results).and_return(nil)
expect(MiqTask).to receive(:generic_action_with_callback).with(task_options, queue_options).and_return(task.id)

expect { subject.import(git_repo.id, ref_name, 321) }.to raise_exception(MiqException::Error)
expect { subject.import(git_repo.id, ref_name, 321) }.to raise_exception(
MiqException::Error, "Selected branch or tag does not contain a valid domain"
)
end
end
end
Expand Down

0 comments on commit bd8353c

Please sign in to comment.