Implement Structured MemcachedError for Enhanced Error Handling#33
Implement Structured MemcachedError for Enhanced Error Handling#33FranzBusch merged 18 commits intoswift-server:mainfrom
Conversation
dc1fa42 to
ea8a16a
Compare
FranzBusch
left a comment
There was a problem hiding this comment.
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.
| 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.
| /// 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.
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.
NIT: (Everywhere please)
| location: MemcachedError.SourceLocation.here() | |
| location: .here() |
| /// - 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.
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.
| 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.
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.
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.
Same as above please with the unexpected return code
| code: .unexpectedNilResponse, | ||
| message: "Received an unexpected nil response from the Memcached server.", |
This PR introduces
MemcachedErrorstructure 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:
MemcachedErrorstructure, containing a Code type for high-level error kinds and detailed descriptions, including the error message, cause, and exact source location.MemcachedConnectionErrorand integrated its case errors intoMemcachedErrorfor a unified error handling approach.MemcachedConnectionthat previously usedMemcachedConnectionErrorto now leverageMemcachedError.getBufferAllocatormethod inMemcachedConnectionto abstract buffer allocation based on the actor's state, making code more concise and readable.MemcachedConnectionfor 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.