Skip to content

Return a 404 if a Scratch project doesn't exist#749

Merged
zetter-rpf merged 1 commit intomainfrom
fix-scratch-project-load-errors
Mar 27, 2026
Merged

Return a 404 if a Scratch project doesn't exist#749
zetter-rpf merged 1 commit intomainfrom
fix-scratch-project-load-errors

Conversation

@zetter-rpf
Copy link
Copy Markdown
Contributor

@zetter-rpf zetter-rpf commented Mar 26, 2026

Status

What's changed?

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.

@cla-bot cla-bot bot added the cla-signed label Mar 26, 2026
@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 26, 2026

Test coverage

89.69% line coverage reported by SimpleCov.
Run: https://github.com/RaspberryPiFoundation/editor-api/actions/runs/23603517947

@zetter-rpf zetter-rpf force-pushed the fix-scratch-project-load-errors branch from 4acbcea to 04610cf Compare March 26, 2026 15:39
@zetter-rpf zetter-rpf marked this pull request as ready for review March 26, 2026 15:40
Copilot AI review requested due to automatic review settings March 26, 2026 15:40
Fixes RaspberryPiFoundation/digital-editor-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.
@zetter-rpf zetter-rpf force-pushed the fix-scratch-project-load-errors branch from 04610cf to bbaa216 Compare March 26, 2026 15:42
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the Scratch API project-loading behavior so missing or non-Scratch projects raise ActiveRecord::RecordNotFound (and therefore return HTTP 404 via ApiController), instead of triggering 500s when accessing Scratch-specific associations on nil/wrong-type projects.

Changes:

  • Add request specs asserting 404 for missing projects and for projects that aren’t Scratch projects.
  • Replace ProjectLoader usage in Scratch API controllers with Project.find_by! scoped to Scratch project type.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
spec/features/scratch/showing_a_scratch_project_spec.rb Adds coverage for 404 responses when a project is missing or not Scratch.
app/controllers/api/scratch/scratch_controller.rb Loads Scratch projects via find_by! to ensure missing/wrong-type projects yield 404.
app/controllers/api/scratch/projects_controller.rb Loads original remix project via find_by! to ensure missing/wrong-type originals yield 404.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Contributor

@abcampo-iry abcampo-iry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the fix

@zetter-rpf zetter-rpf merged commit 0764508 into main Mar 27, 2026
6 checks passed
@zetter-rpf zetter-rpf deleted the fix-scratch-project-load-errors branch March 27, 2026 08:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants