2020-02-21 15:54:05 +00:00
|
|
|
// Copyright 2020 Damir Jelić
|
|
|
|
// Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-10-20 11:56:46 +00:00
|
|
|
//! Error conditions.
|
|
|
|
|
|
|
|
use std::error::Error as StdError;
|
|
|
|
use std::fmt::{Display, Formatter, Result as FmtResult};
|
|
|
|
|
|
|
|
use reqwest::Error as ReqwestError;
|
2020-02-21 15:33:08 +00:00
|
|
|
use ruma_api::error::FromHttpResponseError as RumaResponseError;
|
|
|
|
use ruma_api::error::IntoHttpError as RumaIntoHttpError;
|
2019-11-10 17:33:06 +00:00
|
|
|
use url::ParseError;
|
2019-10-20 11:56:46 +00:00
|
|
|
|
|
|
|
/// An error that can occur during client operations.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Error(pub(crate) InnerError);
|
|
|
|
|
|
|
|
impl Display for Error {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> FmtResult {
|
|
|
|
let message = match self.0 {
|
|
|
|
InnerError::AuthenticationRequired => "The queried endpoint requires authentication but was called with an anonymous client.",
|
|
|
|
InnerError::Reqwest(_) => "An HTTP error occurred.",
|
|
|
|
InnerError::Uri(_) => "Provided string could not be converted into a URI.",
|
2020-02-21 15:33:08 +00:00
|
|
|
InnerError::RumaResponseError(_) => "An error occurred converting between ruma_client_api and hyper types.",
|
|
|
|
InnerError::IntoHttpError(_) => "An error occurred converting between ruma_client_api and hyper types.",
|
2019-10-20 11:56:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
write!(f, "{}", message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl StdError for Error {}
|
|
|
|
|
|
|
|
/// Internal representation of errors.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub(crate) enum InnerError {
|
|
|
|
/// Queried endpoint requires authentication but was called on an anonymous client.
|
|
|
|
AuthenticationRequired,
|
|
|
|
/// An error at the HTTP layer.
|
|
|
|
Reqwest(ReqwestError),
|
|
|
|
/// An error when parsing a string as a URI.
|
2019-10-24 20:34:58 +00:00
|
|
|
Uri(ParseError),
|
2019-10-20 11:56:46 +00:00
|
|
|
/// An error converting between ruma_client_api types and Hyper types.
|
2020-02-21 15:33:08 +00:00
|
|
|
RumaResponseError(RumaResponseError),
|
|
|
|
/// An error converting between ruma_client_api types and Hyper types.
|
|
|
|
IntoHttpError(RumaIntoHttpError),
|
2019-10-20 11:56:46 +00:00
|
|
|
}
|
|
|
|
|
2019-10-24 20:34:58 +00:00
|
|
|
impl From<ParseError> for Error {
|
|
|
|
fn from(error: ParseError) -> Self {
|
2019-10-20 11:56:46 +00:00
|
|
|
Self(InnerError::Uri(error))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-21 15:33:08 +00:00
|
|
|
impl From<RumaResponseError> for Error {
|
|
|
|
fn from(error: RumaResponseError) -> Self {
|
|
|
|
Self(InnerError::RumaResponseError(error))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<RumaIntoHttpError> for Error {
|
|
|
|
fn from(error: RumaIntoHttpError) -> Self {
|
|
|
|
Self(InnerError::IntoHttpError(error))
|
2019-10-20 11:56:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ReqwestError> for Error {
|
|
|
|
fn from(error: ReqwestError) -> Self {
|
|
|
|
Self(InnerError::Reqwest(error))
|
|
|
|
}
|
|
|
|
}
|