Skip to content

Fix unchecked errors in gothic package#8

Merged
tphakala merged 2 commits intomasterfrom
fix/errcheck-gothic-package
Dec 25, 2025
Merged

Fix unchecked errors in gothic package#8
tphakala merged 2 commits intomasterfrom
fix/errcheck-gothic-package

Conversation

@tphakala
Copy link
Owner

@tphakala tphakala commented Dec 25, 2025

Summary

  • Check req.ParseForm() error in CompleteUserAuth and return wrapped error
  • Explicitly ignore fmt.Fprintln error in BeginAuthHandler (terminal error output)
  • Check session.Save errors in tests using a.NoError()
  • Add test for ParseForm error handling

Context

This fixes issue #4 - unchecked errors found by errcheck linter in the gothic package.

The key change is adding proper error handling for req.ParseForm() in CompleteUserAuth. Previously, if form parsing failed during an OAuth callback (e.g., malformed POST body), the error was silently ignored which could lead to confusing authentication failures.

Test plan

  • All existing tests pass
  • New test verifies ParseForm error is properly propagated
  • golangci-lint passes with no issues

Fixes #4

Summary by CodeRabbit

  • Bug Fixes

    • Improved error handling in the authentication flow to properly catch and report errors during form data parsing and output operations.
  • Tests

    • Enhanced test coverage to verify correct behavior when form parsing fails during authentication.

✏️ Tip: You can customize this high-level summary in your review settings.

- Check req.ParseForm() error in CompleteUserAuth and return wrapped error
- Explicitly ignore fmt.Fprintln error in BeginAuthHandler (terminal error)
- Check session.Save errors in tests using a.NoError()

Fixes #4
Tests that CompleteUserAuth properly returns an error when
req.ParseForm() fails during OAuth callback processing.
@coderabbitai
Copy link

coderabbitai bot commented Dec 25, 2025

Walkthrough

This PR addresses unchecked errors in the gothic package by adding explicit error handling for req.ParseForm() failures in CompleteUserAuth and making error handling explicit in BeginAuthHandler. Tests are updated to verify the new error handling behavior and cover form parsing errors.

Changes

Cohort / File(s) Summary
Error handling improvements
gothic/gothic.go
Adds error check for req.ParseForm() in CompleteUserAuth to return wrapped errors on parse failures; makes fmt.Fprintln error handling explicit in BeginAuthHandler with blank identifier assignment.
Test coverage expansion
gothic/gothic_test.go
Adds new Test_CompleteUserAuth_ParseFormError function with custom errorReader type to simulate form parsing failures; updates Test_StateValidation to use a.NoError() assertions for session.Save() calls.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related issues

Possibly related PRs

  • Fix session cookie issues (#626, #549) #1 – Both PRs modify the CompleteUserAuth implementation; this PR adds form-parse error handling while the related PR addresses session storage behavior in the same function.

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and concisely summarizes the main change: fixing unchecked errors in the gothic package, which matches the core objective.
Linked Issues check ✅ Passed All three coding requirements from issue #4 are implemented: req.ParseForm() error handling with wrapped error return, explicit fmt.Fprintln error discard with blank identifiers, and tests updated/added for validation.
Out of Scope Changes check ✅ Passed All changes directly address the three unchecked errors identified in issue #4; no extraneous modifications were introduced.
✨ 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 fix/errcheck-gothic-package

📜 Recent review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 75250b6 and 996afa8.

📒 Files selected for processing (2)
  • gothic/gothic.go
  • gothic/gothic_test.go
🧰 Additional context used
🧬 Code graph analysis (2)
gothic/gothic_test.go (2)
gothic/gothic.go (5)
  • CompleteUserAuth (159-217)
  • Store (32-32)
  • GetState (99-105)
  • GetContextWithProvider (257-259)
  • SessionName (29-29)
session.go (1)
  • Session (13-21)
gothic/gothic.go (1)
user.go (1)
  • User (14-31)
⏰ 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). (4)
  • GitHub Check: test (1.25.x, windows-latest)
  • GitHub Check: test (1.22.x, windows-latest)
  • GitHub Check: test (1.24.x, windows-latest)
  • GitHub Check: test (1.23.x, windows-latest)
