Skip to content

feat: handle sql.ErrNoRows in generated database wrappers - #646

Merged
kalbasit merged 3 commits into
mainfrom
01-24-feat_handle_sql.errnorows_in_generated_database_wrappers
Jan 24, 2026
Merged

kalbasit merged 3 commits into
mainfrom
01-24-feat_handle_sql.errnorows_in_generated_database_wrappers

Conversation

@kalbasit

Copy link
Copy Markdown
Owner

This change updates the database wrapper generator to correctly catch
sql.ErrNoRows and return our custom ErrNotFound error instead. This ensures
consistency across the different database backends (SQLite, Postgres, MySQL)
and simplifies error handling in the application layer.

The generator now includes checks for sql.ErrNoRows in both the loop-based
handlers and the direct return handlers.

This change updates the database wrapper generator to correctly catch
sql.ErrNoRows and return our custom ErrNotFound error instead. This ensures
consistency across the different database backends (SQLite, Postgres, MySQL)
and simplifies error handling in the application layer.

The generator now includes checks for sql.ErrNoRows in both the loop-based
handlers and the direct return handlers.
@dosubot dosubot Bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Jan 24, 2026
@dosubot dosubot Bot added enhancement New feature or request go Pull requests that update go code labels Jan 24, 2026

Copy link
Copy Markdown
Owner Author

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @kalbasit, 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 introduces a significant improvement to the application's database interaction layer by standardizing how "record not found" errors are handled. By modifying the database wrapper generator to catch sql.ErrNoRows and convert it into a custom ErrNotFound, the change ensures consistent error reporting across all supported database engines. This abstraction simplifies error handling for developers, making the codebase more robust and easier to maintain.

Highlights

  • Standardized Error Handling: The database wrapper generator now explicitly intercepts sql.ErrNoRows and translates it into a custom ErrNotFound.
  • Consistent Behavior: This change ensures that all database backends (SQLite, Postgres, MySQL) consistently return ErrNotFound when a record is not found, simplifying error management at the application layer.
  • Generator Updates: The code generation logic has been enhanced to include sql.ErrNoRows checks in both loop-based and direct return handlers, providing appropriate zero values for different return types.
  • Refactored Error Checks: Existing application code has been updated to utilize the new database.IsNotFoundError helper function, promoting a unified approach to checking for "not found" errors.
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. ↩

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

Copy link
Copy Markdown
Contributor

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 introduces a great improvement by abstracting sql.ErrNoRows into a custom database.ErrNotFound. This simplifies error handling throughout the application and makes the database layer more consistent. The changes are well-executed across the generator, application code, and tests.

I've added one suggestion to refactor the error handling logic in the database wrapper generator. This will reduce code duplication in the generated files and also fix a potential latent bug.

@kalbasit

Copy link
Copy Markdown
Owner Author

I've added one suggestion to refactor the error handling logic in the database wrapper generator. This will reduce code duplication in the generated files and also fix a potential latent bug.

@gemini-code-assist I don't see your suggestion.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Hello @kalbasit, I apologize that the suggestion was not visible. It seems there might have been an issue with how the comment was posted. I intended to provide a suggestion for refactoring the error handling logic in the nix/gen-db-wrappers/src/main.go template.

Problem Description

The current template generates repetitive if-else if blocks to handle sql.ErrNoRows and other errors, leading to code duplication in the generated database wrapper files. More critically, for methods where .Method.ReturnsSelf is true (which typically implies a (Querier, error) return signature), the template currently generates return nil (swallowing the error) when sql.ErrNoRows or any other error occurs. This is a latent bug, as sql.ErrNoRows should be consistently translated to ErrNotFound, and other errors should be propagated.

Suggested Refactoring

To address both the code duplication and the latent bug, I suggest introducing a helper template function. This function would encapsulate the logic for returning the appropriate zero value along with the specified error. This makes the generated code cleaner and ensures consistent error handling.

Here's the suggested template definition to be added to nix/gen-db-wrappers/src/main.go (e.g., at the top level or within a relevant define block):

{{- define "handleErrorReturn" -}}
	{{- $retType := .Method.ReturnType -}}
	{{- if .Method.ReturnsSelf -}}
		return nil, {{.ErrorVar}} // Assuming Querier methods return (Querier, error)
	{{- else if not .Method.HasValue -}}
		return {{.ErrorVar}}
	{{- else if isSlice $retType -}}
		return nil, {{.ErrorVar}}
	{{- else if isDomainStruct .Method.ReturnElem -}}
		return {{.Method.ReturnElem}}{}, {{.ErrorVar}}
	{{- else -}}
		return {{zeroValue $retType}}, {{.ErrorVar}}
	{{- end -}}
{{- end -}}

And then, replace the existing if err != nil block (around lines 689-706 in the RIGHT side of nix/gen-db-wrappers/src/main.go) with calls to this new helper:

		{{/* 2. HANDLE ERROR */}}
		{{- if .Method.ReturnsError}}
		if err != nil {
			if errors.Is(err, sql.ErrNoRows) {
				{{- template "handleErrorReturn" (dict "Method" .Method "ErrorVar" "ErrNotFound") -}}
			}
			{{- template "handleErrorReturn" (dict "Method" .Method "ErrorVar" "err") -}}
		}
		{{- end}}

This change ensures that sql.ErrNoRows is always converted to ErrNotFound with the correct zero value for the return type, and other errors are propagated correctly, including for ReturnsSelf methods. The ErrorVar parameter allows the same helper to be used for both ErrNotFound and generic err propagation, significantly reducing template code duplication.

This change introduces a helper template handleErrorReturn to reduce code duplication and fix a latent bug where errors were swallowed for methods returning (Querier, error).
@kalbasit

Copy link
Copy Markdown
Owner Author

/gemini review

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

Copy link
Copy Markdown
Contributor

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 refactors the database layer to abstract away sql.ErrNoRows into a domain-specific database.ErrNotFound. The changes are implemented cleanly by updating the database wrapper generator with a new handleErrorReturn template, which improves code generation and maintainability. The updates are consistently applied across all database backends (MySQL, PostgreSQL, SQLite) and all call sites have been updated to use the new database.IsNotFoundError function. The test suite has also been updated accordingly. Overall, this is a solid improvement for error handling consistency. I've found one minor issue in a test case that I've commented on.

Comment thread nix/gen-db-wrappers/src/main_test.go Resolved
@kalbasit
kalbasit enabled auto-merge (squash) January 24, 2026 18:59
@kalbasit
kalbasit merged commit f7f4068 into main Jan 24, 2026
@kalbasit
kalbasit deleted the 01-24-feat_handle_sql.errnorows_in_generated_database_wrappers branch January 24, 2026 19:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request go Pull requests that update go code size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant