Skip to content

Comments

feat(go): add HandlerFunc for error-returning handlers#4805

Open
dmeremyanin wants to merge 1 commit intofirebase:mainfrom
dmeremyanin:error-returning-handler
Open

feat(go): add HandlerFunc for error-returning handlers#4805
dmeremyanin wants to merge 1 commit intofirebase:mainfrom
dmeremyanin:error-returning-handler

Conversation

@dmeremyanin
Copy link
Contributor

@dmeremyanin dmeremyanin commented Feb 22, 2026

Fixes #4801

Adds genkit.HandlerFunc that returns errors instead of writing them to responses, enabling integration with frameworks expecting error-returning handlers (Echo, Fiber, etc).

genkit.Handler is now just a wrapper for genkit.HandlerFunc. All existing tests for genkit.Handler have been adapted for genkit.HandlerFunc, since it now holds the business logic. Added tests for genkit.Handler verify the error wrapping behavior.

Checklist (if applicable):

@github-actions github-actions bot added docs Improvements or additions to documentation go labels Feb 22, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @dmeremyanin, 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 enhances the genkit Go package by introducing genkit.HandlerFunc, a new handler type that returns errors directly instead of writing them to the HTTP response. This change significantly improves compatibility with popular Go web frameworks like Echo, Fiber, and Gin, allowing them to leverage their built-in centralized error handling mechanisms. The existing genkit.Handler has been refactored to utilize this new HandlerFunc, maintaining backward compatibility while providing a more flexible and idiomatic approach to error management in web applications.

Highlights

  • New HandlerFunc: Introduced genkit.HandlerFunc which returns an error type, allowing for more flexible error handling.
  • Framework Integration: Enabled seamless integration with web frameworks (e.g., Echo, Fiber, Gin) that expect error-returning handlers for centralized error management.
  • Handler Refactoring: Refactored the existing genkit.Handler to internally wrap genkit.HandlerFunc, maintaining backward compatibility.
  • Test Adaptation: Adapted existing tests for genkit.Handler to utilize genkit.HandlerFunc for their core logic.
  • New Error Handling Tests: Added new tests specifically to verify genkit.Handler's error wrapping behavior and genkit.HandlerFunc's error returning.
  • Documentation Update: Updated the README.md with examples demonstrating genkit.HandlerFunc usage with popular web frameworks.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • go/README.md
    • Added a new section detailing genkit.HandlerFunc and providing an example of its integration with the Echo framework.
  • go/genkit/servers.go
    • Defined HandlerFunc with a func(http.ResponseWriter, *http.Request) error signature.
    • Modified Handler to call wrapHandler(HandlerFunc(...)).
  • go/genkit/servers_test.go
    • Expanded TestHandler to include comprehensive error handling tests for genkit.Handler.
    • Created TestHandlerFunc to validate the new error-returning handler's behavior, adapting previous tests to this new function.
Activity
  • dmeremyanin opened this pull request to address issue [Go] Allow using GenKit handler with Echo/Fiber error handling #4801.
  • The PR introduces a new HandlerFunc for error-returning handlers in Go.
  • The author has confirmed adherence to conventional commits and tested the changes.
  • Documentation has been updated to reflect the new functionality.
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
Contributor

@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

The pull request introduces genkit.HandlerFunc to provide an error-returning HTTP handler, which is beneficial for integration with frameworks that have centralized error handling. The changes include updates to servers.go to implement HandlerFunc and modify Handler to wrap HandlerFunc. The README.md has been updated to document the new HandlerFunc and its use cases. Additionally, servers_test.go has been significantly expanded to include comprehensive tests for both Handler and HandlerFunc, covering various success and error scenarios, including streaming and durable streaming. The test cases for Handler now verify error wrapping behavior, while HandlerFunc tests focus on the returned error. Overall, the changes are well-implemented and tested, improving the flexibility of Genkit's HTTP handling.

Comment on lines +126 to 127
panic(fmt.Errorf("genkit.HandlerFunc: error applying options: %w", err))
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The panic message here should reflect that it's coming from genkit.HandlerFunc for clarity in debugging, as Handler now calls HandlerFunc.

			if err := opt.applyHandler(options); err != nil {
				panic(fmt.Errorf("genkit.HandlerFunc: error applying options: %w", err))
			}

Comment on lines 265 to 267
if !strings.Contains(string(body), `"test-input"`) {
t.Errorf("want response to contain test-input, got %q", string(body))
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The error message here is slightly off. It should be want response to contain 'test-input', got %q to match the expected string. Also, the single quotes around test-input are missing in the current message.

Suggested change
if !strings.Contains(string(body), `"test-input"`) {
t.Errorf("want response to contain test-input, got %q", string(body))
}
if !strings.Contains(string(body), `"test-input"`) {
t.Errorf("want response to contain \"test-input\", got %q", string(body))
}

Comment on lines 321 to 323
if !strings.Contains(string(body), "action-context-value") {
t.Errorf("want response to containaction-context-value, got %q", string(body))
t.Errorf("want response to contain action-context-value, got %q", string(body))
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The error message here has a typo: containaction-context-value. It should be contain 'action-context-value'.

Suggested change
if !strings.Contains(string(body), "action-context-value") {
t.Errorf("want response to containaction-context-value, got %q", string(body))
t.Errorf("want response to contain action-context-value, got %q", string(body))
}
if !strings.Contains(string(body), "action-context-value") {
t.Errorf("want response to contain 'action-context-value', got %q", string(body))
}

Comment on lines 349 to 351
if !strings.Contains(string(body), "value1,value2") {
t.Errorf("want response to contain value1,value2, got %q", string(body))
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The error message here has a typo: contain value1,value2. It should be contain 'value1,value2'.

Suggested change
if !strings.Contains(string(body), "value1,value2") {
t.Errorf("want response to contain value1,value2, got %q", string(body))
}
if !strings.Contains(string(body), "value1,value2") {
t.Errorf("want response to contain 'value1,value2', got %q", string(body))
}

@dmeremyanin dmeremyanin force-pushed the error-returning-handler branch from f7be254 to b892d7e Compare February 22, 2026 14:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to documentation go

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

[Go] Allow using GenKit handler with Echo/Fiber error handling

1 participant