Skip to content

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

Merged
merged 18 commits into from
Aug 18, 2023

Conversation

dkz2
Copy link
Contributor

@dkz2 dkz2 commented Aug 11, 2023

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:

  • Introduced the MemcachedError structure, containing a Code type for high-level error kinds and detailed descriptions, including the error message, cause, and exact source location.
  • Removed MemcachedConnectionError and integrated its case errors into MemcachedError for a unified error handling approach.
  • Updated all instances within MemcachedConnection that previously used MemcachedConnectionError to now leverage MemcachedError.
  • Added the getBufferAllocator method in MemcachedConnection to abstract buffer allocation based on the actor's state, making code more concise and readable.
  • Further enhanced state management within MemcachedConnection for cleaner and more maintainable code.
  • Added Unit Test an Updated our Integration Test.

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.

@dkz2 dkz2 force-pushed the Implement-MemcachedError branch from dc1fa42 to ea8a16a Compare August 11, 2023 19:18
@dkz2 dkz2 marked this pull request as ready for review August 12, 2023 12:22
Copy link
Contributor

@FranzBusch FranzBusch left a 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.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// A high-level error code to provide broad a classification.
/// A high-level error code to provide a broad classification.

Comment on lines 159 to 160
/// Indicates that a nil response was received from the server.
case unexpectedNilResponse
Copy link
Contributor

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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: (Everywhere please)

Suggested change
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.
Copy link
Contributor

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

Suggested change
/// - Throws: A `MemcachedError` with the code `.connectionShutdown` if the connection to the Memcache server is shut down.
/// - Throws: A `MemcachedError` that indicates the failure.

Comment on lines 313 to 314
code: .unexpectedNilResponse,
message: "Received an unexpected nil response from the Memcached server.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link
Contributor

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.
Copy link
Contributor

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!

Comment on lines 440 to 441
code: .unexpectedNilResponse,
message: "Received an unexpected nil response from the Memcached server.",
Copy link
Contributor

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

Comment on lines 495 to 496
code: .unexpectedNilResponse,
message: "Received an unexpected nil response from the Memcached server.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

@FranzBusch FranzBusch merged commit 1808018 into swift-server:main Aug 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants