use spiral_contract::error::{ErrorEnvelope, new_error_instance_id};
pub use super::api::ScandalError;
impl ScandalError {
pub fn to_wire(&self) -> ErrorEnvelope {
self.envelope(new_error_instance_id())
}
pub fn from_wire(envelope: &ErrorEnvelope) -> Self {
Self::decode_envelope(envelope).unwrap_or_else(|| Self::Internal {
message: envelope.to_string(),
})
}
pub fn message(&self) -> String {
match self {
Self::Unauthenticated { message }
| Self::InvalidArgument { message }
| Self::Conflict { message }
| Self::BudgetExceeded { message }
| Self::StorageUnavailable { message }
| Self::Unavailable { message }
| Self::Internal { message } => message.clone(),
Self::PermissionDenied {
operation,
resource,
} => format!("Permission `{operation}` denied on `{resource}`"),
Self::NotFound { resource } => format!("Resource `{resource}` not found"),
}
}
}