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

refactor(http)!: cleanup ApiError #1737

Open
wants to merge 6 commits into
base: old-next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
255 changes: 0 additions & 255 deletions http/src/api_error.rs

This file was deleted.

56 changes: 54 additions & 2 deletions http/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{api_error::ApiError, json::JsonError, response::StatusCode};
use crate::{json::JsonError, response::StatusCode};
use hyper::{Body, Response};
use serde::{Deserialize, Serialize};
use std::{
error::Error as StdError,
fmt::{Debug, Display, Formatter, Result as FmtResult},
Expand Down Expand Up @@ -110,7 +111,7 @@ pub enum ErrorType {
RequestTimedOut,
Response {
body: Vec<u8>,
error: ApiError,
ratelimit: Option<Ratelimit>,
status: StatusCode,
},
/// API service is unavailable. Consider re-sending the request at a
Expand All @@ -126,3 +127,54 @@ pub enum ErrorType {
/// or is revoked. Recreate the client to configure a new token.
Unauthorized,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
vilgotf marked this conversation as resolved.
Show resolved Hide resolved
#[non_exhaustive]
pub struct Ratelimit {
/// Whether the ratelimit is global.
pub global: bool,
/// Amount of time to wait before retrying.
pub retry_after: f64,
}

impl Display for Ratelimit {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
if self.global {
f.write_str("globally ")?;
}

f.write_str("ratelimited for ")?;
Display::fmt(&self.retry_after, f)?;

f.write_str("s")
}
}

#[cfg(test)]
mod tests {
use super::Ratelimit;
use serde_test::Token;

#[test]
fn test_ratelimited_api_error() {
let expected = Ratelimit {
global: true,
retry_after: 6.457,
};

serde_test::assert_ser_tokens(
&expected,
&[
Token::Struct {
name: "Ratelimit",
len: 2,
},
Token::Str("global"),
Token::Bool(true),
Token::Str("retry_after"),
Token::F64(6.457),
Token::StructEnd,
],
);
}
}
1 change: 0 additions & 1 deletion http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@
clippy::unnecessary_wraps
)]

pub mod api_error;
pub mod client;
pub mod error;
pub mod request;
Expand Down
41 changes: 13 additions & 28 deletions http/src/response/future.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use super::{Response, StatusCode};
use crate::{
api_error::ApiError,
error::{Error, ErrorType},
};
use crate::error::{Error, ErrorType};
use hyper::{client::ResponseFuture as HyperResponseFuture, StatusCode as HyperStatusCode};
use std::{
future::Future,
Expand Down Expand Up @@ -35,30 +32,18 @@ struct Chunking {

impl Chunking {
fn poll<T>(mut self, cx: &mut Context<'_>) -> InnerPoll<T> {
let bytes = match Pin::new(&mut self.future).poll(cx) {
Poll::Ready(Ok(bytes)) => bytes,
Poll::Ready(Err(source)) => return InnerPoll::Ready(Err(source)),
Poll::Pending => return InnerPoll::Pending(ResponseFutureStage::Chunking(self)),
};

let error = match crate::json::from_bytes::<ApiError>(&bytes) {
Ok(error) => error,
Err(source) => {
return InnerPoll::Ready(Err(Error {
kind: ErrorType::Parsing { body: bytes },
source: Some(Box::new(source)),
}));
}
};

InnerPoll::Ready(Err(Error {
kind: ErrorType::Response {
body: bytes,
error,
status: StatusCode::new(self.status.as_u16()),
},
source: None,
}))
match Pin::new(&mut self.future).poll(cx) {
Poll::Ready(Ok(bytes)) => InnerPoll::Ready(Err(Error {
kind: ErrorType::Response {
ratelimit: crate::json::from_bytes(&bytes).ok(),
body: bytes,
status: StatusCode::new(self.status.as_u16()),
},
source: None,
})),
Poll::Ready(Err(source)) => InnerPoll::Ready(Err(source)),
Poll::Pending => InnerPoll::Pending(ResponseFutureStage::Chunking(self)),
}
}
}

Expand Down