docs(goerr-error-handling): add comprehensive guide for error handling using the goerr library to enhance Go error management practices#34
Conversation
…g using the goerr library to enhance Go error management practices
Summary of ChangesHello @m-mizutani, 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 new documentation file that serves as a comprehensive guide for Go developers on effectively handling errors using the Highlights
🧠 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. Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
Code Review
This pull request adds a comprehensive guide for using the goerr library. The documentation is well-structured and covers the main features of the library effectively. I've provided a few suggestions to enhance clarity and provide more complete examples, particularly around error categorization and the usage of tags and sentinel errors. I also suggested a minor formatting improvement for better readability.
| | Add context without changing message | `goerr.With(err, goerr.V("k", v))` | `goerr.Wrap(err, err.Error(), goerr.V("k", v))` | | ||
| | Simple key-value context | `goerr.V("key", value)` | Building context into message string | | ||
| | Type-safe context | `goerr.TV(typedKey, value)` | `goerr.V()` when type safety matters | | ||
| | Categorize errors | `goerr.T(tagValue)` | Sentinel errors for categories | |
There was a problem hiding this comment.
The 'NOT' column for 'Categorize errors' could be clearer. While sentinel errors can be used for categorization, a more common anti-pattern that tags help avoid is embedding category information as strings within the error message. This aligns better with the guide's general advice to use structured context over string manipulation.
| | Categorize errors | `goerr.T(tagValue)` | Sentinel errors for categories | | |
| | Categorize errors | `goerr.T(tagValue)` | Ad-hoc strings in error messages | |
| var ( | ||
| TagNotFound = goerr.NewTag("not_found") | ||
| TagValidation = goerr.NewTag("validation") | ||
| ) | ||
|
|
||
| var ( | ||
| ErrNotFound = goerr.New("not found", goerr.ID("ERR_NOT_FOUND")) | ||
| ErrInvalid = goerr.New("invalid input", goerr.ID("ERR_INVALID")) | ||
| ) |
There was a problem hiding this comment.
The example for defining tags and sentinel errors is good, but it would be more helpful to also show how to apply and check for them. This provides a more complete picture of the pattern.
| var ( | |
| TagNotFound = goerr.NewTag("not_found") | |
| TagValidation = goerr.NewTag("validation") | |
| ) | |
| var ( | |
| ErrNotFound = goerr.New("not found", goerr.ID("ERR_NOT_FOUND")) | |
| ErrInvalid = goerr.New("invalid input", goerr.ID("ERR_INVALID")) | |
| ) | |
| var ( | |
| TagNotFound = goerr.NewTag("not_found") | |
| TagValidation = goerr.NewTag("validation") | |
| ) | |
| var ( | |
| ErrNotFound = goerr.New("not found", goerr.ID("ERR_NOT_FOUND")) | |
| ErrInvalid = goerr.New("invalid input", goerr.ID("ERR_INVALID")) | |
| ) | |
| // Example usage: | |
| // | |
| // To apply a tag: | |
| // err := goerr.New("user not found", goerr.T(TagNotFound)) | |
| // | |
| // To check for a tag: | |
| // if goerr.HasTag(err, TagNotFound) { /* ... */ } | |
| // | |
| // To check for a sentinel error (even if wrapped): | |
| // if errors.Is(err, ErrNotFound) { /* ... */ } |
| **Error creation**: `goerr.New`, `goerr.Wrap`, `goerr.With` | ||
|
|
||
| **Options**: `goerr.Value` / `goerr.V`, `goerr.TypedValue` / `goerr.TV`, `goerr.Tag` / `goerr.T`, `goerr.ID` | ||
|
|
||
| **Extraction**: `goerr.Unwrap`, `goerr.AsErrors`, `goerr.Values`, `goerr.TypedValues`, `goerr.Tags`, `goerr.HasTag`, `goerr.GetTypedValue` | ||
|
|
||
| **Multiple errors**: `goerr.Join`, `goerr.Append`, `(*Errors).ErrorOrNil`, `(*Errors).IsEmpty`, `(*Errors).Len` | ||
|
|
||
| **Builder**: `goerr.NewBuilder`, `(*Builder).With`, `(*Builder).New`, `(*Builder).Wrap` | ||
|
|
||
| **Type-safe keys**: `goerr.NewTypedKey[T]`, `goerr.TV`, `goerr.GetTypedValue` | ||
|
|
||
| **Tags**: `goerr.NewTag`, `goerr.T`, `goerr.HasTag`, `(*Error).HasTag` | ||
|
|
||
| **Stack control**: `(*Error).Unstack`, `(*Error).UnstackN` |
There was a problem hiding this comment.
For better readability and structure, it's recommended to format the Quick Reference section as a bulleted list. This makes the different categories of functions easier to scan.
| **Error creation**: `goerr.New`, `goerr.Wrap`, `goerr.With` | |
| **Options**: `goerr.Value` / `goerr.V`, `goerr.TypedValue` / `goerr.TV`, `goerr.Tag` / `goerr.T`, `goerr.ID` | |
| **Extraction**: `goerr.Unwrap`, `goerr.AsErrors`, `goerr.Values`, `goerr.TypedValues`, `goerr.Tags`, `goerr.HasTag`, `goerr.GetTypedValue` | |
| **Multiple errors**: `goerr.Join`, `goerr.Append`, `(*Errors).ErrorOrNil`, `(*Errors).IsEmpty`, `(*Errors).Len` | |
| **Builder**: `goerr.NewBuilder`, `(*Builder).With`, `(*Builder).New`, `(*Builder).Wrap` | |
| **Type-safe keys**: `goerr.NewTypedKey[T]`, `goerr.TV`, `goerr.GetTypedValue` | |
| **Tags**: `goerr.NewTag`, `goerr.T`, `goerr.HasTag`, `(*Error).HasTag` | |
| **Stack control**: `(*Error).Unstack`, `(*Error).UnstackN` | |
| - **Error creation**: `goerr.New`, `goerr.Wrap`, `goerr.With` | |
| - **Options**: `goerr.Value` / `goerr.V`, `goerr.TypedValue` / `goerr.TV`, `goerr.Tag` / `goerr.T`, `goerr.ID` | |
| - **Extraction**: `goerr.Unwrap`, `goerr.AsErrors`, `goerr.Values`, `goerr.TypedValues`, `goerr.Tags`, `goerr.HasTag`, `goerr.GetTypedValue` | |
| - **Multiple errors**: `goerr.Join`, `goerr.Append`, `(*Errors).ErrorOrNil`, `(*Errors).IsEmpty`, `(*Errors).Len` | |
| - **Builder**: `goerr.NewBuilder`, `(*Builder).With`, `(*Builder).New`, `(*Builder).Wrap` | |
| - **Type-safe keys**: `goerr.NewTypedKey[T]`, `goerr.TV`, `goerr.GetTypedValue` | |
| - **Tags**: `goerr.NewTag`, `goerr.T`, `goerr.HasTag`, `(*Error).HasTag` | |
| - **Stack control**: `(*Error).Unstack`, `(*Error).UnstackN` |
No description provided.