Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix obscure errors by introducing FULL_TEXT_ERRORS #1695

Merged
merged 7 commits into from
Feb 10, 2025

Conversation

Romex91
Copy link
Contributor

@Romex91 Romex91 commented Feb 5, 2025

Summary

Troubleshooting 500 errors is challenging as important parts of the error messages are hidden.

This PR adds the following message to truncated error texts:

The rest of the backtrace is hidden. To see the full backtrace, set FULL_TEXT_ERRORS=true

Pull Request checklist

  • Add/update test to cover these changes
    - [ ] Update documentation - No need, the new feature is self-documented
  • Update CHANGELOG file

This change is Reviewable

Summary by CodeRabbit

  • New Features

    • Introduced an option to display complete error details, allowing users to view full diagnostic information when needed.
    • Added a new feature to control error message verbosity based on the FULL_TEXT_ERRORS environment variable.
  • Bug Fixes

    • Enhanced the clarity of error messages with improved formatting and a more intuitive backtrace summary to resolve issues with previously obscure outputs.
    • Updated the changelog to reflect the new error handling features.

Copy link
Contributor

coderabbitai bot commented Feb 5, 2025

Walkthrough

The changes introduce a new environment-variable–controlled feature, FULL_TEXT_ERRORS, to improve error reporting. The updates modify error message formatting and backtrace logging in the error handling class, adjust string trimming behavior in the utility module, and update tests to cover both scenarios when the variable is enabled or not. The changelog is updated with a new "Fixed" section to document this feature addition, and a new dependency on the Rainbow gem is introduced to enhance message coloring.

Changes

File(s) Change Summary
CHANGELOG.md Added a new "Fixed" section under "Unreleased" with an entry for the FULL_TEXT_ERRORS feature (linked to PR 1695, contributor Romex91).
lib/react_on_rails/prerender_error.rb
lib/react_on_rails/utils.rb
Updated error handling and output formatting: added dependency on the Rainbow gem for colored messages, modified error backtrace logging in PrerenderError to show either full or truncated backtrace based on FULL_TEXT_ERRORS.
Updated the TRUNCATION_FILLER constant and modified the smart_trim method to bypass truncation when FULL_TEXT_ERRORS is true.
spec/react_on_rails/prerender_error_spec.rb
spec/react_on_rails/utils_spec.rb
Added and refactored tests to verify both behaviors of error message formatting and string trimming with FULL_TEXT_ERRORS set to true versus not set.

Sequence Diagram(s)

sequenceDiagram
    participant App as Application
    participant PR as PrerenderError
    participant Utl as Utils
    participant Log as Logger

    App->>PR: An error occurs during prerendering
    PR->>PR: Format error with Rainbow styling
    PR->>PR: Check FULL_TEXT_ERRORS env variable
    alt FULL_TEXT_ERRORS = true
        PR->>PR: Use full error backtrace
    else
        PR->>PR: Trim backtrace to first 15 lines with hidden notice
    end
    PR->>Log: Log the formatted error message

    App->>Utl: Call smart_trim(s, length)
    Utl->>Utl: Check FULL_TEXT_ERRORS env variable
    alt FULL_TEXT_ERRORS = true
        Utl->>App: Return original string
    else
        Utl->>App: Return trimmed string
    end
Loading

Possibly related PRs

Suggested reviewers

  • justin808
  • Judahmeek

Poem

(_/) I hop through code with glee,
Errors now shine in full clarity.
Rainbow hues and trimmed display,
Toggle FULL_TEXT_ERRORS and brighten the day.
With each fix, my heart leaps free —
Happy coding from this rabbit, that's me! 🐰


📜 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 3148f41 and b90f1a2.

📒 Files selected for processing (1)
  • spec/react_on_rails/prender_error_spec.rb (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • spec/react_on_rails/prender_error_spec.rb
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: rspec-package-tests (newest)
  • GitHub Check: build-dummy-app-webpack-test-bundles (oldest)
  • GitHub Check: rspec-package-tests (oldest)
  • GitHub Check: build

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

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)
lib/react_on_rails/prerender_error.rb (1)

60-66: Consider enhancing the error message for better debugging.

While the conditional logic is good, consider adding more context to help users debug issues faster:

  1. Include the environment (development/production)
  2. Add timestamp for error occurrence
 backtrace = if ENV["FULL_TEXT_ERRORS"] == "true"
               err.backtrace.join("\n")
             else
               "#{err.backtrace.first(15).join("\n")}\n" +
