Skip to content

Commit 04610cf

Browse files
committed
Return a 404 if a Scratch project doesn't exist
Fixes RaspberryPiFoundation/digital-editor-issues#1269 Previously a 500 was raised because we would try to access the scratch component of an `nil` object or if we tried to load a project that was of another type. I think we've overcomplicated this by using the ProjectFinder and loading by locale. API requests from Scratch won't have a locale set so it's confusing to make it look like we're supporting multiple locales from Scratch. Instead use the Rails Finders for loading, which will raise as expected if a record isn't found.
1 parent 38be3fb commit 04610cf

3 files changed

Lines changed: 16 additions & 9 deletions

File tree

app/controllers/api/scratch/projects_controller.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,7 @@ def create_params
6262
end
6363

6464
def load_original_project(identifier)
65-
project_loader = ProjectLoader.new(identifier, [params[:locale]])
66-
original_project = project_loader.load
67-
68-
raise ActiveRecord::RecordNotFound, I18n.t('errors.project.not_found') unless original_project
69-
raise ActiveRecord::RecordNotFound, I18n.t('errors.project.not_found') unless original_project.scratch_project?
70-
71-
original_project
65+
Project.find_by!(identifier:, project_type: Project::Types::CODE_EDITOR_SCRATCH)
7266
end
7367

7468
def scratch_content_params

app/controllers/api/scratch/scratch_controller.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ def check_scratch_feature
1818
end
1919

2020
def load_project
21-
project_loader = ProjectLoader.new(params[:id], [params[:locale]])
22-
@project = project_loader.load
21+
@project = Project.find_by!(identifier: params[:id], project_type: Project::Types::CODE_EDITOR_SCRATCH)
2322
end
2423
end
2524
end

spec/features/scratch/showing_a_scratch_project_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,18 @@
1818
data = JSON.parse(response.body, symbolize_names: true)
1919
expect(data).to have_key(:targets)
2020
end
21+
22+
it 'returns a 404 if project does not exist' do
23+
get '/api/scratch/projects/non_existent_project'
24+
25+
expect(response).to have_http_status(:not_found)
26+
end
27+
28+
it 'returns a 404 if project is not a scratch project' do
29+
project = create(:project, project_type: Project::Types::PYTHON, locale: 'en')
30+
31+
get "/api/scratch/projects/#{project.identifier}"
32+
33+
expect(response).to have_http_status(:not_found)
34+
end
2135
end

0 commit comments

Comments
 (0)