Thanks to visit codestin.com
Credit goes to github.com

Skip to content

refract(gerror): add ITextArgs interface and its implements, mainly for i18n that needs text and args separately#4597

Merged
gqcn merged 5 commits intomasterfrom
feat/gerror-i18n
Jan 9, 2026
Merged

refract(gerror): add ITextArgs interface and its implements, mainly for i18n that needs text and args separately#4597
gqcn merged 5 commits intomasterfrom
feat/gerror-i18n

Conversation

@gqcn
Copy link
Member

@gqcn gqcn commented Jan 9, 2026

This pull request refactors the error handling code to improve support for error text formatting with arguments, making it easier to retrieve both the error message template and its arguments (useful for i18n and structured error handling). It introduces the new ITextArgs interface, updates error constructors to store format strings and arguments separately, and adds methods to retrieve them. Several usages and tests are updated to reflect these changes.

Error formatting and argument support

  • Introduced the ITextArgs interface to allow errors to expose their text template and arguments separately, supporting advanced use cases like internationalization (errors/gerror/gerror.go).
  • Updated the Error struct to include an args field for error arguments, and added methods TextWithArgs(), Text(), and Args() to retrieve formatted error text, the template, and arguments respectively (errors/gerror/gerror_error.go). [1] [2]
  • Changed all error creation and wrapping functions (e.g., Newf, Wrapf, NewCodef, etc.) to store the format string and arguments separately, rather than pre-formatting the error text (errors/gerror/gerror_api.go, errors/gerror/gerror_api_code.go). [1] [2] [3] [4] [5] [6] [7]
  • Updated the Option struct and related constructor to handle error arguments (errors/gerror/gerror_api_option.go). [1] [2]

Code and test improvements

  • Updated formatting and equality checks to use the new methods for retrieving formatted error text and arguments, ensuring consistent behavior (errors/gerror/gerror_error.go, errors/gerror/gerror_error_format.go). [1] [2]
  • Improved unit tests to verify the new interface and argument handling, including tests for the ITextArgs interface (errors/gerror/gerror_z_unit_test.go).
  • Minor code cleanup, such as removing unused imports and updating comments for clarity (errors/gerror/gerror_api.go, errors/gerror/gerror_api_code.go, errors/gerror/gerror_error_json.go). [1] [2] [3]

These changes make error handling more flexible and maintainable, especially for scenarios where error messages need to be localized or programmatically inspected.

@gqcn gqcn requested review from LanceAdd, Copilot, hailaz and houseme and removed request for Copilot January 9, 2026 01:43
Copilot AI review requested due to automatic review settings January 9, 2026 01:45
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request introduces the ITextArgs interface to enable separation of error text templates from their formatting arguments, primarily to support internationalization (i18n) scenarios. The changes refactor error creation to store format strings and arguments separately rather than pre-formatting them.

Key changes:

  • Added ITextArgs interface with Text() and Args() methods to expose error templates and arguments
  • Modified Error struct to include an args field and added TextWithArgs(), Text(), and Args() methods
  • Updated all error creation functions (Newf, Wrapf, NewCodef, etc.) to store format strings and args separately instead of pre-formatting

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
errors/gerror/gerror.go Defines new ITextArgs interface and updates existing interfaces to embed error instead of declaring Error() string
errors/gerror/gerror_error.go Adds args field to Error struct and implements TextWithArgs(), Text(), and Args() methods
errors/gerror/gerror_api.go Updates error creation functions to store format and args separately; removes fmt import
errors/gerror/gerror_api_code.go Updates code-based error creation functions to store format and args separately; removes fmt import
errors/gerror/gerror_api_option.go Adds Args field to Option struct for deferred formatting support
errors/gerror/gerror_error_format.go Updates Format() to use TextWithArgs() instead of direct text field access
errors/gerror/gerror_error_json.go Changes MarshalJSON receiver from value to pointer (contradicts existing comment)
errors/gerror/gerror_z_unit_test.go Adds Test_TextArgs function to test new interface and updates comment wording

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copilot AI review requested due to automatic review settings January 9, 2026 02:04
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Member

@houseme houseme left a comment

Choose a reason for hiding this comment

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

LGTM

@gqcn gqcn merged commit db9f47d into master Jan 9, 2026
26 checks passed
@gqcn gqcn deleted the feat/gerror-i18n branch January 9, 2026 02:48
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.

3 participants