From bbaa216f9605e9474c3ec702c2a284bd0d055381 Mon Sep 17 00:00:00 2001 From: Chris Zetter <253059100+zetter-rpf@users.noreply.github.com> Date: Thu, 26 Mar 2026 15:42:19 +0000 Subject: [PATCH] Return a 404 if a Scratch project doesn't exist Fixes https://github.com/RaspberryPiFoundation/digital-editor-issues/issues/1269 Previously a 500 was raised if a scratch project wasn't found or we tried to load a project that was of another type. This is because these project don't have the expected ScratchComponents. Instead use the Rails Finders for loading, which will raise as expected if a record isn't found. I think this simplifies this code and removes the misleading locale lookup - Scratch API requests don't provide a locale so it's confusing to make them look like they do. --- app/controllers/api/scratch/projects_controller.rb | 8 +------- app/controllers/api/scratch/scratch_controller.rb | 3 +-- .../scratch/showing_a_scratch_project_spec.rb | 14 ++++++++++++++ 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/app/controllers/api/scratch/projects_controller.rb b/app/controllers/api/scratch/projects_controller.rb index 772056ee1..4d90722ff 100644 --- a/app/controllers/api/scratch/projects_controller.rb +++ b/app/controllers/api/scratch/projects_controller.rb @@ -62,13 +62,7 @@ def create_params end def load_original_project(identifier) - project_loader = ProjectLoader.new(identifier, [params[:locale]]) - original_project = project_loader.load - - raise ActiveRecord::RecordNotFound, I18n.t('errors.project.not_found') unless original_project - raise ActiveRecord::RecordNotFound, I18n.t('errors.project.not_found') unless original_project.scratch_project? - - original_project + Project.find_by!(identifier:, project_type: Project::Types::CODE_EDITOR_SCRATCH) end def scratch_content_params diff --git a/app/controllers/api/scratch/scratch_controller.rb b/app/controllers/api/scratch/scratch_controller.rb index c1b792141..9b1d1b187 100644 --- a/app/controllers/api/scratch/scratch_controller.rb +++ b/app/controllers/api/scratch/scratch_controller.rb @@ -18,8 +18,7 @@ def check_scratch_feature end def load_project - project_loader = ProjectLoader.new(params[:id], [params[:locale]]) - @project = project_loader.load + @project = Project.find_by!(identifier: params[:id], project_type: Project::Types::CODE_EDITOR_SCRATCH) end end end diff --git a/spec/features/scratch/showing_a_scratch_project_spec.rb b/spec/features/scratch/showing_a_scratch_project_spec.rb index 40bd25c3b..dcd760ca6 100644 --- a/spec/features/scratch/showing_a_scratch_project_spec.rb +++ b/spec/features/scratch/showing_a_scratch_project_spec.rb @@ -18,4 +18,18 @@ data = JSON.parse(response.body, symbolize_names: true) expect(data).to have_key(:targets) end + + it 'returns a 404 if project does not exist' do + get '/api/scratch/projects/non_existent_project' + + expect(response).to have_http_status(:not_found) + end + + it 'returns a 404 if project is not a scratch project' do + project = create(:project, project_type: Project::Types::PYTHON, locale: 'en') + + get "/api/scratch/projects/#{project.identifier}" + + expect(response).to have_http_status(:not_found) + end end