diff --git a/gha-cache/src/api.rs b/gha-cache/src/api.rs index 5cce11d..4d52f66 100644 --- a/gha-cache/src/api.rs +++ b/gha-cache/src/api.rs @@ -2,6 +2,8 @@ //! //! We expose a high-level API that deals with "files." +use std::fmt; + use async_trait::async_trait; use bytes::{Bytes, BytesMut}; use rand::{distributions::Alphanumeric, Rng}; @@ -40,26 +42,26 @@ type Result = std::result::Result; /// An API error. #[derive(Error, Debug)] pub enum Error { - #[error("Failed to initialize the client")] + #[error("Failed to initialize the client: {0}")] InitError(Box), - #[error("Request error")] + #[error("Request error: {0}")] RequestError(#[from] reqwest::Error), // TODO: Better errors - #[error("Failed to decode response")] + #[error("Failed to decode response ({status}): {error}")] DecodeError { status: StatusCode, bytes: Bytes, error: serde_json::Error, }, - #[error("API error")] + #[error("API error ({status}): {info}")] ApiError { status: StatusCode, info: ApiErrorInfo, }, - #[error("I/O error")] + #[error("I/O error: {0}")] IoError(#[from] std::io::Error), #[error("Too many collisions")] @@ -195,6 +197,19 @@ impl Error { } } +impl fmt::Display for ApiErrorInfo { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Unstructured(bytes) => { + write!(f, "[Unstructured] {}", String::from_utf8_lossy(bytes)) + } + Self::Structured(e) => { + write!(f, "{:?}", e) + } + } + } +} + impl Api { pub fn new(credentials: Credentials) -> Result { let mut headers = HeaderMap::new(); diff --git a/nix-actions-cache/src/error.rs b/nix-actions-cache/src/error.rs index 28b91b7..cf9a423 100644 --- a/nix-actions-cache/src/error.rs +++ b/nix-actions-cache/src/error.rs @@ -12,7 +12,7 @@ pub type Result = std::result::Result; #[derive(Error, Debug)] pub enum Error { - #[error("GitHub API error")] + #[error("GitHub API error: {0}")] ApiError(#[from] ApiError), #[error("Not Found")]