-                Rainbow("The rest of the backtrace is hidden. " \
-                         "To see the full backtrace, set FULL_TEXT_ERRORS=true.").red
+                Rainbow("[#{Time.now}][#{Rails.env}] The rest of the backtrace is hidden. " \
+                         "To see the full backtrace, set FULL_TEXT_ERRORS=true.").red
             end
lib/react_on_rails/utils.rb (1)

11-13: Consider extracting the environment variable check to a method.

The FULL_TEXT_ERRORS check is repeated. Consider extracting it to a method for better maintainability:

 module ReactOnRails
   module Utils
+    def self.full_text_errors?
+      ENV["FULL_TEXT_ERRORS"] == "true"
+    end
+
     TRUNCATION_FILLER = "\n... TRUNCATED #{
       Rainbow('To see the full output, set FULL_TEXT_ERRORS=true.').red
     } ...\n".freeze

     def self.smart_trim(str, max_length = 1000)
       str = str.to_s
-      return str if ENV["FULL_TEXT_ERRORS"] == "true"
+      return str if full_text_errors?

Also applies to: 191-191

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 90033e7 and cf81d07.

📒 Files selected for processing (5)
  • CHANGELOG.md (2 hunks)
  • lib/react_on_rails/prerender_error.rb (2 hunks)
  • lib/react_on_rails/utils.rb (2 hunks)
  • spec/react_on_rails/prender_error_spec.rb (1 hunks)
  • spec/react_on_rails/utils_spec.rb (1 hunks)
🧰 Additional context used
🪛 markdownlint-cli2 (0.17.2)
CHANGELOG.md

22-22: Unordered list indentation
Expected: 0; Actual: 2

(MD007, ul-indent)

⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: dummy-app-integration-tests (newest)
  • GitHub Check: dummy-app-integration-tests (oldest)
  • GitHub Check: rspec-package-tests (newest)
  • GitHub Check: build
  • GitHub Check: rspec-package-tests (oldest)
🔇 Additional comments (4)
spec/react_on_rails/prender_error_spec.rb (1)

49-72: Well-structured test coverage for the new FULL_TEXT_ERRORS feature!

The test suite effectively covers both scenarios (enabled/disabled) with proper environment variable management.

lib/react_on_rails/prerender_error.rb (1)

3-3: LGTM! Good choice using Rainbow for colored output.

The Rainbow gem is a good choice for improving error message visibility.

spec/react_on_rails/utils_spec.rb (1)

315-354: Excellent test coverage for smart_trim!

The test suite thoroughly covers:

  • Both FULL_TEXT_ERRORS scenarios
  • Edge cases with different string lengths
  • Hash handling
  • Environment variable cleanup
CHANGELOG.md (1)

21-23: Document FULL_TEXT_ERRORS Feature in Changelog

The changelog entry clearly records the fix for obscure errors by introducing the FULL_TEXT_ERRORS feature, and it correctly references PR 1695 by Romex91. Please verify that this entry is placed in the appropriate category (here, under "Fixed") according to the project's changelog conventions.

Additionally, the bullet list item is indented by two spaces. According to markdown style guidelines (MD007), unordered list markers are expected to have no leading indentation. Consider removing these extra spaces to ensure consistency with the project's markdown style guidelines.

[approved, nitpick]

🧰 Tools
🪛 markdownlint-cli2 (0.17.2)

22-22: Unordered list indentation
Expected: 0; Actual: 2

(MD007, ul-indent)

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

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 (1)
CHANGELOG.md (1)

21-23: Markdown Lint: Adjust Unordered List Indentation
The markdown linter reports that the bullet list item is indented with 2 spaces (MD007) whereas the convention in this file is to have no indentation. To align with the expected style, consider removing the extra leading spaces from the bullet. For example, change:

