add helper functions to Error and ResponseValue (#28)

This commit is contained in:
Adam Leventhal 2022-02-23 21:07:30 -08:00 committed by GitHub
parent 24aa57a243
commit 39b1d9107d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 4 deletions

View File

@ -23,7 +23,7 @@ impl Client {
``` ```
For more info on the `ResponseValue<T>` and `Error<E>` types, see For more info on the `ResponseValue<T>` and `Error<E>` types, see
[progenitor_client](./progenitor_client). [progenitor_client](./progenitor-client.md).
Note that methods are `async` so must be `await`ed to get the response. Note that methods are `async` so must be `await`ed to get the response.

View File

@ -56,9 +56,10 @@ impl<T> ResponseValue<T> {
} }
/// Get the status from this response. /// Get the status from this response.
pub fn status(&self) -> &reqwest::StatusCode { pub fn status(&self) -> reqwest::StatusCode {
&self.status self.status
} }
/// Get the headers from this response. /// Get the headers from this response.
pub fn headers(&self) -> &reqwest::header::HeaderMap { pub fn headers(&self) -> &reqwest::header::HeaderMap {
&self.headers &self.headers
@ -112,7 +113,7 @@ impl<T: std::fmt::Debug> std::fmt::Debug for ResponseValue<T> {
/// or an enum if there are multiple valid error types. It can be the unit type /// or an enum if there are multiple valid error types. It can be the unit type
/// if there are no structured returns expected. /// if there are no structured returns expected.
#[derive(Debug)] #[derive(Debug)]
pub enum Error<E: std::fmt::Debug> { pub enum Error<E: std::fmt::Debug = ()> {
/// A server error either with the data, or with the connection. /// A server error either with the data, or with the connection.
CommunicationError(reqwest::Error), CommunicationError(reqwest::Error),
@ -128,6 +129,39 @@ pub enum Error<E: std::fmt::Debug> {
UnexpectedResponse(reqwest::Response), UnexpectedResponse(reqwest::Response),
} }
impl<E: std::fmt::Debug> Error<E> {
/// Returns the status code, if the error was generated from a response.
pub fn status(&self) -> Option<reqwest::StatusCode> {
match self {
Error::CommunicationError(e) => e.status(),
Error::ErrorResponse(rv) => Some(rv.status()),
Error::InvalidResponsePayload(e) => e.status(),
Error::UnexpectedResponse(r) => Some(r.status()),
}
}
/// Convert this error into one without a typed body for unified error
/// handling with APIs that distinguish various error response bodies.
pub fn into_untyped(self) -> Error {
match self {
Error::CommunicationError(e) => Error::CommunicationError(e),
Error::ErrorResponse(ResponseValue {
inner: _,
status,
headers,
}) => Error::ErrorResponse(ResponseValue {
inner: (),
status,
headers,
}),
Error::InvalidResponsePayload(e) => {
Error::InvalidResponsePayload(e)
}
Error::UnexpectedResponse(r) => Error::UnexpectedResponse(r),
}
}
}
impl<E: std::fmt::Debug> From<reqwest::Error> for Error<E> { impl<E: std::fmt::Debug> From<reqwest::Error> for Error<E> {
fn from(e: reqwest::Error) -> Self { fn from(e: reqwest::Error) -> Self {
Self::CommunicationError(e) Self::CommunicationError(e)