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

Emit Friendly Message When Component Docs Not Found #1032

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

codercr
Copy link
Collaborator

@codercr codercr commented Feb 6, 2025

what

  • Merging component directory error messages into one to improve user experience.

why

  • Show a user friendly error when docs are not found

references

DEV-2718

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced error messaging by streamlining checks to provide a single, clear notification when a component is missing or an issue occurs.
    • Improved clarity of error messages related to loading documentation by offering a more straightforward context.
    • Updated logging mechanisms for consistency and clarity across the command's error handling.

@codercr codercr requested a review from a team as a code owner February 6, 2025 00:31
@mergify mergify bot added the triage Needs triage label Feb 6, 2025
Copy link
Contributor

coderabbitai bot commented Feb 6, 2025

📝 Walkthrough

Walkthrough

The pull request modifies the error handling and logging mechanisms in the Run function of the docsCmd command within the cmd/docs.go file. It replaces the custom logging utility with the log package from github.com/charmbracelet/log, updating log statements for consistency. Error messages have been consolidated to provide clearer feedback for missing components and README files. The changes streamline error reporting without altering any public entity declarations.

Changes

File Change Summary
cmd/docs.go Updated logging to use github.com/charmbracelet/log, consolidated error handling for component path and README checks, and modified error messages for markdown rendering.

Possibly related PRs

Suggested labels

patch

Suggested reviewers

  • osterman
  • aknysh

📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 65c51bb and f088bc5.

📒 Files selected for processing (1)
  • cmd/docs.go (6 hunks)
🧰 Additional context used
🪛 golangci-lint (1.62.2)
cmd/docs.go

12-12: import "github.com/charmbracelet/log" imported without alias but must be with alias "log" according to config

(importas)

🪛 GitHub Check: golangci
cmd/docs.go

[failure] 12-12: [golangci] cmd/docs.go#L12
import "github.com/charmbracelet/log" imported without alias but must be with alias "log" according to config (importas)


[failure] 80-80: [golangci] cmd/docs.go#L80
error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)


[failure] 72-72: [golangci] cmd/docs.go#L72
do not define dynamic errors, use wrapped static errors instead: "fmt.Errorf("Component not found")" (err113)


[failure] 75-75: [golangci] cmd/docs.go#L75
do not define dynamic errors, use wrapped static errors instead: "fmt.Errorf("Component not found")" (err113)

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Summary
🔇 Additional comments (4)
cmd/docs.go (4)

49-49: Log statements look good.

You've properly replaced the deprecated utility warnings with the new logger. The deprecation notices inform users about configuration changes effectively.

Also applies to: 106-106


75-75: Use same static error as above.

This is a duplicate of the error handling from lines 71-73, so it should use the same static error.

-u.PrintErrorMarkdownAndExit("", fmt.Errorf("Component not found"), "")
+u.PrintErrorMarkdownAndExit("", ErrComponentNotFound, "")
🧰 Tools
🪛 GitHub Check: golangci

[failure] 75-75: [golangci] cmd/docs.go#L75
do not define dynamic errors, use wrapped static errors instead: "fmt.Errorf("Component not found")" (err113)


95-95: Consistent error handling looks good.

You've successfully standardized error handling across the file using u.PrintErrorMarkdownAndExit and proper error formatting/wrapping.

Also applies to: 100-100, 110-110, 120-120, 134-134


32-140:

❓ Verification inconclusive

Consider adding a test case.

As osterman mentioned in the PR comments, don't forget to add a test case to verify the new error handling behavior, similar to the existing test at https://github.com/cloudposse/atmos/blob/main/tests/test-cases/core.yaml#L67-L81.

The test should check for:

  1. Non-zero exit code when documentation is not found
  2. The presence of your helpful error messages in the output

🏁 Script executed:

#!/bin/bash
# Verify the test case mentioned by osterman exists
grep -A 15 "docsCommandNonExistentComponent" tests/test-cases/core.yaml

Length of output: 71


Action Required: Add and Verify Missing Test Case for Documentation Error Handling

