Skip to content

Commit

Permalink
[api][webui] Catch backend errors when storing download repositories
Browse files Browse the repository at this point in the history
This implements exception handling for storing download repository data in
the backend. This is needed to avoid inconsistency between frontend DB and
backend.
  • Loading branch information
bgeuken committed Apr 12, 2016
1 parent 3a5a0e1 commit ee58bcf
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
47 changes: 32 additions & 15 deletions src/api/app/controllers/webui/download_on_demand_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,51 @@ class Webui::DownloadOnDemandController < Webui::WebuiController
def create
@download_on_demand = DownloadRepository.new(permitted_params)
authorize @download_on_demand
if @download_on_demand.save
@project.store
redirect_to project_repositories_path(@project), notice: "Successfully created Download on Demand"
else
redirect_to :back, error: "Download on Demand can't be created: #{@download_on_demand.errors.full_messages.to_sentence}"

begin
ActiveRecord::Base.transaction do
@download_on_demand.save!
@project.store
end
rescue ActiveRecord::RecordInvalid, ActiveXML::Transport::Error => exception
redirect_to :back, error: "Download on Demand can't be created: #{exception.message}"
return
end

redirect_to project_repositories_path(@project), notice: "Successfully created Download on Demand"
end

def update
@download_on_demand = DownloadRepository.find(params[:id])
authorize @download_on_demand
if @download_on_demand.update_attributes(permitted_params)
@project.store
redirect_to project_repositories_path(@project), notice: "Successfully updated Download on Demand"
else
redirect_to :back, error: "Download on Demand can't be updated: #{@download_on_demand.errors.full_messages.to_sentence}"

begin
ActiveRecord::Base.transaction do
@download_on_demand.update_attributes!(permitted_params)
@project.store
end
rescue ActiveRecord::RecordInvalid, ActiveXML::Transport::Error => exception
redirect_to :back, error: "Download on Demand can't be updated: #{exception.message}"
return
end

redirect_to project_repositories_path(@project), notice: "Successfully updated Download on Demand"
end

def destroy
@download_on_demand = DownloadRepository.find(params[:id])
authorize @download_on_demand
if @download_on_demand.destroy
@project.store
redirect_to project_repositories_path(@project), notice: "Successfully removed Download on Demand"
else
redirect_to :back, error: "Download on Demand can't be removed: #{@download_on_demand.errors.full_messages.to_sentence}"

begin
ActiveRecord::Base.transaction do
@download_on_demand.destroy!
@project.store
end
rescue ActiveRecord::RecordInvalid, ActiveXML::Transport::Error => exception
redirect_to :back, error: "Download on Demand can't be removed: #{exception.message}"
end

redirect_to project_repositories_path(@project), notice: "Successfully removed Download on Demand"
end

private
Expand Down
1 change: 1 addition & 0 deletions src/api/app/models/download_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ class DownloadRepository < ActiveRecord::Base
validates :repotype, inclusion: { in: REPOTYPES }

delegate :to_s, to: :id

end
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
end

it { is_expected.to redirect_to(root_path) }
it { expect(flash[:error]).to eq("Download on Demand can't be created: Arch can't be blank and Arch is not included in the list") }
it { expect(flash[:error]).to eq("Download on Demand can't be created: Validation failed: Arch can't be blank, Arch is not included in the list") }
it { expect(assigns(:download_on_demand)).to be_kind_of(DownloadRepository) }
it { expect(DownloadRepository.where(dod_parameters[:download_repository])).not_to exist }
end
Expand Down

0 comments on commit ee58bcf

Please sign in to comment.