Skip to content

Commit

Permalink
Replace map_err() conversions with a From call via the Try oper…
Browse files Browse the repository at this point in the history
…ator

The `?` or `Try` operator in the standard library calls `.into()` on the
`Error` type before bubbling it up, allowing for natural conversions to
other error types.  `thiserror` supports marking such errors as `#[from]`
(implying `#[source]`) to generate the necessary `From<>` implementation
to facilitate automatic conversion from any specified `Error` type to the
corresponding enum variant in our `thiserror` enums.
  • Loading branch information
MarijnS95 committed Nov 28, 2023
1 parent 335bfd2 commit 85c70cb
Showing 1 changed file with 21 additions and 45 deletions.
66 changes: 21 additions & 45 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1309,9 +1309,7 @@ where
F: FnOnce(HttpRequest) -> Result<HttpResponse, RE>,
RE: Error + 'static,
{
http_client(self.prepare_request()?)
.map_err(RequestTokenError::Request)
.and_then(endpoint_response)
endpoint_response(http_client(self.prepare_request()?)?)
}

///
Expand All @@ -1327,9 +1325,7 @@ where
RE: Error + 'static,
{
let http_request = self.prepare_request()?;
let http_response = http_client(http_request)
.await
.map_err(RequestTokenError::Request)?;
let http_response = http_client(http_request).await?;
endpoint_response(http_response)
}
}
Expand Down Expand Up @@ -1412,9 +1408,7 @@ where
F: FnOnce(HttpRequest) -> Result<HttpResponse, RE>,
RE: Error + 'static,
{
http_client(self.prepare_request()?)
.map_err(RequestTokenError::Request)
.and_then(endpoint_response)
endpoint_response(http_client(self.prepare_request()?)?)
}
///
/// Asynchronously sends the request to the authorization server and awaits a response.
Expand All @@ -1429,9 +1423,7 @@ where
RE: Error + 'static,
{
let http_request = self.prepare_request()?;
let http_response = http_client(http_request)
.await
.map_err(RequestTokenError::Request)?;
let http_response = http_client(http_request).await?;
endpoint_response(http_response)
}

Expand Down Expand Up @@ -1536,9 +1528,7 @@ where
F: FnOnce(HttpRequest) -> Result<HttpResponse, RE>,
RE: Error + 'static,
{
http_client(self.prepare_request()?)
.map_err(RequestTokenError::Request)
.and_then(endpoint_response)
endpoint_response(http_client(self.prepare_request()?)?)
}

///
Expand All @@ -1554,9 +1544,7 @@ where
RE: Error + 'static,
{
let http_request = self.prepare_request()?;
let http_response = http_client(http_request)
.await
.map_err(RequestTokenError::Request)?;
let http_response = http_client(http_request).await?;
endpoint_response(http_response)
}

Expand Down Expand Up @@ -1660,9 +1648,7 @@ where
F: FnOnce(HttpRequest) -> Result<HttpResponse, RE>,
RE: Error + 'static,
{
http_client(self.prepare_request()?)
.map_err(RequestTokenError::Request)
.and_then(endpoint_response)
endpoint_response(http_client(self.prepare_request()?)?)
}

///
Expand All @@ -1678,9 +1664,7 @@ where
RE: Error + 'static,
{
let http_request = self.prepare_request()?;
let http_response = http_client(http_request)
.await
.map_err(RequestTokenError::Request)?;
let http_response = http_client(http_request).await?;
endpoint_response(http_response)
}

Expand Down Expand Up @@ -1810,9 +1794,7 @@ where
F: FnOnce(HttpRequest) -> Result<HttpResponse, RE>,
RE: Error + 'static,
{
http_client(self.prepare_request()?)
.map_err(RequestTokenError::Request)
.and_then(endpoint_response)
endpoint_response(http_client(self.prepare_request()?)?)
}

///
Expand All @@ -1828,9 +1810,7 @@ where
RE: Error + 'static,
{
let http_request = self.prepare_request()?;
let http_response = http_client(http_request)
.await
.map_err(RequestTokenError::Request)?;
let http_response = http_client(http_request).await?;
endpoint_response(http_response)
}
}
Expand Down Expand Up @@ -1923,9 +1903,7 @@ where
// From https://tools.ietf.org/html/rfc7009#section-2.2:
// "The content of the response body is ignored by the client as all
// necessary information is conveyed in the response code."
http_client(self.prepare_request()?)
.map_err(RequestTokenError::Request)
.and_then(endpoint_response_status_only)
endpoint_response_status_only(http_client(self.prepare_request()?)?)
}

///
Expand All @@ -1941,9 +1919,7 @@ where
RE: Error + 'static,
{
let http_request = self.prepare_request()?;
let http_response = http_client(http_request)
.await
.map_err(RequestTokenError::Request)?;
let http_response = http_client(http_request).await?;
endpoint_response_status_only(http_response)
}
}
Expand Down Expand Up @@ -2224,9 +2200,7 @@ where
RE: Error + 'static,
EF: ExtraDeviceAuthorizationFields,
{
http_client(self.prepare_request()?)
.map_err(RequestTokenError::Request)
.and_then(endpoint_response)
endpoint_response(http_client(self.prepare_request()?)?)
}

///
Expand All @@ -2243,9 +2217,7 @@ where
EF: ExtraDeviceAuthorizationFields,
{
let http_request = self.prepare_request()?;
let http_response = http_client(http_request)
.await
.map_err(RequestTokenError::Request)?;
let http_response = http_client(http_request).await?;
endpoint_response(http_response)
}
}
Expand Down Expand Up @@ -2504,8 +2476,12 @@ where
// use that, otherwise use the value given by the device authorization
// response.
let timeout_dur = timeout.unwrap_or_else(|| self.dev_auth_resp.expires_in());
let chrono_timeout = chrono::Duration::from_std(timeout_dur)
.map_err(|_| RequestTokenError::Other("Failed to convert duration".to_string()))?;
let chrono_timeout = chrono::Duration::from_std(timeout_dur).map_err(|e| {
RequestTokenError::Other(format!(
"Failed to convert `{:?}` to `chrono::Duration`: {}",
timeout_dur, e
))
})?;

// Calculate the DateTime at which the request times out.
let timeout_dt = (*self.time_fn)()
Expand Down Expand Up @@ -3179,7 +3155,7 @@ where
/// connectivity failed).
///
#[error("Request failed")]
Request(#[source] RE),
Request(#[from] RE),
///
/// Failed to parse server response. Parse errors may occur while parsing either successful
/// or error responses.
Expand Down

0 comments on commit 85c70cb

Please sign in to comment.