Handle Task::NotFoundError with a 404 page instead of raising an error#1440
Handle Task::NotFoundError with a 404 page instead of raising an error#1440kaibadash wants to merge 1 commit intoShopify:mainfrom
Conversation
This shouldn't be the case, since we declare maintenance_tasks/lib/maintenance_tasks/engine.rb Lines 36 to 38 in 4b8ef60 $ bin/production &
$ curl -s -D - http://127.0.0.1:3000/maintenance_tasks/tasks/DoesNotExist | grep 404
HTTP/1.1 404 Not Found
<title>The page you were looking for doesn't exist (404)</title>
<!-- This file lives in public/404.html -->Maybe Sentry's code does still mark those as unhandled exceptions, but then it's a bug/feature in their code. |
etiennebarrie
left a comment
There was a problem hiding this comment.
I think this could be nice.
At first I thought that shouldn't be necessary (easy enough to tweak the URL), and the Sentry issue is a problem with that gem or the configuration (although it shouldn't be necessary to add this exception class to list of excluded exceptions, doing so would solve that issue).
But the change is small, the page looks nicer (and also the main app's 404 page might be super custom) and moving away from action_dispatch.rescue_responses means better support for exception monitoring tools which don't respect action_dispatch.report_exception.
| test "show a not found Task" do | ||
| visit maintenance_tasks_path + "/tasks/Maintenance::DoesNotExist" | ||
|
|
||
| assert_title "Not Found" |
There was a problem hiding this comment.
I wish we could explicitly test the 404 but we can't use page.status_code 😞
However the 404 causes an issue with
since the browser reports navigating to the page as an error. We can work around this with| assert_title "Not Found" | |
| assert_title "Not Found" | |
| assert_includes page.driver.browser.logs.get(:browser).sole.message, "404" |
rescue_from Task::NotFoundErrorinApplicationControllerto render a friendly 404 page when users access a task that no longer existsnot_found.html.erb) styled with Bulma to match the existing UI, with a link back to the tasks listrescue_responsesmapping fromEnginesince the error is now handled directly by the controllerMotivation
Maintenance tasks are short-lived by nature — they are created for one-time data migrations or cleanup operations and are expected to be deleted once they are no longer needed. However, users may still have bookmarked URLs or links in Slack/docs pointing to deleted tasks.
Previously, accessing a deleted task with no associated runs raised
MaintenanceTasks::Task::NotFoundError, which:By using
rescue_fromin the controller, the exception is caught before it propagates to error monitoring middleware, and users see a clean 404 page with navigation back to the task list.