Error handling prototype #7
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR is a mockup for handling the API errors. It uses a top level enum
WPApiError
and then uses subtypes when appropriate, such asClientErrorType
. Endpoint specific errors would probably be handled this way as well, but it's hard to tell if that'd be the right setup without a full investigation of each endpoint.One further idea might be to define suggested resolutions per error type - such as "Show a message to the user", "retry the request", "report the issue" etc. I am not sure if this library is the right place to define those resolutions, but it's something we should think about as that's the biggest consideration in terms of how much granular we'd like to go with our error types. If multiple errors are expected to be resolved in the same way, there is rarely a reason to separate each one.
With this setup, we should be able to handle generic errors from a centralized function and that should cover most our needs. We can also handle the parsing in a more generic way and plug the centralized error handling to it. That means, there is a good chance we can keep our endpoint specific functions clear and concise.
This PR also introduces
NetworkResponseStatus
which makes it easy to usehttp::StatusCode
directly. One of the main disadvantages of having our network layer be swappable is that we can't directly work with the types from the libraries that we use. Defining our own traits and implementing them for thehttp
crate could be the glue to make thewp_networking
code cleaner.I am not sure if this pattern is necessary for status code, because we could probably just use a
u16
and that'd be enough. However, I wanted to keep this implementation for now as a reference to this pattern.