🔇 Additional comments (5)
gothic/gothic.go (2)

66-66: LGTM - Explicit error discard for terminal error handler.

Making the error discard explicit is the right approach here. In this terminal error handler where we've already written the HTTP 400 status, there's limited recourse if writing the error message fails.


197-199: Critical fix - proper error handling for form parsing.

This correctly addresses the unchecked req.ParseForm() error identified in issue #4. Without this check, malformed POST data or oversized requests would cause ParseForm to fail silently, and the code would continue with potentially empty or incorrect form parameters, leading to silent failures in the OAuth callback flow.

The error wrapping with %w is correct and preserves the error chain.

gothic/gothic_test.go (3)

243-243: LGTM - Improved test robustness.

Adding explicit assertions for session.Save error checking makes the test more robust and ensures that session operations succeed as expected. If session operations fail, the test will now fail with a clear error message rather than potentially causing confusing failures later in the test.

Also applies to: 249-249


413-453: Excellent test coverage for ParseForm error handling.

This test comprehensively verifies the new error handling for ParseForm failures. The test setup is well-designed:

  • GetState override: Correctly prevents FormValue from consuming the request body before CompleteUserAuth can test the ParseForm path (as noted in the helpful comment on line 421)
  • errorReader body: Simulates read failures that ParseForm would encounter with malformed request bodies
  • Session setup: Forces the code path where FetchUser fails, triggering the form parsing logic
  • Request construction: Properly sets Content-Type header and uses context provider to ensure empty query params, which triggers the POST form handling path

The comment on line 437-438 correctly notes the importance of using the request after GetContextWithProvider, since WithContext returns a new request object and ProviderStore keys by request pointer.


456-460: LGTM - Simple and effective test helper.

The errorReader implementation correctly simulates read failures for testing the ParseForm error path. The implementation is minimal and follows the io.Reader interface properly.


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.

@gemini-code-assist
Copy link

Summary of Changes

Hello @tphakala, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses unchecked errors within the gothic package, primarily focusing on enhancing the robustness of error handling during OAuth authentication flows. The key improvement involves properly checking and propagating errors from http.Request.ParseForm() in CompleteUserAuth, preventing silent failures that could lead to confusing authentication issues. Additionally, it refines error handling for terminal output and strengthens test coverage for session operations.

Highlights

  • Error Handling for req.ParseForm(): Implemented explicit error checking for req.ParseForm() within CompleteUserAuth, ensuring that form parsing failures are no longer silently ignored and are instead properly propagated.
  • fmt.Fprintln Error Ignoring: The return value of fmt.Fprintln in BeginAuthHandler is now explicitly ignored, acknowledging that errors during terminal output are generally not recoverable in this context.
  • Test Coverage for session.Save: Existing tests have been updated to explicitly assert no errors when calling session.Save, improving test robustness.
  • New Test for ParseForm Errors: A new test case (Test_CompleteUserAuth_ParseFormError) has been added to specifically verify the correct error handling when req.ParseForm() encounters an issue.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request effectively addresses several unchecked errors found by the errcheck linter. The primary fix of adding error handling for req.ParseForm() in CompleteUserAuth is well-implemented, including proper error wrapping. The changes to explicitly ignore errors where appropriate (like with fmt.Fprintln on a response writer) and to assert on previously unchecked errors in tests are also great improvements. The new test case for the ParseForm error path is thorough. I have one minor suggestion to make the new test code even more concise by using a more specific assertion function from the testify library.

Comment on lines +450 to +452
if a.Error(err, "Expected error from ParseForm failure") {
a.Contains(err.Error(), "failed to parse form", "Error should indicate form parsing failure")
}

Choose a reason for hiding this comment

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

medium

The testify/assert package provides a convenient ErrorContains function that can simplify this check. It combines asserting that an error is not nil and that its message contains a specific substring into a single call, making the test slightly more concise and readable.

a.ErrorContains(err, "failed to parse form", "Error should indicate form parsing failure")

Copy link
Owner Author

Choose a reason for hiding this comment

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

good suggestion but this project isn't using testify

@tphakala tphakala merged commit 54db017 into master Dec 25, 2025
18 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.

[errcheck] Critical: Fix unchecked errors in gothic package

1 participant