-  - Fix obscure errors by introducing FULL_TEXT_ERRORS [PR 1695](https://github.com/shakacode/react_on_rails/pull/1695) by [Romex91](https://github.com/Romex91).
+ - Fix obscure errors by introducing FULL_TEXT_ERRORS [PR 1695](https://github.com/shakacode/react_on_rails/pull/1695) by [Romex91](https://github.com/Romex91).
🧰 Tools
🪛 markdownlint-cli2 (0.17.2)

22-22: Unordered list indentation
Expected: 0; Actual: 2

(MD007, ul-indent)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cf81d07 and 5a50062.

📒 Files selected for processing (1)
  • CHANGELOG.md (1 hunks)
🧰 Additional context used
🪛 markdownlint-cli2 (0.17.2)
CHANGELOG.md

22-22: Unordered list indentation
Expected: 0; Actual: 2

(MD007, ul-indent)

⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: build-dummy-app-webpack-test-bundles (newest)
  • GitHub Check: rspec-package-tests (newest)
  • GitHub Check: build
  • GitHub Check: build-dummy-app-webpack-test-bundles (oldest)
  • GitHub Check: rspec-package-tests (oldest)
🔇 Additional comments (1)
CHANGELOG.md (1)

21-23: New Changelog Entry for FULL_TEXT_ERRORS
The new entry clearly documents the fix for obscure errors by introducing the FULL_TEXT_ERRORS feature (as described in PR 1695 by Romex91). The entry is concise and consistent with the previous changelog entries.

🧰 Tools
🪛 markdownlint-cli2 (0.17.2)

22-22: Unordered list indentation
Expected: 0; Actual: 2

(MD007, ul-indent)

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

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 (3)
lib/react_on_rails/prerender_error.rb (2)

56-56: Consider using err.message instead of err.inspect.

Using err.inspect might expose internal error details that aren't relevant to users. Consider using err.message for a cleaner error message.

-          #{err.inspect}
+          #{err.message}

60-66: LGTM: Clear backtrace truncation with user guidance.

The implementation effectively handles backtrace truncation and provides clear instructions for viewing the full backtrace.

However, consider extracting the magic number 15 to a constant for better maintainability.

+    BACKTRACE_LINES_TO_SHOW = 15
+
     def calc_message(component_name, console_messages, err, js_code, props)
       # ...
-                      "#{err.backtrace.first(15).join("\n")}\n" +
+                      "#{err.backtrace.first(BACKTRACE_LINES_TO_SHOW).join("\n")}\n" +
lib/react_on_rails/utils.rb (1)

188-190: Consider caching the environment variable check.

The full_text_errors_enabled? method is called frequently but reads from ENV each time. Consider caching the result.

     def self.full_text_errors_enabled?
-      ENV["FULL_TEXT_ERRORS"] == "true"
+      return @full_text_errors if defined?(@full_text_errors)
+      @full_text_errors = ENV["FULL_TEXT_ERRORS"] == "true"
     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 5a50062 and a59fbd1.

📒 Files selected for processing (2)
  • lib/react_on_rails/prerender_error.rb (2 hunks)
  • lib/react_on_rails/utils.rb (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: rspec-package-tests (newest)
  • GitHub Check: rspec-package-tests (oldest)
  • GitHub Check: build
🔇 Additional comments (3)
lib/react_on_rails/prerender_error.rb (1)

3-3: LGTM: Rainbow gem dependency added for colored output.

The Rainbow gem is appropriately required for enhancing error message visibility with color.

lib/react_on_rails/utils.rb (2)

11-13: LGTM: Clear truncation message with color.

The truncation message effectively communicates how to see the full output.


195-195: LGTM: Early return for full text output.

The early return when full text errors are enabled is clean and efficient.

@Romex91 Romex91 requested a review from alexeyr-ci February 7, 2025 07:31
backtrace = if Utils.full_text_errors_enabled?
err.backtrace.join("\n")
else
"#{err.backtrace.first(15).join("\n")}\n" +
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do we need this at all? And why first 15 instead of the normal Rails backtrace cleaning?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you suggest removing the ENV variable and using Rails.backtrace_cleaner?

Copy link
Collaborator

Choose a reason for hiding this comment

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

We can leave the variable and only use Rails.backtrace_cleaner in the else case.

Copy link
Collaborator

@alexeyr-ci alexeyr-ci Feb 7, 2025

Choose a reason for hiding this comment

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

The alternative is just to always show the full stack trace, but I don't know why we'd ever want first N in particular unless N is 1 or 2.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@Romex91 Romex91 requested a review from alexeyr-ci February 10, 2025 07:33
@alexeyr-ci alexeyr-ci merged commit 923525d into master Feb 10, 2025
11 checks passed
@alexeyr-ci alexeyr-ci deleted the roman-kuksin/full-text-errors branch February 10, 2025 07:50
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.

2 participants