Skip to content

Commit

Permalink
fix: parse ApiError from Hub and bubble up (#3692)
Browse files Browse the repository at this point in the history
  • Loading branch information
EstebanBorai authored Nov 17, 2023
1 parent 1ce572c commit b04b0f9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
32 changes: 24 additions & 8 deletions crates/fluvio-hub-util/src/fvm/api/client.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
//! Hub FVM API Client
use anyhow::{Error, Result};
use serde::{Deserialize, Serialize};
use url::Url;

use crate::fvm::{Channel, PackageSet, PackageSetRecord};

#[derive(Debug, Deserialize, Serialize)]
pub struct ApiError {
pub status: u16,
pub message: String,
}

/// HTTP Client for interacting with the Hub FVM API
pub struct Client {
api_url: Url,
Expand All @@ -24,17 +31,26 @@ impl Client {
let mut res = surf::get(url)
.await
.map_err(|err| Error::msg(err.to_string()))?;
let pkgset_record = res.body_json::<PackageSetRecord>().await.map_err(|err| {
tracing::error!(?err, "Failed to parse PackageSet from Hub");
Error::msg(format!(
"Server responded with status code {}",
err.status()
))
let res_status = res.status();

if res_status.is_success() {
let pkgset_record = res.body_json::<PackageSetRecord>().await.map_err(|err| {
tracing::debug!(?err, "Failed to parse PackageSet from Hub");
Error::msg("Failed to parse server's response")
})?;

tracing::info!(?pkgset_record, "Found PackageSet");
return Ok(pkgset_record.into());
}

let error = res.body_json::<ApiError>().await.map_err(|err| {
tracing::debug!(?err, "Failed to parse API Error from Hub");
Error::msg(format!("Server responded with status code {}", res_status))
})?;

tracing::info!(?pkgset_record, "Found PackageSet");
tracing::debug!(?error, "Server responded with not successful status code");

Ok(pkgset_record.into())
Err(anyhow::anyhow!(error.message))
}

/// Builds the URL to the Hub API for fetching a [`PackageSet`] using the
Expand Down
21 changes: 21 additions & 0 deletions tests/cli/fvm_smoke_tests/fvm_basic.bats
Original file line number Diff line number Diff line change
Expand Up @@ -791,3 +791,24 @@ setup_file() {
rm -rf $FLUVIO_HOME_DIR
assert_success
}

@test "Handles unexistent version error" {
run bash -c '$FVM_BIN self install'
assert_success

# Sets `fvm` in the PATH using the "env" file included in the installation
source ~/.fvm/env

# Attempts to install unexistent version
run bash -c 'fvm install 0.0.0'
assert_line --index 0 "Error: PackageSet 0.0.0 doest not exist"
assert_failure

# Removes FVM
run bash -c 'fvm self uninstall --yes'
assert_success

# Removes Fluvio
rm -rf $FLUVIO_HOME_DIR
assert_success
}

0 comments on commit b04b0f9

Please sign in to comment.