Skip to content

Improved error handeling - #1094

Merged
lodewiges merged 8 commits into
stagingfrom
error-handeling-bug
Oct 26, 2025
Merged

Improved error handeling#1094
lodewiges merged 8 commits into
stagingfrom
error-handeling-bug

Conversation

@lodewiges

@lodewiges lodewiges commented Oct 19, 2025

Copy link
Copy Markdown
Contributor

The staging env was targeted with a wordpress penetration attack and now ended up in a bootlood due to bad error handeling

Summary by CodeRabbit

  • New Features

    • Added a dynamic 422 (Unprocessable Entity) error page with user-facing guidance.
  • Bug Fixes

    • Preserved correct HTTP status codes for error responses and improved fallback plain-text responses when error rendering fails.
    • Added global handling to better surface and log rendering failures.
  • Refactor

    • Removed static public error pages in favor of template-based error rendering with a consistent error layout and updated error-to-handler routing.

@coderabbitai

coderabbitai Bot commented Oct 19, 2025

Copy link
Copy Markdown

Walkthrough

ErrorsController was updated to use a shared layout 'errors', add rescue_from StandardError and a render_error_page helper, and include a new unprocessable_entity action. Four static public error pages were removed and a new app/views/errors/unprocessable_entity.html.erb was added. Routes for 406 and 422 were adjusted.

Changes

Cohort / File(s) Summary
ErrorsController changes
app/controllers/errors_controller.rb
Added layout 'errors', rescue_from StandardError, with: :handle_rendering_error, introduced unprocessable_entity action, consolidated rendering into private render_error_page(template, status, fallback_message), and updated actions (forbidden, not_found, unacceptable, internal_server_error) to use that helper with fallback logging and plain-text fallback.
Removed static error pages
public/404.html, public/406-unsupported-browser.html, public/422.html, public/500.html
Deleted four static HTML error pages; error responses are now served via controller-rendered templates.
New view template
app/views/errors/unprocessable_entity.html.erb
Added ERB template for 422 (unprocessable_entity) with Dutch copy and a mailto link to Rails.application.config.x.admin_email.
Routes updated
config/routes.rb
Changed routing: added get '/406', to: 'errors#unacceptable' and replaced previous get '/422', to: 'errors#unacceptable' with get '/422', to: 'errors#unprocessable_entity'.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Client
  participant Router
  participant ErrorsController
  participant ViewTemplate

  Client->>Router: request to error route (/404,/406,/422,/500)
  Router->>ErrorsController: dispatch to specific error action
  ErrorsController->>ViewTemplate: render 'errors/<action>' with layout 'errors' and status
  ViewTemplate-->>ErrorsController: rendered HTML
  ErrorsController-->>Client: HTTP response (error page)
  Note over ErrorsController: render_error_page rescues StandardError\nlogs error and renders plain-text fallback
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 I hopped from static files to views anew,
Errors dressed in one layout, tidy and true,
A Dutch 422 joins the crew,
If rendering trips, a plain note will do,
I nibble logs and bound anew. 🥕

Pre-merge checks and finishing touches

❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Description Check ⚠️ Warning The pull request description is significantly incomplete and does not follow the required template. The provided description consists only of a single sentence explaining the background context (the WordPress penetration attack and bootloop) but lacks the structured sections required by the template: the checklist section is entirely missing, the Summary section should describe what changes were made to fix the error handling and should include any issue references or UI changes, and the Other information section is absent. Without these key sections, reviewers cannot fully understand what the PR accomplishes or verify that database migrations were handled correctly.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (1 passed)
Check name Status Explanation
Title Check ✅ Passed The pull request title "Improved error handeling" is directly related to the main objectives of the changeset. The PR refactors error handling in the ErrorsController by introducing a rescue_from handler, adding a common error-rendering helper method, replacing static HTML error pages with dynamic ERB templates, and fixing error routing. The title accurately captures the primary intent of improving error handling infrastructure, though it uses somewhat general language rather than specific details about the refactoring approach. The title is clear and conveys meaningful information without being vague or generic.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch error-handeling-bug

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between dbd7e62 and f58f7e1.

