-
Notifications
You must be signed in to change notification settings - Fork 244
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
Adding context to database provider #1118
base: development
Are you sure you want to change the base?
Adding context to database provider #1118
Conversation
Hey @mfreeman451, the mocks have not been updated, but before that can we discuss some approaches and implementations? I feel like One other train of thought would be to FATAL log the errors, as it would panic the code even if it starts as the handler might expect the databases to be connected. |
…o bug/mongodb_context
// ErrInvalidURI is returned when the MongoDB URI is invalid or cannot be parsed. | ||
ErrInvalidURI = errors.New("invalid MongoDB URI") | ||
|
||
// ErrAuthentication is returned when authentication fails. | ||
ErrAuthentication = errors.New("authentication failed") | ||
|
||
// ErrDatabaseConnection is returned when the client fails to connect to the specified database. | ||
ErrDatabaseConnection = errors.New("failed to connect to database") | ||
|
||
// ErrGenericConnection is returned for general connection issues. | ||
ErrGenericConnection = errors.New("MongoDB connection error") |
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.
Here is a suggestion, feel free to consider it or ignore it
// ErrInvalidURI is returned when the MongoDB URI is invalid or cannot be parsed. | |
ErrInvalidURI = errors.New("invalid MongoDB URI") | |
// ErrAuthentication is returned when authentication fails. | |
ErrAuthentication = errors.New("authentication failed") | |
// ErrDatabaseConnection is returned when the client fails to connect to the specified database. | |
ErrDatabaseConnection = errors.New("failed to connect to database") | |
// ErrGenericConnection is returned for general connection issues. | |
ErrGenericConnection = errors.New("MongoDB connection error") | |
// ErrInvalidURI is returned when the MongoDB URI is invalid or cannot be parsed. | |
ErrInvalidURI = fmt.Errorf("%w: invalid MongoDB URI", ErrConnection) | |
// ErrAuthentication is returned when authentication fails. | |
ErrAuthentication = fmt.Errorf("%w: authentication failed", ErrConnection) | |
// ErrDatabaseConnection is returned when the client fails to connect to the specified database. | |
ErrDatabaseConnection = fmt.Errorf("%w: failed to connect to database", ErrConnection) | |
// ErrGenericConnection is returned for general connection issues. | |
ErrConnection = errors.New("MongoDB connection error") |
This way the caller can catch all mongodb error with an errors.Is(err, ErrConnection)
func (*Client) isTimeoutError(err error) bool { | ||
return strings.Contains(err.Error(), "connection timeout") || mongo.IsTimeout(err) | ||
} | ||
|
||
return | ||
func (*Client) isAuthenticationError(err error) bool { | ||
return strings.Contains(err.Error(), "authentication failed") || | ||
strings.Contains(err.Error(), "AuthenticationFailed") | ||
} |
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.
I have a big problem with this
Validating errors with what they contains is a bad idea
If you have no other way to do that, then you have to add a clear comment about it
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.
Take a look at the code
An error could be caught with an errors.As, looking for auth.Error
https://pkg.go.dev/go.mongodb.org/[email protected]/x/mongo/driver/auth#Error
} | ||
|
||
if errors.Is(err, mongo.ErrClientDisconnected) { | ||
return fmt.Errorf("%w: client disconnected", ErrGenericConnection) |
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.
return fmt.Errorf("%w: client disconnected", ErrGenericConnection) | |
return fmt.Errorf("%w: client disconnected: %w", ErrGenericConnection, err) |
|
||
func (c *Client) handlePingError(err error) error { | ||
if mongo.IsTimeout(err) { | ||
return fmt.Errorf("%w: connection timeout", ErrGenericConnection) |
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.
return fmt.Errorf("%w: connection timeout", ErrGenericConnection) | |
return fmt.Errorf("%w: connection timeout: %w", ErrGenericConnection, err) |
You should never hide low level error with yours. You should wrap them with yours
func (*Client) isTimeoutError(err error) bool { | ||
return strings.Contains(err.Error(), "connection timeout") || mongo.IsTimeout(err) |
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.
Is there a reason to do not rely only on mongo.IsTimeOut ?
It seems strange to check if error contains a text
If you have to, something I'm not sure, you should add a comment explaining it.
Pull Request Template
Description:
Breaking Changes (if applicable):
Additional Information:
Checklist:
goimport
andgolangci-lint
.Thank you for your contribution!