-
Notifications
You must be signed in to change notification settings - Fork 2.5k
feat(errors): Introduce typed errors #3602
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this 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 PR introduces typed Redis errors to improve error handling when errors are wrapped by hooks or other middleware. The implementation replaces string-based error checking with type-based checking using Go's errors.As() function, allowing error type detection to work correctly even through multiple layers of error wrapping.
Key changes:
- Added typed error structs for common Redis errors (LoadingError, ReadOnlyError, MovedError, AskError, ClusterDownError, TryAgainError, MasterDownError, MaxClientsError)
- Implemented helper functions for type-safe error checking that work with wrapped errors
- Updated error handling logic in
shouldRetry()andisMovedError()to use typed checks - Added comprehensive test coverage for typed errors and error wrapping scenarios
- Updated documentation with examples of error wrapping in hooks
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 9 comments.
Show a summary per file
| File | Description |
|---|---|
| internal/proto/redis_errors.go | Defines typed error types and parsing logic for common Redis errors |
| internal/proto/redis_errors_test.go | Unit tests for typed error creation, wrapping, and address extraction |
| internal/proto/reader.go | Updates ParseErrorReply to use typed error parsing |
| error.go | Updates retry logic to use typed error checks and exports public error checking functions |
| error_wrapping_test.go | Integration tests demonstrating typed errors work with hook wrapping |
| README.md | Documents typed error checking functions and error wrapping patterns in hooks |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this 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 7 out of 7 changed files in this pull request and generated 4 comments.
Comments suppressed due to low confidence (1)
error.go:117
- The string fallback for
MASTERDOWNerrors is missing. While there's a typed error check on line 91, the fallback string check for backward compatibility with plain errors is not present. This could cause retries to not work for wrapped non-typed MASTERDOWN errors.
Add the following after line 115:
if strings.HasPrefix(s, "MASTERDOWN ") {
return true
} // Fallback to string checking for backward compatibility with plain errors
s := err.Error()
if strings.HasPrefix(s, "ERR max number of clients reached") {
return true
}
if strings.HasPrefix(s, "LOADING ") {
return true
}
if strings.HasPrefix(s, "READONLY ") {
return true
}
if strings.HasPrefix(s, "CLUSTERDOWN ") {
return true
}
if strings.HasPrefix(s, "TRYAGAIN ") {
return true
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
htemelski-redis
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like a great and much needed addition
Well done
There was a problem hiding this 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 11 out of 11 changed files in this pull request and generated 4 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
67ca39b to
6b5a569
Compare
This PR introduces typed errors for the library, utilising errors.As for error handling instead of the string checks that were used before. This way, errors can be wrapped and set to the command in hooks without breaking the library functionality.