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

HTTP Error cleanup #258

Merged
merged 14 commits into from
Oct 8, 2021
Merged

HTTP Error cleanup #258

merged 14 commits into from
Oct 8, 2021

Conversation

marioortizmanero
Copy link
Collaborator

@marioortizmanero marioortizmanero commented Sep 25, 2021

Description

This cleans up the errors for rspotify-http, according to #137.

I would say the rest of the error types are OK, so this closes #137.

Motivation and Context

The current error type for http tries to wrap up all the errors from the clients we support under the same interface. However, it's a mess because:

  1. It mixes multiple kinds of errors for different clients; HttpError::Io is only needed for ureq
  2. It serializes only specific kinds of errors. Why is there a variant for RateLimited or Unauthorized but it's StatusCode for the rest?
  3. Aren't the Api and StatusCode variants the same?

So this PR attempts to fix it by simply having a variant of error for each client supported by rspotify-http. It's as easy as delegating error handling to the HTTP client. If the user has enabled ureq, then they should deal with ureq::Error instead of some wrapper of our own that might not have everything they need.

This way, error handling is complete. There is nothing of functionality we miss by wrapping all of them under the same interface. And we also don't have to maintain such a complex wrapper ourselves.

I remember that @hrkfdn pointed out that the RateLimited variant was a must for him in order to retry a request. Well, this design still makes it possible to do so. It's just dependent on what HTTP client he's using. If he uses reqwest then it should be something like:

match request(...) {
    ClientError::Http(HttpError::StatusCode(response)) => {
        // This is a reqwest::Response with full information about the error
        let timeout = response
            .headers()
            .get(reqwest::header::RETRY_AFTER)
            .and_then(|header| header.to_str().ok())
            .and_then(|duration| duration.parse().ok());
        time::sleep(timeout);
    }
    _ => { todo!() }
}

And if someone wanted to get the API error from Spotify:

match request(...) {
    ClientError::Http(HttpError::StatusCode(response)) => {
        let api_err = response
            .json::<rspotify::model::ApiError>()?;
    }
    _ => { todo!() }
}

Dependencies

None

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • 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?

The CI still passes

rspotify-http/src/reqwest.rs Show resolved Hide resolved
if response.status().is_success() {
response.text().await.map_err(Into::into)
} else {
Err(HttpError::from_response(response).await)
Err(ReqwestError::StatusCode(response))
Copy link
Owner

Choose a reason for hiding this comment

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

It seems you don't show how to construct the ReqwestError::Client()? When will then client return this error type?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's just the same as reqwest::Error, so whenever that happens. For example when making the request or when converting the reply into a String.

rspotify-http/src/ureq.rs Show resolved Hide resolved
rspotify-http/src/ureq.rs Outdated Show resolved Hide resolved
rspotify-http/src/ureq.rs Show resolved Hide resolved
src/lib.rs Show resolved Hide resolved
@marioortizmanero
Copy link
Collaborator Author

Let me know if you like the updated documentation! This can be merged if you're ok with it.

@@ -14,6 +14,8 @@ keywords = ["spotify", "api"]
edition = "2018"

[dependencies]
rspotify-model = { path = "../rspotify-model", version = "0.10.0" }
Copy link
Owner

Choose a reason for hiding this comment

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

Is it necessary to include rspotify-model?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, because it's used in examples in the documentation

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh sorry! it should be in dev-dependencies. Fixed.

@ramsayleung
Copy link
Owner

The CI flow failed :)

@marioortizmanero
Copy link
Collaborator Author

Fixed!

@ramsayleung ramsayleung merged commit 8c99e46 into master Oct 8, 2021
@ramsayleung ramsayleung deleted the errors branch October 8, 2021 14:33
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.

Clean up and re-structure the errors
2 participants