It appears that the expected test case (e.g. using the identifier docsCommandNonExistentComponent) was not found in the test cases file. Please ensure that you add a test case which verifies that when a component's documentation is missing, the command exits with a non-zero status and produces a helpful error message. Ideally, this test should verify:

  • The command exits with a non-zero exit code when documentation is absent.
  • Your error outputs include a descriptive message (such as "Documentation is missing for the component...").

This test should mimic the behavior and structure of the existing test in tests/test-cases/core.yaml at lines 67-81.

🧰 Tools
🪛 GitHub Check: golangci

[failure] 80-80: [golangci] cmd/docs.go#L80
error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)


[failure] 72-72: [golangci] cmd/docs.go#L72
do not define dynamic errors, use wrapped static errors instead: "fmt.Errorf("Component not found")" (err113)


[failure] 75-75: [golangci] cmd/docs.go#L75
do not define dynamic errors, use wrapped static errors instead: "fmt.Errorf("Component not found")" (err113)

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 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.
  • @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 or @auto-summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @auto-title anywhere in the PR title to generate the title automatically.

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 (1)
cmd/docs.go (1)

73-79: Consider consolidating README error messages for consistency.

Since we're streamlining error messages, we could also simplify this error handling block to match the same pattern:

 if _, err := os.Stat(readmePath); err != nil {
-    if os.IsNotExist(err) {
-        u.LogErrorAndExit(fmt.Errorf("No README found for component: %s", info.Component))
-    } else {
-        u.LogErrorAndExit(fmt.Errorf("Component %s not found", info.Component))
-    }
+    u.LogErrorAndExit(fmt.Errorf("No README found for component: %s", info.Component))
 }
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7d202f6 and 6e58be8.

