Skip to content
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

Support generic error types #5

Merged
merged 6 commits into from Jun 3, 2021
Merged

Support generic error types #5

merged 6 commits into from Jun 3, 2021

Conversation

ByteAlex
Copy link
Owner

@ByteAlex ByteAlex commented Jun 3, 2021

As wished in #3 here's an implementation for generic error types

@ByteAlex ByteAlex linked an issue Jun 3, 2021 that may be closed by this pull request
Copy link
Contributor

@Dessix Dessix 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 to me :)

src/cache_api.rs Outdated
#[error("An error occurred when trying to submit the cache request")]
TokioMpscSendError(),
#[error("An error occurred when trying to join the result future")]
FutureJoinError(tokio::task::JoinError),
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider using #[from] for each of these to automatically generate Into implementations:

Suggested change
FutureJoinError(tokio::task::JoinError),
FutureJoinError(#[from] tokio::task::JoinError),

Copy link
Owner Author

Choose a reason for hiding this comment

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

Your suggested change doesn't compile.
I'm not quite sure, but it seems the derive generates some code which clashes with the code generated from the #[from].
I'll check if its a simple thing, or if I just implement the Into traits myself.

Copy link
Contributor

@Dessix Dessix Jun 3, 2021

Choose a reason for hiding this comment

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

Interesting- if you post the compilation error, I can suggest what may be breaking.

It's also possible that the issue is because thiserror struggles with Generic Parameters: dtolnay/thiserror#79

#[derive(Error, Debug)]
pub enum CacheLoadingError<E: Debug> {
#[error("An error occurred when trying to submit the cache request")]
TokioMpscSendError(),
Copy link
Contributor

Choose a reason for hiding this comment

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

Potentially, tokio errors may be worth grouping together. Do you intend for the user to ever "handle" one of these?

I'd consider a second error type, CacheCommunicationsError or similar, which then contains details on whether it was a Send issue, a failure to join futures, etc.

Generally, I'd expect the user is to be interested in: LoadingError, LookupLoop, and NoData (Is this one actually expected to be seen by the user at all, or is it internally used to trigger a load?). Beyond that- everything else strikes me as "I probably just want to panic if it's one of these".

Copy link
Owner Author

Choose a reason for hiding this comment

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

This one indeed makes sense. I think the only error actually relevant to the user is the LoadingError.
In any other case something is odd with the cache.
LookupLoop happens when the cache state doesnt mutate at all, and it would have to do the same lookup again.
NoData is when you call an update function, but the loader doesn't return success, then there's "NoData" to update/mutate.
The most frequent error case will be the LoadingError though, eventually NoData will be interesting too.
The others are actually really edge cases.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Implemented in new commit, thanks for the suggestion!

pub loading_error: Option<LoadingError>,
// todo: nested errors
#[derive(Error, Debug)]
pub enum CacheLoadingError<E: Debug> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Copying from #3 for contextual proximity:

Potentially, a few helpers for the CacheLoadingError type may be warranted, such as into_loading_error(self) -> Option<E> / as_loading_error(&self) -> Option<&E> helpers.

Copy link
Owner Author

Choose a reason for hiding this comment

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

I think I implemented these utility functions in my new commit

Move internal stuff into CacheCommunicationError
Wow, they don't clash because there's no generics in here, nice
src/cache_api.rs Outdated Show resolved Hide resolved
Co-authored-by: Zoey <Dessix@Dessix.net>
@ByteAlex ByteAlex merged commit 734bb54 into master Jun 3, 2021
@ByteAlex ByteAlex deleted the generic_errors branch August 23, 2021 03:14
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.

StdError is not implemented for CacheLoadingError
2 participants