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

Remove ModelError in favour of strongly-typed errors #327

Closed

Conversation

SabrinaJewson
Copy link
Contributor

Description

  • Removed ModelError
  • Added ReadTokenCacheError and WriteTokenCacheError
  • To support the above two types, added ReadFileError and WriteFileError
  • Replaced ClientError::Model with the more explicit and useful ClientError::UpdateTokenCache
  • Also removed ClientError::CacheFile, which seems to have been left in by accident

Motivation and Context

Strongly-typed errors are better than weakly typed errors because:

  • They make it easier for the user to see exactly what can go wrong through just types
  • They give better error messages

For example, printing the backtrace of one of the new error types might give this:

Error: failed to read token from cache

Caused by:
0: failed to read file token_cache.json
1: No such file or directory (os error 2)

Which is extremely helpful to the user.

Dependencies

None.

Type of change

Please delete options that are not relevant.

  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

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

  • Test A: cargo test --features env-file

Copy link
Collaborator

@marioortizmanero marioortizmanero left a 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 in ClientError?

@@ -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> {
Copy link
Collaborator

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

Copy link
Contributor Author

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 ClientErrors 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.

Copy link
Collaborator

@marioortizmanero marioortizmanero Jun 15, 2022

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?

src/clients/oauth.rs Show resolved Hide resolved

/// An error writing a [`Token`] to its cache.
#[derive(Debug, Error)]
#[non_exhaustive]
Copy link
Collaborator

@marioortizmanero marioortizmanero Jun 15, 2022

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?

Copy link
Contributor Author

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?

Copy link
Collaborator

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.

@github-actions
Copy link

Message to comment on stale PRs. If none provided, will not mark PRs stale

@github-actions github-actions bot added the Stale label Jun 25, 2023
@github-actions github-actions bot closed this Jul 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants