Skip to content

Commit

Permalink
style: Define error variants instead of using unwrap()
Browse files Browse the repository at this point in the history
  • Loading branch information
vadorovsky committed Jun 18, 2024
1 parent d35739d commit e441688
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
32 changes: 25 additions & 7 deletions crates/bpf-common/src/containers/layers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,27 @@ struct Rootfs {
/// Returns a list of layer paths for the given Docker image ID.
pub(crate) async fn docker_layers(image_id: &str) -> Result<Vec<PathBuf>, ContainerError> {
let client = Client::unix();
let url = HyperlocalUri::new(DOCKER_SOCKET, &format!("/images/{}/json", image_id));

let response = client.get(url.into()).await.unwrap();
let body_bytes = body::to_bytes(response).await.unwrap();

let response: ImageInspect = serde_json::from_slice(&body_bytes).unwrap();
let uri = HyperlocalUri::new(DOCKER_SOCKET, &format!("/images/{}/json", image_id));
let uri: hyper::Uri = uri.into();

let response =
client
.get(uri.clone())
.await
.map_err(|source| ContainerError::HyperRequest {
source,
uri: uri.clone(),
})?;
let body_bytes =
body::to_bytes(response)
.await
.map_err(|source| ContainerError::HyperResponse {
source,
uri: uri.clone(),
})?;

let response: ImageInspect = serde_json::from_slice(&body_bytes)
.map_err(|source| ContainerError::ParseResponse { source, uri })?;

match response.graph_driver.name {
GraphDriverName::Btrfs => docker_btrfs_layers(image_id),
Expand Down Expand Up @@ -132,7 +147,10 @@ fn docker_btrfs_layers(image_id: &str) -> Result<Vec<PathBuf>, ContainerError> {
.map_err(|source| ContainerError::ParseConfigFile { source, path })?;

for layer_id in imagedb_entry.rootfs.diff_ids {
let layer_id = layer_id.split(':').last().unwrap();
let layer_id = layer_id
.split(':')
.last()
.ok_or(ContainerError::InvalidLayerID(layer_id.clone()))?;

let path = PathBuf::from(DOCKER_LAYERDB_PATH).join(&layer_id);
if path.exists() {
Expand Down
20 changes: 20 additions & 0 deletions crates/bpf-common/src/containers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,26 @@ pub enum ContainerError {
source: serde_json::error::Error,
path: PathBuf,
},
#[error("parsing response from `{uri:?}` failed")]
ParseResponse {
#[source]
source: serde_json::error::Error,
uri: hyper::Uri,
},
#[error("path `{path}` is non-UTF-8")]
PathNonUtf8 { path: PathBuf },
#[error("failed to make a request to the UNIX socket `{uri:?}`")]
HyperRequest {
#[source]
source: hyper::Error,
uri: hyper::Uri,
},
#[error("failed to parse a response from the UNIX socket `{uri:?}`")]
HyperResponse {
#[source]
source: hyper::Error,
uri: hyper::Uri,
},
#[error("could not connect to the database `{path:?}`")]
SqliteConnection {
#[source]
Expand Down Expand Up @@ -79,6 +97,8 @@ pub enum ContainerError {
BoltBucketNotFound(String),
#[error("bolt key `{0}` not found")]
BoltKeyNotFound(String),
#[error("Invalid layer ID: `{0}`")]
InvalidLayerID(String),
}

/// A container ID.
Expand Down

0 comments on commit e441688

Please sign in to comment.