#[derive(Debug, thiserror::Error)]
pub enum VtaError {
#[cfg(feature = "client")]
#[error("network error: {0}")]
Network(#[from] reqwest::Error),
#[error("authentication failed: {0}")]
Auth(String),
#[error("not found: {0}")]
NotFound(String),
#[error("validation error: {0}")]
Validation(String),
#[error("forbidden: {0}")]
Forbidden(String),
#[error("conflict: {0}")]
Conflict(String),
#[error("server error ({status}): {body}")]
Server { status: u16, body: String },
#[error("protocol error: {0}")]
Protocol(String),
#[error("serialization error: {0}")]
Serialization(#[from] serde_json::Error),
#[error("{0}")]
Other(String),
}
impl VtaError {
#[cfg(feature = "client")]
pub(crate) fn from_http(status: reqwest::StatusCode, body: String) -> Self {
match status.as_u16() {
401 => Self::Auth(body),
403 => Self::Forbidden(body),
404 => Self::NotFound(body),
400 | 422 => Self::Validation(body),
409 => Self::Conflict(body),
s if s >= 500 => Self::Server { status: s, body },
s => Self::Other(format!("{s}: {body}")),
}
}
pub fn is_auth(&self) -> bool {
matches!(self, Self::Auth(_) | Self::Forbidden(_))
}
pub fn is_network(&self) -> bool {
#[cfg(feature = "client")]
if matches!(self, Self::Network(_)) {
return true;
}
false
}
pub fn is_not_found(&self) -> bool {
matches!(self, Self::NotFound(_))
}
}
impl From<String> for VtaError {
fn from(s: String) -> Self {
Self::Other(s)
}
}
impl From<&str> for VtaError {
fn from(s: &str) -> Self {
Self::Other(s.to_string())
}
}
impl From<Box<dyn std::error::Error>> for VtaError {
fn from(e: Box<dyn std::error::Error>) -> Self {
Self::Other(e.to_string())
}
}
impl From<crate::credentials::CredentialBundleError> for VtaError {
fn from(e: crate::credentials::CredentialBundleError) -> Self {
Self::Validation(e.to_string())
}
}
impl From<crate::did_key::DidKeyError> for VtaError {
fn from(e: crate::did_key::DidKeyError) -> Self {
Self::Other(e.to_string())
}
}