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: write errors/warnings to stderr rather than stdout #651

Merged
merged 4 commits into from
Nov 14, 2024

Conversation

sm3142
Copy link
Contributor

@sm3142 sm3142 commented Nov 13, 2024

What

Tiny PR to send Huma's (rare) direct error/warning output to stderr rather than stdout

Why

  1. It's customary to send errors and warnings to stderr
  2. I stumbled upon this when using go run . openapi > openapi.yaml and the warnings from transforms.go:135 were mucking up the Open API spec.

How

Code should be obvious enough without further exposition

[1] I realize I still have to deal with the actual cause for the warnings in my code, but they are incidentally also more visible when sent to stderr.

Testing

AFAICT, the statements are not currently covered by test cases. IMHO the changes are too trivial to justify the cost of setting up explicit tests.

Thanks for considering this PR!

Summary by CodeRabbit

  • Bug Fixes

    • Improved error logging by directing messages to the standard error output, enhancing visibility of error reporting.
  • New Features

    • Enhanced error handling in schema link operations, capturing and logging any panics that occur during processing.
    • Warnings related to unexported options fields are now logged to standard error for better visibility.
    • Error messages related to SSE operations are now consistently logged to standard error, improving clarity in reporting.

Copy link

coderabbitai bot commented Nov 13, 2024

Walkthrough

The changes in this pull request focus on enhancing error logging across multiple files: error.go, transforms.go, humacli/humacli.go, and sse/sse.go. The common update involves replacing standard output logging with standard error logging using os.Stderr. Specifically, the WriteErr function in error.go, the OnAddOperation method in transforms.go, the setupOptions method in humacli.go, and the Register function in sse.go have all been modified to improve error visibility. These modifications do not alter existing functionalities or control flows.

Changes

File Change Summary
error.go Added import for os. Updated WriteErr to use fmt.Fprintf for logging errors to os.Stderr.
transforms.go Added import for os. Updated OnAddOperation to use fmt.Fprintln(os.Stderr, ...) for logging errors. Maintained existing logic in addSchemaField.
humacli/humacli.go Added import for os. Updated setupOptions to use fmt.Fprintln(os.Stderr, ...) for logging warnings about unexported fields.
sse/sse.go Added import for os. Updated Register to use fmt.Fprintln(os.Stderr, ...) for logging warnings and errors.

Possibly related PRs

Suggested reviewers

  • danielgtaylor

🐇 In the land of code where errors reside,
A hop to stderr, we now take in stride.
With fmt.Fprintf, our logs shine bright,
No more lost errors, they’re now in plain sight.
So let’s celebrate with a joyful cheer,
For clearer logs, we hold so dear! 🎉


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between eee2d29 and 7e4e027.

📒 Files selected for processing (2)
  • error.go (2 hunks)
  • sse/sse.go (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • error.go
  • sse/sse.go

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

codecov bot commented Nov 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 93.04%. Comparing base (bea7c1a) to head (7e4e027).
Report is 8 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #651      +/-   ##
==========================================
+ Coverage   92.97%   93.04%   +0.06%     
==========================================
  Files          22       22              
  Lines        4895     4901       +6     
==========================================
+ Hits         4551     4560       +9     
+ Misses        300      298       -2     
+ Partials       44       43       -1     

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

@sm3142 sm3142 changed the title fix: write error messages to stderr rather than stdout fix: write errors/warnings to stderr rather than stdout Nov 13, 2024
Copy link

@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: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between f71eb50 and 1c5a71d.

📒 Files selected for processing (2)
  • error.go (2 hunks)
  • transforms.go (2 hunks)
🔇 Additional comments (4)
transforms.go (2)

6-6: LGTM: Import added correctly

The os package import is correctly added and properly ordered within the import block.


136-136: LGTM: Error output correctly redirected to stderr

The change properly implements the PR objective by redirecting the warning message to stderr. This is particularly beneficial when redirecting stdout to files (e.g., go run . openapi > openapi.yaml), as warning messages will no longer interfere with the generated output.

error.go (2)

7-7: LGTM: Required import for stderr functionality

The addition of the "os" package import is necessary and properly placed.


268-268: LGTM: Appropriate use of stderr for error logging

The change from fmt.Printf to fmt.Fprintf(os.Stderr, ...) aligns with Unix convention and best practices where error messages should be written to stderr. This fixes the issue where error messages were interfering with stdout operations like redirecting output to files.

transforms.go Show resolved Hide resolved
Copy link

@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

🧹 Outside diff range and nitpick comments (2)
sse/sse.go (2)

164-167: Consider enhancing the warning message for better debugging.

The warnings are now correctly directed to stderr. However, the warning message on line 167 could be more informative.

Consider this enhancement:

-fmt.Fprintln(os.Stderr, "warning: unable to set write deadline")
+fmt.Fprintln(os.Stderr, "warning: write deadline not supported by the underlying writer")

Line range hint 180-182: Missed stdout usage that should be converted to stderr.

This error message is still using stdout (fmt.Println) while all other errors have been converted to use stderr.

Apply this change for consistency:

-fmt.Println("error: unknown event type", reflect.TypeOf(msg.Data))
+fmt.Fprintf(os.Stderr, "error: unknown event type %v\n", reflect.TypeOf(msg.Data))
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 1c5a71d and a130837.

📒 Files selected for processing (2)
  • humacli/humacli.go (1 hunks)
  • sse/sse.go (3 hunks)
✅ Files skipped from review due to trivial changes (1)
  • humacli/humacli.go
🔇 Additional comments (2)
sse/sse.go (2)

9-9: LGTM: Import added for stderr redirection.

The addition of the "os" import is necessary and correctly placed.


202-204: LGTM: Error handling improved with stderr.

The error is now correctly written to stderr and properly wrapped with http.ErrNotSupported.

Copy link
Owner

@danielgtaylor danielgtaylor left a comment

Choose a reason for hiding this comment

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

Thanks!

@danielgtaylor danielgtaylor merged commit bb5d167 into danielgtaylor:main Nov 14, 2024
5 checks passed
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