Skip to content

Commit

Permalink
Handle Project/Package not found more gracefully
Browse files Browse the repository at this point in the history
Instead of sending people to the 404 page, redirect and render a flash message.
  • Loading branch information
hennevogel committed Sep 27, 2023
1 parent e560814 commit 8d66433
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
7 changes: 4 additions & 3 deletions src/api/app/controllers/concerns/webui/rescue_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ module Webui::RescueHandler
end
end

rescue_from Package::Errors::ScmsyncReadOnly do |exception|
rescue_from Project::Errors::UnknownObjectError, Package::Errors::UnknownObjectError, Package::Errors::ReadSourceAccessError, Package::Errors::ScmsyncReadOnly do |exception|
message = exception.message || exception.default_message
if request.xhr?
render json: { error: exception.default_message }, status: exception.status
head :not_found
else
flash[:error] = exception.default_message
flash[:error] = message
redirect_back(fallback_location: root_path)
end
end
Expand Down
5 changes: 3 additions & 2 deletions src/api/app/controllers/webui/webui_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ def handle_unverified_request

def set_project
# We've started to use project_name for new routes...
@project = ::Project.find_by(name: params[:project_name] || params[:project])
raise ActiveRecord::RecordNotFound unless @project
project_name = params[:project_name] || params[:project]
@project = ::Project.find_by(name: project_name)
raise Project::Errors::UnknownObjectError, "Project not found: #{project_name}" unless @project
end

def require_login
Expand Down
18 changes: 10 additions & 8 deletions src/api/spec/controllers/webui/webui_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,11 @@ def create

describe 'set_project before filter' do
context 'with invalid project parameter' do
it 'raises an ActiveRecord::RecordNotFound exception' do
expect do
get :edit, params: { id: 1, project: 'invalid' }
end.to raise_error(ActiveRecord::RecordNotFound)
it 'redirects back' do
from projects_path
get :edit, params: { id: 1, project: 'invalid' }
expect(flash[:error]).to eq('Project not found: invalid')
expect(response).to redirect_to projects_url
end
end

Expand All @@ -122,10 +123,11 @@ def create
let(:project) { create(:project) }

context 'with invalid package parameter' do
it 'raises an Package::Errors::UnknownObjectError exception' do
expect do
get :create, params: { project: project, package: 'invalid' }
end.to raise_error(Package::Errors::UnknownObjectError)
it 'redirects back' do
from project_show_path(project: project)
get :create, params: { project: project, package: 'invalid' }
expect(flash[:error]).to eq("Package not found: #{project.name}/invalid")
expect(response).to redirect_to project_show_url(project: project)
end
end

Expand Down

0 comments on commit 8d66433

Please sign in to comment.