#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum TesseraError {
#[error("{0}")]
Configuration(String),
#[error("{0}")]
InvalidArgument(String),
#[error("network error contacting Tessera: {0}")]
Network(String),
#[error("{0}")]
UnexpectedResponse(String),
#[error(
"A presigned download URL was rejected (likely expired). Presigned URLs are short-lived — call read()/scan() again to mint fresh ones."
)]
PresignExpired,
#[error("{message}")]
BadRequest {
code: Option<String>,
message: String,
},
#[error("{message}")]
Authentication {
code: Option<String>,
message: String,
},
#[error("{message}")]
Forbidden {
code: Option<String>,
message: String,
},
#[error("{message}")]
NotFound {
code: Option<String>,
message: String,
},
#[error("{message}")]
ServiceUnavailable {
code: Option<String>,
message: String,
},
#[error("{message}")]
InternalServer {
code: Option<String>,
message: String,
},
#[error("{message}")]
Api {
status_code: u16,
code: Option<String>,
message: String,
},
}
impl TesseraError {
#[must_use]
pub fn status_code(&self) -> Option<u16> {
match self {
Self::BadRequest { .. } => Some(400),
Self::Authentication { .. } => Some(401),
Self::Forbidden { .. } => Some(403),
Self::NotFound { .. } => Some(404),
Self::ServiceUnavailable { .. } => Some(503),
Self::InternalServer { .. } => Some(500),
Self::Api { status_code, .. } => Some(*status_code),
Self::Configuration(_)
| Self::InvalidArgument(_)
| Self::Network(_)
| Self::UnexpectedResponse(_)
| Self::PresignExpired => None,
}
}
#[must_use]
pub fn code(&self) -> Option<&str> {
match self {
Self::BadRequest { code, .. }
| Self::Authentication { code, .. }
| Self::Forbidden { code, .. }
| Self::NotFound { code, .. }
| Self::ServiceUnavailable { code, .. }
| Self::InternalServer { code, .. }
| Self::Api { code, .. } => code.as_deref(),
Self::Configuration(_)
| Self::InvalidArgument(_)
| Self::Network(_)
| Self::UnexpectedResponse(_)
| Self::PresignExpired => None,
}
}
}
pub fn error_from_response(status_code: u16, body: &[u8]) -> TesseraError {
let code: Option<String> = serde_json::from_slice::<serde_json::Value>(body)
.ok()
.and_then(|parsed| {
parsed
.get("error")
.and_then(serde_json::Value::as_str)
.map(ToOwned::to_owned)
});
let detail = code
.as_ref()
.map_or_else(String::new, |c| format!(" ({c})"));
let message = format!("Tessera API request failed with HTTP {status_code}{detail}");
if let Some(c) = &code {
return match c.as_str() {
"bad_request" => TesseraError::BadRequest {
code: Some(c.clone()),
message,
},
"unauthorized" => TesseraError::Authentication {
code: Some(c.clone()),
message,
},
"forbidden" => TesseraError::Forbidden {
code: Some(c.clone()),
message,
},
"not_found" => TesseraError::NotFound {
code: Some(c.clone()),
message,
},
"unavailable" => TesseraError::ServiceUnavailable {
code: Some(c.clone()),
message,
},
"internal" => TesseraError::InternalServer {
code: Some(c.clone()),
message,
},
_ => TesseraError::Api {
status_code,
code: Some(c.clone()),
message,
},
};
}
match status_code {
400 => TesseraError::BadRequest {
code: None,
message,
},
401 => TesseraError::Authentication {
code: None,
message,
},
403 => TesseraError::Forbidden {
code: None,
message,
},
404 => TesseraError::NotFound {
code: None,
message,
},
500 => TesseraError::InternalServer {
code: None,
message,
},
502..=504 => TesseraError::ServiceUnavailable {
code: None,
message,
},
_ => TesseraError::Api {
status_code,
code: None,
message,
},
}
}