📒 Files selected for processing (2)
  • app/controllers/errors_controller.rb (1 hunks)
  • app/views/errors/unprocessable_entity.html.erb (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • app/views/errors/unprocessable_entity.html.erb
  • app/controllers/errors_controller.rb

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov

codecov Bot commented Oct 19, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 16 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.56%. Comparing base (b20bf72) to head (f58f7e1).
⚠️ Report is 5 commits behind head on staging.

Files with missing lines Patch % Lines
app/controllers/errors_controller.rb 0.00% 16 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           staging    #1094      +/-   ##
===========================================
- Coverage    75.15%   74.56%   -0.60%     
===========================================
  Files           53       51       -2     
  Lines         1103     1093      -10     
===========================================
- Hits           829      815      -14     
- Misses         274      278       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 3

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 439db22 and dce8de7.

📒 Files selected for processing (5)
  • app/controllers/errors_controller.rb (1 hunks)
  • public/404.html (0 hunks)
  • public/406-unsupported-browser.html (0 hunks)
  • public/422.html (0 hunks)
  • public/500.html (0 hunks)
💤 Files with no reviewable changes (4)
  • public/500.html
  • public/404.html
  • public/406-unsupported-browser.html
  • public/422.html
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: Test
  • GitHub Check: Lint
🔇 Additional comments (1)
app/controllers/errors_controller.rb (1)

5-17: All error view templates are present and correct.

The verification confirms that all four templates referenced in app/controllers/errors_controller.rb exist at app/views/errors/:

  • forbidden.html.erb
  • not_found.html.erb
  • unacceptable.html.erb
  • internal_server_error.html.erb

The code is ready for deployment.

Comment thread app/controllers/errors_controller.rb Outdated
Comment thread app/controllers/errors_controller.rb
Comment thread app/controllers/errors_controller.rb

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
app/views/errors/unprocessable_entity.html.erb (1)

1-2: Consider a more user-friendly heading.

The heading "unprocessable_entity (422)" is very technical. Since the rest of the page uses friendly Dutch text, consider using a more user-friendly heading like "Verzoek niet verwerkt" or "Ongeldige gegevens" while keeping the error code for debugging purposes.

Apply this diff for a friendlier heading:

-<% content_for :title, "unprocessable_entity (422) - #{Rails.application.config.x.site_name}" %>
-<h1>unprocessable_entity (422)</h1>
+<% content_for :title, "Verzoek niet verwerkt (422) - #{Rails.application.config.x.site_name}" %>
+<h1>Verzoek niet verwerkt <small>(422)</small></h1>
app/controllers/errors_controller.rb (1)

4-37: Consider extracting rescue logic to reduce duplication.

All five error actions share nearly identical rescue blocks, differing only in the error message and status code. This duplication could be reduced with a helper method or by using a more generic rescue approach.

Consider extracting the common pattern:

class ErrorsController < ApplicationController
  layout 'errors'

  rescue_from StandardError, with: :handle_rendering_error

  def forbidden
    render_error_page('errors/forbidden', :forbidden, '403 Forbidden')
  end

  def not_found
    render_error_page('errors/not_found', :not_found, '404 Not Found')
  end

  def unacceptable
    render_error_page('errors/unacceptable', :not_acceptable, '406 Not Acceptable')
  end

  def unprocessable_entity
    render_error_page('errors/unprocessable_entity', :unprocessable_entity, '422 Unprocessable Entity')
  end

  def internal_server_error
    render_error_page('errors/internal_server_error', :internal_server_error, '500 Internal Server Error')
  end

  private

  def render_error_page(template, status, fallback_message)
    render template: template, status: status
  rescue StandardError => e
    Rails.logger.error("Error rendering #{status} page: #{e.message}")
    render plain: fallback_message, status: status
  end
end
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 708d6d5 and dbd7e62.

📒 Files selected for processing (3)
  • app/controllers/errors_controller.rb (1 hunks)
  • app/views/errors/unprocessable_entity.html.erb (1 hunks)
  • config/routes.rb (1 hunks)
🔇 Additional comments (4)
app/views/errors/unprocessable_entity.html.erb (1)

3-4: LGTM - Clear user guidance.

The Dutch error message and contact information are clear and helpful. The mailto link with admin email configuration is implemented correctly.

config/routes.rb (1)

68-69: LGTM - Error routes correctly mapped.

The routes now correctly map 406 to the unacceptable action and 422 to the new unprocessable_entity action. This separation ensures proper HTTP status codes are returned for each error type.

app/controllers/errors_controller.rb (2)

2-2: LGTM - Error-specific layout prevents cascading failures.

Using the 'errors' layout instead of 'application' is the correct approach to prevent the bootloop issue mentioned in the PR description. This ensures error pages can render even when the application is in a degraded state.


25-30: LGTM - New unprocessable_entity action follows established pattern.

The new action correctly renders the 422 template with appropriate error handling and fallback, consistent with the other error actions.

@lodewiges
lodewiges enabled auto-merge October 20, 2025 07:10
@lodewiges
lodewiges disabled auto-merge October 20, 2025 07:11
@lodewiges
lodewiges enabled auto-merge October 20, 2025 07:11
@lodewiges
lodewiges disabled auto-merge October 26, 2025 17:53
@lodewiges
lodewiges merged commit c5fe871 into staging Oct 26, 2025
4 of 6 checks passed
@lodewiges
lodewiges deleted the error-handeling-bug branch October 26, 2025 17:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant