Skip to content

Commit 581b56d

Browse files
committed
feat(http): forward a wrapped error completely
1 parent 20b7444 commit 581b56d

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

htsget-http/src/error.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use http::StatusCode;
22
use http::header::{InvalidHeaderName, InvalidHeaderValue};
3-
use serde::Serialize;
3+
use serde::{Deserialize, Serialize};
44
use thiserror::Error;
55

66
use crate::HtsGetError::InternalError;
@@ -30,18 +30,20 @@ pub enum HtsGetError {
3030
MethodNotAllowed(String),
3131
#[error("InternalError")]
3232
InternalError(String),
33+
#[error("Wrapped")]
34+
Wrapped(WrappedHtsGetError, StatusCode),
3335
}
3436

3537
/// A helper struct implementing [serde's Serialize trait](Serialize) to allow
3638
/// easily converting HtsGetErrors to JSON
37-
#[derive(Serialize)]
39+
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
3840
pub struct JsonHtsGetError {
3941
error: String,
4042
message: String,
4143
}
4244

4345
/// The "htsget" container wrapping the actual error response above
44-
#[derive(Serialize)]
46+
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
4547
pub struct WrappedHtsGetError {
4648
htsget: JsonHtsGetError,
4749
}
@@ -60,6 +62,7 @@ impl HtsGetError {
6062
| HtsGetError::InvalidRange(err) => (err, StatusCode::BAD_REQUEST),
6163
HtsGetError::MethodNotAllowed(err) => (err, StatusCode::METHOD_NOT_ALLOWED),
6264
HtsGetError::InternalError(err) => (err, StatusCode::INTERNAL_SERVER_ERROR),
65+
HtsGetError::Wrapped(err, status) => return (err.clone(), *status),
6366
};
6467

6568
(

htsget-http/src/middleware/auth.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! The htsget authorization middleware.
22
//!
33
4-
use crate::error::Result as HtsGetResult;
4+
use crate::error::{Result as HtsGetResult, WrappedHtsGetError};
55
use crate::middleware::error::Error::AuthBuilderError;
66
use crate::middleware::error::Result;
77
use crate::{Endpoint, HtsGetError};
@@ -98,14 +98,22 @@ impl Auth {
9898
.await?;
9999
trace!("response: {:?}", response);
100100

101+
let status = response.status();
102+
103+
// Forward a valid htsget error if that's what the backend returns.
101104
let value = response.json::<Value>().await.map_err(|err| {
102105
HtsGetError::InternalError(format!("failed to fetch data from {url}: {err}"))
103106
})?;
107+
trace!("value: {}", value);
108+
104109
match serde_json::from_value::<D>(value.clone()) {
105110
Ok(response) => Ok(response),
106-
Err(_) => Err(HtsGetError::InternalError(format!(
107-
"unexpected JSON: {value}"
108-
))),
111+
Err(_) => match serde_json::from_value::<WrappedHtsGetError>(value.clone()) {
112+
Ok(err) => Err(HtsGetError::Wrapped(err, status)),
113+
Err(_) => Err(HtsGetError::InternalError(format!(
114+
"failed to fetch data from {url}: {value}"
115+
))),
116+
},
109117
}
110118
}
111119

0 commit comments

Comments
 (0)