📒 Files selected for processing (1)
  • cmd/docs.go (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Build (windows-latest, windows)
  • GitHub Check: Summary
🔇 Additional comments (1)
cmd/docs.go (1)

68-70: LGTM! Clean consolidation of error handling.

The combined error check simplifies the control flow while maintaining clear error messaging. This aligns well with the PR's goal of providing a more consistent user experience.

coderabbitai[bot]
coderabbitai bot previously approved these changes Feb 6, 2025
@mergify mergify bot removed the triage Needs triage label Feb 6, 2025
@mergify mergify bot added the triage Needs triage label Feb 6, 2025
@osterman
Copy link
Member

osterman commented Feb 6, 2025

Please add a test case similar to this one:

https://github.com/cloudposse/atmos/blob/main/tests/test-cases/core.yaml#L67-L81

that checks for non-zero exit code, and a helpful error message.

coderabbitai[bot]
coderabbitai bot previously approved these changes Feb 6, 2025
@mergify mergify bot removed the triage Needs triage label Feb 6, 2025
cmd/docs.go Outdated
@@ -83,7 +80,7 @@ var docsCmd = &cobra.Command{

readmeContent, err := os.ReadFile(readmePath)
if err != nil {
u.LogErrorAndExit(err)
u.LogErrorAndExit(fmt.Errorf("Loading README file for component: %s", info.Component))
Copy link
Member

Choose a reason for hiding this comment

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

do we want to show the actual err here?

Copy link
Member

@osterman osterman Feb 6, 2025

Choose a reason for hiding this comment

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

@aknysh is correct, this doesn't achieve the desired outcome. We handle errors today, but it's not friendly.

Here's what we want:

  • If the component folder is not found, say "Component not found"
  • If the component lacks a README.md, say "Documentation is missing for the component asd. Consider adding a README.md to provide more context and details."

Copy link
Member

Choose a reason for hiding this comment

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

Let’s use l.Debug to emit the actual error, and print a friendly one here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That would be useful

Copy link
Member

@osterman osterman Feb 12, 2025

Choose a reason for hiding this comment

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

Also, use l.Fatal instead of u.LogErrorAndExit (deprecated after moving to new logger)

Copy link
Member

@aknysh aknysh left a comment

Choose a reason for hiding this comment

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

please see comments

@osterman osterman changed the title [DEV-2718] Merging component directory error messages into one Emit Friendly Message When Component Docs Not Found Feb 6, 2025
@osterman osterman closed this Feb 28, 2025
@codercr codercr reopened this Mar 7, 2025
Copy link

mergify bot commented Mar 7, 2025

💥 This pull request now has conflicts. Could you fix it @codercr? 🙏

@mergify mergify bot added the conflict This PR has conflicts label Mar 7, 2025
@github-actions github-actions bot added the size/s label Mar 7, 2025
@mergify mergify bot removed the conflict This PR has conflicts label Mar 7, 2025
@codercr
Copy link
Collaborator Author

codercr commented Mar 7, 2025

@osterman @aknysh - I made changes and will work on test next. Let me know if you still want messages changed.

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.

Caution

Inline review comments failed to post. This is likely due to GitHub's limits when posting large numbers of comments. If you are seeing this consistently it is likely a permissions issue. Please check "Moderation" -> "Code review limits" under your organization settings.

Actionable comments posted: 1

🧹 Nitpick comments (5)
cmd/docs.go (5)

10-10: Adjust import alias to follow project conventions.

The static analysis tool flags that the import alias "l" for charmbracelet/log should be "log" according to project config.

-	l "github.com/charmbracelet/log"
+	log "github.com/charmbracelet/log"

Remember to update all references to "l" throughout the code.

🧰 Tools
🪛 golangci-lint (1.62.2)

10-10: import "github.com/charmbracelet/log" imported as "l" but must be "log" according to config

(importas)

🪛 GitHub Check: golangci

[failure] 10-10: [golangci] cmd/docs.go#L10
import "github.com/charmbracelet/log" imported as "l" but must be "log" according to config (importas)


72-74: Improved error handling for missing components

The changes here implement the requested debug logging and user-friendly error message. However, there are two static analysis issues:

  1. Using dynamic errors (fmt.Errorf) instead of wrapped static errors
  2. Error message starts with capital letter
	l.Debug(err)
-	u.PrintErrorMarkdownAndExit("", fmt.Errorf("Component not found"), "")
+	u.PrintErrorMarkdownAndExit("", fmt.Errorf("component not found"), "")

Consider defining a constant for this error message to address the dynamic error issue.

🧰 Tools
🪛 GitHub Check: golangci

[failure] 73-73: [golangci] cmd/docs.go#L73
do not define dynamic errors, use wrapped static errors instead: "fmt.Errorf("Component not found")" (err113)


76-76: Consistent error for missing component

Same static analysis issues as the previous error message.

-	u.PrintErrorMarkdownAndExit("", fmt.Errorf("Component not found"), "")
+	u.PrintErrorMarkdownAndExit("", fmt.Errorf("component not found"), "")
🧰 Tools
🪛 GitHub Check: golangci

[failure] 76-76: [golangci] cmd/docs.go#L76
do not define dynamic errors, use wrapped static errors instead: "fmt.Errorf("Component not found")" (err113)


81-81: User-friendly message for missing README

This implements the requested feature to provide a helpful error message when documentation is missing. However, static analysis flags capitalization and punctuation in the error string.

-	u.PrintErrorMarkdownAndExit("", fmt.Errorf("Documentation is missing for the component `%s`. Consider adding a README.md to provide more context and details.", info.Component), "")
+	u.PrintErrorMarkdownAndExit("", fmt.Errorf("documentation is missing for the component `%s`; consider adding a README.md to provide more context and details", info.Component), "")
🧰 Tools
🪛 GitHub Check: golangci

[failure] 81-81: [golangci] cmd/docs.go#L81
error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)


96-97: Consistent error format for markdown renderer

Good improvement to error handling, but remember to follow error string formatting standards.

-	u.PrintErrorMarkdownAndExit("", fmt.Errorf("failed to initialize markdown renderer: %w", err), "")
+	u.PrintErrorMarkdownAndExit("", fmt.Errorf("failed to initialize markdown renderer: %w", err), "")

The format with a lowercase initial letter and %w error wrapping is correct here.

🛑 Comments failed to post (1)
cmd/docs.go (1)

72-82: 💡 Verification agent

❓ Verification inconclusive

Missing test case

In the PR comments, osterman requested adding a test case to validate these changes. Make sure to add a test that checks for a non-zero exit code and verifies the display of a helpful error message when documentation is not found.


🏁 Script executed:

#!/bin/bash
# Check if test case was added as requested in PR comments
grep -r "documentation is missing" "tests/test-cases"

Length of output: 53


Action Required: Add Missing Test Case for Documentation Error

The updated code in cmd/docs.go (lines 72-82) correctly exits when documentation is missing, but as noted, Osterman requested a test case to validate this behavior. Please add a test that:

  • Invokes the scenario where the component’s README.md is missing.
  • Verifies that the process exits with a non-zero exit code.
  • Confirms that the error message (indicating that documentation is missing) is clearly displayed.
🧰 Tools
🪛 GitHub Check: golangci

[failure] 81-81: [golangci] cmd/docs.go#L81
error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)


[failure] 73-73: [golangci] cmd/docs.go#L73
do not define dynamic errors, use wrapped static errors instead: "fmt.Errorf("Component not found")" (err113)


[failure] 76-76: [golangci] cmd/docs.go#L76
do not define dynamic errors, use wrapped static errors instead: "fmt.Errorf("Component not found")" (err113)

Copy link

codecov bot commented Mar 7, 2025

Codecov Report

Attention: Patch coverage is 0% with 12 lines in your changes missing coverage. Please review.

Project coverage is 17.45%. Comparing base (a410633) to head (f088bc5).

Files with missing lines Patch % Lines
cmd/docs.go 0.00% 12 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1032   +/-   ##
=======================================
  Coverage   17.44%   17.45%           
=======================================
  Files         170      170           
  Lines       18764    18762    -2     
=======================================
  Hits         3274     3274           
+ Misses      14892    14890    -2     
  Partials      598      598           
Flag Coverage Δ
unittests 17.45% <0.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ 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.

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.

Caution

Inline review comments failed to post. This is likely due to GitHub's limits when posting large numbers of comments. If you are seeing this consistently it is likely a permissions issue. Please check "Moderation" -> "Code review limits" under your organization settings.

Actionable comments posted: 3

🛑 Comments failed to post (3)
cmd/docs.go (3)

12-12: ⚠️ Potential issue

Alias required for imported log package.

The github.com/charmbracelet/log package should be imported with an alias according to project configuration.

-	"github.com/charmbracelet/log"
+	log "github.com/charmbracelet/log"
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

	"log" "github.com/charmbracelet/log"
🧰 Tools
🪛 golangci-lint (1.62.2)

12-12: import "github.com/charmbracelet/log" imported without alias but must be with alias "log" according to config

(importas)

🪛 GitHub Check: golangci

[failure] 12-12: [golangci] cmd/docs.go#L12
import "github.com/charmbracelet/log" imported without alias but must be with alias "log" according to config (importas)


71-73: 🛠️ Refactor suggestion

Improved error handling with static errors.

Good job logging the actual error for debugging while showing a friendly message to users. However, the linter flags that dynamic errors should be replaced with wrapped static errors.

 log.Debug(err)
-u.PrintErrorMarkdownAndExit("", fmt.Errorf("Component not found"), "")
+var ErrComponentNotFound = fmt.Errorf("component not found")
+u.PrintErrorMarkdownAndExit("", ErrComponentNotFound, "")

Move the static error declaration to the package level constants.

Committable suggestion skipped: line range outside the PR's diff.

🧰 Tools
🪛 GitHub Check: golangci

[failure] 72-72: [golangci] cmd/docs.go#L72
do not define dynamic errors, use wrapped static errors instead: "fmt.Errorf("Component not found")" (err113)


80-80: 🛠️ Refactor suggestion

Fix error message capitalization and formatting.

The linter is flagging that error strings should not be capitalized or end with punctuation. Let's fix the formatting while keeping the helpful message.

-u.PrintErrorMarkdownAndExit("", fmt.Errorf("Documentation is missing for the component `%s`. Consider adding a README.md to provide more context and details.", info.Component), "")
+var ErrDocumentationMissing = fmt.Errorf("documentation is missing for the component `%s`; consider adding a README.md to provide more context and details")
+u.PrintErrorMarkdownAndExit("", fmt.Errorf(ErrDocumentationMissing.Error(), info.Component), "")

Move the static error declaration to the package level constants.

Committable suggestion skipped: line range outside the PR's diff.

🧰 Tools
🪛 GitHub Check: golangci

[failure] 80-80: [golangci] cmd/docs.go#L80
error-strings: error strings should not be capitalized or end with punctuation or a newline (revive)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants