Skip to content

Commit 77c5ff6

Browse files
authored
fix(cli): name the sandbox in service not-found errors (#3798)
Fixes #3736 Signed-off-by: Eric Curtin <eric.curtin@docker.com>
1 parent eeba0e7 commit 77c5ff6

1 file changed

Lines changed: 76 additions & 14 deletions

File tree

  • crates/openshell-cli/src

‎crates/openshell-cli/src/run.rs‎

Lines changed: 76 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4183,14 +4183,10 @@ async fn expose_service_endpoint(
41834183
)),
41844184
})
41854185
.await
4186-
.map_err(service_expose_status_error)
4186+
.map_err(|status| service_status_error("expose service", "sandbox:write", sandbox, status))
41874187
.map(tonic::Response::into_inner)
41884188
}
41894189

4190-
fn service_expose_status_error(status: Status) -> miette::Report {
4191-
service_status_error("expose service", "sandbox:write", status)
4192-
}
4193-
41944190
#[allow(clippy::too_many_arguments)] // user-facing CLI command
41954191
pub async fn service_list(
41964192
server: &str,
@@ -4215,7 +4211,14 @@ pub async fn service_list(
42154211
}),
42164212
})
42174213
.await
4218-
.map_err(|status| service_status_error("list services", "sandbox:read", status))?
4214+
.map_err(|status| {
4215+
service_status_error(
4216+
"list services",
4217+
"sandbox:read",
4218+
sandbox.unwrap_or_default(),
4219+
status,
4220+
)
4221+
})?
42194222
.into_inner();
42204223

42214224
let next_page_token = response.next_page_token.clone();
@@ -4265,7 +4268,7 @@ pub async fn service_get(
42654268
name: service.to_string(),
42664269
})
42674270
.await
4268-
.map_err(|status| service_status_error("get service", "sandbox:read", status))?
4271+
.map_err(|status| service_status_error("get service", "sandbox:read", sandbox, status))?
42694272
.into_inner();
42704273

42714274
print_service_endpoint_table(&[response], server, false);
@@ -4291,7 +4294,7 @@ pub async fn service_delete(
42914294
)),
42924295
})
42934296
.await
4294-
.map_err(|status| service_status_error("delete service", "sandbox:write", status))?
4297+
.map_err(|status| service_status_error("delete service", "sandbox:write", sandbox, status))?
42954298
.into_inner();
42964299

42974300
if !deletion_completed(response.outcome)? {
@@ -4315,13 +4318,23 @@ pub async fn service_delete(
43154318
Ok(())
43164319
}
43174320

4318-
fn service_status_error(action: &str, required_scope: &str, status: Status) -> miette::Report {
4321+
fn service_status_error(
4322+
action: &str,
4323+
required_scope: &str,
4324+
sandbox: &str,
4325+
status: Status,
4326+
) -> miette::Report {
43194327
let message = status.message();
43204328
match status.code() {
43214329
Code::PermissionDenied => {
43224330
miette!("{action} failed: permission denied (requires {required_scope})")
43234331
}
43244332
Code::Unauthenticated => miette!("{action} failed: authentication required"),
4333+
Code::NotFound if message == "sandbox not found" && !sandbox.is_empty() => {
4334+
miette!(
4335+
"{action} failed: sandbox \"{sandbox}\" not found (the first argument is the sandbox name, not the service name)"
4336+
)
4337+
}
43254338
Code::NotFound if message == "sandbox not found" => {
43264339
miette!("{action} failed: sandbox not found")
43274340
}
@@ -6902,7 +6915,7 @@ mod tests {
69026915
policy_revision_to_json, proto_execution_timeout, provisioning_timeout_message,
69036916
ready_false_condition_message, relay_local_socket, resolve_from,
69046917
rootfs_tar_sources_supported_for_gateway, sandbox_should_persist, sandbox_upload_plan,
6905-
service_endpoint_to_json, service_expose_status_error, service_url_for_gateway,
6918+
service_endpoint_to_json, service_status_error, service_url_for_gateway,
69066919
workspace_member_to_json,
69076920
};
69086921
use openshell_core::proto::TcpForwardFrame;
@@ -7847,17 +7860,66 @@ mod tests {
78477860
}
78487861

78497862
#[test]
7850-
fn service_expose_status_error_mentions_required_scope() {
7851-
let report = service_expose_status_error(Status::permission_denied(
7852-
"scope 'sandbox:write' required",
7853-
));
7863+
fn service_status_error_mentions_required_scope() {
7864+
let report = service_status_error(
7865+
"expose service",
7866+
"sandbox:write",
7867+
"demo",
7868+
Status::permission_denied("scope 'sandbox:write' required"),
7869+
);
78547870

78557871
assert_eq!(
78567872
report.to_string(),
78577873
"expose service failed: permission denied (requires sandbox:write)"
78587874
);
78597875
}
78607876

7877+
#[test]
7878+
fn service_status_error_names_missing_sandbox() {
7879+
let report = service_status_error(
7880+
"delete service",
7881+
"sandbox:write",
7882+
"web-http",
7883+
Status::not_found("sandbox not found"),
7884+
);
7885+
7886+
assert_eq!(
7887+
report.to_string(),
7888+
"delete service failed: sandbox \"web-http\" not found \
7889+
(the first argument is the sandbox name, not the service name)"
7890+
);
7891+
}
7892+
7893+
#[test]
7894+
fn service_status_error_omits_empty_sandbox_name() {
7895+
let report = service_status_error(
7896+
"list services",
7897+
"sandbox:read",
7898+
"",
7899+
Status::not_found("sandbox not found"),
7900+
);
7901+
7902+
assert_eq!(
7903+
report.to_string(),
7904+
"list services failed: sandbox not found"
7905+
);
7906+
}
7907+
7908+
#[test]
7909+
fn service_status_error_keeps_missing_service_message() {
7910+
let report = service_status_error(
7911+
"get service",
7912+
"sandbox:read",
7913+
"demo",
7914+
Status::not_found("service endpoint not found"),
7915+
);
7916+
7917+
assert_eq!(
7918+
report.to_string(),
7919+
"get service failed: service endpoint not found"
7920+
);
7921+
}
7922+
78617923
#[test]
78627924
fn ready_false_condition_message_prefers_reason_and_message() {
78637925
let status = SandboxStatus {

0 commit comments

Comments
 (0)