-
Notifications
You must be signed in to change notification settings - Fork 124
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
Remove ModelError
in favour of strongly-typed errors
#327
Conversation
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 love the idea, thanks for submitting a PR.
- Why not use
#[derive(Error)]
instead of implementing them manually, though? We already have the dependency anyway. - Shouldn't
ReadTokenCacheError
and etc be a variant inClientError
?
@@ -93,7 +94,7 @@ impl ClientCredsSpotify { | |||
/// * The read token is expired | |||
/// * The cached token is disabled in the config | |||
#[maybe_async] | |||
pub async fn read_token_cache(&self) -> ClientResult<Option<Token>> { | |||
pub async fn read_token_cache(&self) -> Result<Option<Token>, ReadTokenCacheError> { |
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 change can be avoided by making this error part of ClientError
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.
But then all code that deals in ClientError
s would now have one more variant to think about even though it can’t actually be created from most code, and similarly any code that calls this function now has to deal with every variant of ClientError
even though only one actually can be created.
I suppose it would technically fit ClientError
’s stated purpose to use it here, but the API can be made more strongly typed by not using it I think.
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.
Fair enough. My reasoning was that if ReadTokenCacheError
isn't in ClientError
, then if someone used to return a ClientError
, they will now have to define their own custom error type for both. But I guess that's not really a huge issue, as most libraries will have their own error anyway?
|
||
/// An error writing a [`Token`] to its cache. | ||
#[derive(Debug, Error)] | ||
#[non_exhaustive] |
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.
Why does this use non_exhaustive
, for future-proofing? And why not a tuple struct?
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.
Could’ve been a tuple struct I suppose — should I change it?
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'd say we don't really need to future-proof anything here? Not sure what else we would need to add into the error.
8a92b72
to
48a3e6d
Compare
Message to comment on stale PRs. If none provided, will not mark PRs stale |
Description
ModelError
ReadTokenCacheError
andWriteTokenCacheError
ReadFileError
andWriteFileError
ClientError::Model
with the more explicit and usefulClientError::UpdateTokenCache
ClientError::CacheFile
, which seems to have been left in by accidentMotivation and Context
Strongly-typed errors are better than weakly typed errors because:
For example, printing the backtrace of one of the new error types might give this:
Which is extremely helpful to the user.
Dependencies
None.
Type of change
Please delete options that are not relevant.
How has this been tested?
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce.
Please also list any relevant details for your test configuration
cargo test --features env-file