-
Notifications
You must be signed in to change notification settings - Fork 6
Implement Structured MemcachedError for Enhanced Error Handling #33
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
dc1fa42
to
ea8a16a
Compare
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.
Looks good overall. Left some nits around what errors we throw where.
throw MemcachedConnectionError.connectionShutdown | ||
throw MemcachedError( | ||
code: .connectionShutdown, | ||
message: "The connection to the Memcached server has shut down.", |
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.
message: "The connection to the Memcached server has shut down.", | |
message: "The connection to the Memcached server has been shut down.", |
} | ||
} | ||
|
||
/// A high-level error code to provide broad a classification. |
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.
/// A high-level error code to provide broad a classification. | |
/// A high-level error code to provide a broad classification. |
/// Indicates that a nil response was received from the server. | ||
case unexpectedNilResponse |
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 don't think we should expose this publicly and we should double check when we actually throw this. This really should only happen when the inboundStream
returns us nil
. In that case we should just throw connectionShutdown
code: .connectionShutdown, | ||
message: "The connection to the Memcached server has shut down.", | ||
cause: nil, | ||
location: MemcachedError.SourceLocation.here() |
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.
NIT: (Everywhere please)
location: MemcachedError.SourceLocation.here() | |
location: .here() |
@@ -461,7 +550,7 @@ public actor MemcachedConnection { | |||
/// - Parameters: | |||
/// - key: The key for the value to decrement. | |||
/// - amount: The `Int` amount to decrement the value by. Must be larger than 0. | |||
/// - Throws: A `MemcachedConnectionError` if the connection to the Memcached server is shut down. | |||
/// - Throws: A `MemcachedError` with the code `.connectionShutdown` if the connection to the Memcache server is shut down. |
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.
Let's just put that comment everywhere
/// - Throws: A `MemcachedError` with the code `.connectionShutdown` if the connection to the Memcache server is shut down. | |
/// - Throws: A `MemcachedError` that indicates the failure. |
code: .unexpectedNilResponse, | ||
message: "Received an unexpected nil response from the Memcached server.", |
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.
code: .unexpectedNilResponse, | |
message: "Received an unexpected nil response from the Memcached server.", | |
code: .protocolError, | |
message: "Received an unexpected return code \(response.returnCode) for a delete request.", |
/// - Throws: A `MemcachedConnectionError.connectionShutdown` error if the connection to the Memcache server is shut down. | ||
/// - Throws: A `MemcachedConnectionError.unexpectedNilResponse` error if the key was not found or if an unexpected response code was returned. | ||
/// - Throws: A `MemcachedError` with the code `.connectionShutdown` if the connection to the Memcache server is shut down. | ||
/// - Throws: A `MemcachedError` with the code `.keyNotFound` if the key was not found. |
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.
This is good to document!
/// - Throws: A `MemcachedConnectionError.keyExist` if the key already exists in the Memcached server. | ||
/// - Throws: A `MemcachedConnectionError.unexpectedNilResponse` if an unexpected response code is returned. | ||
/// - Throws: A `MemcachedError` with the code `.connectionShutdown` if the connection to the Memcache server is shut down. | ||
/// - Throws: A `MemcachedError` with the code `.keyExist` if the key already exist. |
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.
This is also good to document!
code: .unexpectedNilResponse, | ||
message: "Received an unexpected nil response from the Memcached server.", |
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.
Same as above please with the unexpected return code
code: .unexpectedNilResponse, | ||
message: "Received an unexpected nil response from the Memcached server.", |
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.
Same here
This PR introduces
MemcachedError
structure to provide a more structured and intuitive error handling mechanism. This aligns error handled with best practices from the SwiftNIO community, ensuring developers can quickly identify the Nate and origin of any errors.Motivation:
Adopting a more structured error handling mechanism empowers developers with clearer insights into errors. Moving away from string descriptions towards a type that retains detailed information about the error will enhance the overall developer experience.
Modifications:
MemcachedError
structure, containing a Code type for high-level error kinds and detailed descriptions, including the error message, cause, and exact source location.MemcachedConnectionError
and integrated its case errors intoMemcachedError
for a unified error handling approach.MemcachedConnection
that previously usedMemcachedConnectionError
to now leverageMemcachedError
.getBufferAllocator
method inMemcachedConnection
to abstract buffer allocation based on the actor's state, making code more concise and readable.MemcachedConnection
for cleaner and more maintainable code.Results:
With the structured
MemcachedError
, our API provides developers with a more comprehensive understanding of errors, ensuring that issues can be identified and addresses more efficiently.