kordophone: better handling of url decoding errors
This commit is contained in:
@@ -49,6 +49,7 @@ pub enum Error {
|
||||
SerdeError(serde_json::Error),
|
||||
DecodeError(String),
|
||||
PongError(tungstenite::Error),
|
||||
URLError,
|
||||
Unauthorized,
|
||||
}
|
||||
|
||||
@@ -392,7 +393,8 @@ impl<K: AuthenticationStore + Send + Sync> APIInterface for HTTPAPIClient<K> {
|
||||
None => "updates".to_string(),
|
||||
};
|
||||
|
||||
let uri = self.uri_for_endpoint(&endpoint, Some(self.websocket_scheme()));
|
||||
let uri = self
|
||||
.uri_for_endpoint(&endpoint, Some(self.websocket_scheme()))?;
|
||||
|
||||
log::debug!("Connecting to websocket: {:?}", uri);
|
||||
|
||||
@@ -463,17 +465,23 @@ impl<K: AuthenticationStore + Send + Sync> HTTPAPIClient<K> {
|
||||
}
|
||||
}
|
||||
|
||||
fn uri_for_endpoint(&self, endpoint: &str, scheme: Option<&str>) -> Uri {
|
||||
fn uri_for_endpoint(&self, endpoint: &str, scheme: Option<&str>) -> Result<Uri, Error> {
|
||||
let mut parts = self.base_url.clone().into_parts();
|
||||
let root_path: PathBuf = parts.path_and_query.unwrap().path().into();
|
||||
let root_path: PathBuf = parts
|
||||
.path_and_query
|
||||
.ok_or(Error::URLError)?
|
||||
.path()
|
||||
.into();
|
||||
|
||||
let path = root_path.join(endpoint);
|
||||
parts.path_and_query = Some(path.to_str().unwrap().parse().unwrap());
|
||||
let path_str = path.to_str().ok_or(Error::URLError)?;
|
||||
parts.path_and_query = Some(path_str.parse().map_err(|_| Error::URLError)?);
|
||||
|
||||
if let Some(scheme) = scheme {
|
||||
parts.scheme = Some(scheme.parse().unwrap());
|
||||
parts.scheme = Some(scheme.parse().map_err(|_| Error::URLError)?);
|
||||
}
|
||||
|
||||
Uri::try_from(parts).unwrap()
|
||||
Uri::try_from(parts).map_err(|_| Error::URLError)
|
||||
}
|
||||
|
||||
fn websocket_scheme(&self) -> &str {
|
||||
@@ -547,14 +555,14 @@ impl<K: AuthenticationStore + Send + Sync> HTTPAPIClient<K> {
|
||||
) -> Result<hyper::Response<Body>, Error> {
|
||||
use hyper::StatusCode;
|
||||
|
||||
let uri = self.uri_for_endpoint(endpoint, None);
|
||||
let uri = self.uri_for_endpoint(endpoint, None)?;
|
||||
log::debug!("Requesting {:?} {:?}", method, uri);
|
||||
|
||||
let mut build_request = |auth: &Option<String>| {
|
||||
let body = body_fn();
|
||||
Request::builder()
|
||||
.method(&method)
|
||||
.uri(&uri)
|
||||
.uri(uri.clone())
|
||||
.with_auth_string(auth)
|
||||
.body(body)
|
||||
.expect("Unable to build request")
|
||||
|
||||
Reference in New Issue
Block a user