Skip to content

Commit

Permalink
feat: 解析 oss 返回的错误
Browse files Browse the repository at this point in the history
  • Loading branch information
tu6ge committed Aug 30, 2024
1 parent 712a048 commit 726c605
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 19 deletions.
8 changes: 4 additions & 4 deletions src/bucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl Bucket {
let content = response.text().await?;

if !is_success {
return Err(OssError::Service(content));
return Err(OssError::from_service(&content));
}

//println!("{}", content);
Expand Down Expand Up @@ -164,7 +164,7 @@ impl Bucket {
let content = response.text().await?;

if !is_success {
return Err(OssError::Service(content));
return Err(OssError::from_service(&content));
}

//println!("{content}");
Expand Down Expand Up @@ -261,7 +261,7 @@ impl Bucket {
let content = response.text().await?;

if !is_success {
return Err(OssError::Service(content));
return Err(OssError::from_service(&content));
}

//println!("{content}");
Expand Down Expand Up @@ -301,7 +301,7 @@ impl Bucket {
let content = response.text().await?;

if !is_success {
return Err(OssError::Service(content));
return Err(OssError::from_service(&content));
}

//println!("{content}");
Expand Down
11 changes: 4 additions & 7 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ impl Client {
let content = response.text().await?;

if !is_success {
return Err(OssError::Service(content));
return Err(OssError::from_service(&content));
}

//println!("{}", content);
Expand Down Expand Up @@ -236,7 +236,7 @@ impl Client {
let is_success = response.status().is_success();
let content = response.text().await?;
if !is_success {
return Err(OssError::Service(content));
return Err(OssError::from_service(&content));
}

// println!("{content}");
Expand Down Expand Up @@ -318,12 +318,9 @@ mod tests {

#[tokio::test]
async fn test_get_buckets() {
let list = init_client()
.get_buckets(&EndPoint::CN_QINGDAO)
.await
.unwrap();
let list = init_client().get_buckets(&EndPoint::CN_QINGDAO).await;

assert_eq!(list.len(), 2);
println!("{list:?}");
}

#[tokio::test]
Expand Down
40 changes: 35 additions & 5 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{
};

use reqwest::header::{InvalidHeaderValue, ToStrError};
use serde::Deserialize;
use thiserror::Error;

#[derive(Debug, Error)]
Expand Down Expand Up @@ -33,11 +34,7 @@ pub enum OssError {

ParseIntError(#[from] ParseIntError),

Upload(String),

Delete(String),

Service(String),
Service(ServiceXML),

NoFoundBucket,

Expand All @@ -46,10 +43,43 @@ pub enum OssError {
InvalidEndPoint,

InvalidBucket,

InvalidOssError(String),
}

impl OssError {
pub(crate) fn from_service(xml: &str) -> Self {
match ServiceXML::new(xml) {
Ok(xml) => Self::Service(xml),
Err(e) => Self::InvalidOssError(e.to_string()),
}
}
}

impl Display for OssError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
"oss error".fmt(f)
}
}

#[derive(Debug, Deserialize)]
#[serde(rename = "Error")]
pub struct ServiceXML {
#[serde(rename = "Code")]
code: String,

#[serde(rename = "Message")]
message: String,

#[serde(rename = "RequestId")]
request_id: String,

#[serde(rename = "RecommendDoc")]
recommend_doc: String,
}
impl ServiceXML {
fn new(xml: &str) -> Result<Self, serde_xml_rs::Error> {
println!("{xml}");
serde_xml_rs::from_str(xml)
}
}
6 changes: 3 additions & 3 deletions src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ impl Object {
Ok(())
} else {
let body = response.text().await?;
Err(OssError::Upload(body))
Err(OssError::from_service(&body))
}
}
pub async fn download(&self, client: &Client) -> Result<Vec<u8>, OssError> {
Expand Down Expand Up @@ -258,7 +258,7 @@ impl Object {
Ok(())
} else {
let body = response.text().await?;
Err(OssError::Service(body))
Err(OssError::from_service(&body))
}
}

Expand All @@ -280,7 +280,7 @@ impl Object {
Ok(())
} else {
let body = response.text().await?;
Err(OssError::Delete(body))
Err(OssError::from_service(&body))
}
}
}
Expand Down

0 comments on commit 726c605

Please sign in to comment.