Skip to content

Commit 794ad45

Browse files
committed
fix: improve error messages to include HTTP status codes
- Enhanced error conversion to preserve HTTP status codes and details - Replaced .context() calls with proper error conversion for REST API calls - Now shows specific messages like '401 Unauthorized' and '404 Not Found' - Maintains backward compatibility while providing clearer error information
1 parent 313e83a commit 794ad45

30 files changed

+185
-143
lines changed

crates/redisctl/src/commands/enterprise/actions.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::error::RedisCtlError;
12
use anyhow::Context;
23
use clap::Subcommand;
34
use redis_enterprise::ActionHandler;
@@ -81,7 +82,8 @@ impl ActionCommands {
8182
.await
8283
.context("Failed to list actions (v2)")?
8384
} else {
84-
handler.list().await.context("Failed to list actions")?
85+
handler.list().await
86+
.map_err(|e| RedisCtlError::from(e))?
8587
};
8688

8789
// Convert to JSON Value for filtering and output

crates/redisctl/src/commands/enterprise/alerts.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use anyhow::{Context, Result as AnyhowResult};
1+
use crate::error::RedisCtlError;
2+
use anyhow::Result as AnyhowResult;
23
use clap::Subcommand;
34
use serde_json::Value;
45

@@ -283,7 +284,7 @@ async fn handle_cluster_alerts(
283284
let response = client
284285
.get::<Value>(&endpoint)
285286
.await
286-
.context("Failed to get cluster alerts")?;
287+
.map_err(|e| RedisCtlError::from(e))?;
287288

288289
let response = if let Some(q) = query {
289290
super::utils::apply_jmespath(&response, q)?
@@ -314,7 +315,7 @@ async fn handle_node_alerts(
314315
let response = client
315316
.get::<Value>(&endpoint)
316317
.await
317-
.context("Failed to get node alerts")?;
318+
.map_err(|e| RedisCtlError::from(e))?;
318319

319320
let response = if let Some(q) = query {
320321
super::utils::apply_jmespath(&response, q)?
@@ -345,7 +346,7 @@ async fn handle_database_alerts(
345346
let response = client
346347
.get::<Value>(&endpoint)
347348
.await
348-
.context("Failed to get database alerts")?;
349+
.map_err(|e| RedisCtlError::from(e))?;
349350

350351
let response = if let Some(q) = query {
351352
super::utils::apply_jmespath(&response, q)?
@@ -368,7 +369,7 @@ async fn handle_get_alert_settings(
368369
let response = client
369370
.get::<Value>("/v1/cluster")
370371
.await
371-
.context("Failed to get cluster configuration")?;
372+
.map_err(|e| RedisCtlError::from(e))?;
372373

373374
// Extract alert_settings from the cluster config
374375
let alert_settings = response
@@ -404,7 +405,7 @@ async fn handle_update_alert_settings(
404405
let response = client
405406
.put::<_, Value>("/v1/cluster", &update_payload)
406407
.await
407-
.context("Failed to update alert settings")?;
408+
.map_err(|e| RedisCtlError::from(e))?;
408409

409410
let response = if let Some(q) = query {
410411
super::utils::apply_jmespath(&response, q)?

crates/redisctl/src/commands/enterprise/bdb_group.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use anyhow::Context;
1+
use crate::error::RedisCtlError;
22
use clap::Subcommand;
33

44
use crate::cli::OutputFormat;
@@ -82,7 +82,7 @@ impl BdbGroupCommands {
8282
let response: serde_json::Value = client
8383
.get("/v1/bdb_groups")
8484
.await
85-
.context("Failed to list database groups")?;
85+
.map_err(|e| RedisCtlError::from(e))?;
8686

8787
let output_data = if let Some(q) = query {
8888
super::utils::apply_jmespath(&response, q)?
@@ -96,7 +96,7 @@ impl BdbGroupCommands {
9696
let response: serde_json::Value = client
9797
.get(&format!("/v1/bdb_groups/{}", uid))
9898
.await
99-
.context("Failed to get database group")?;
99+
.map_err(|e| RedisCtlError::from(e))?;
100100

101101
let output_data = if let Some(q) = query {
102102
super::utils::apply_jmespath(&response, q)?
@@ -112,7 +112,7 @@ impl BdbGroupCommands {
112112
let response: serde_json::Value = client
113113
.post("/v1/bdb_groups", &json_data)
114114
.await
115-
.context("Failed to create database group")?;
115+
.map_err(|e| RedisCtlError::from(e))?;
116116

117117
let output_data = if let Some(q) = query {
118118
super::utils::apply_jmespath(&response, q)?
@@ -128,7 +128,7 @@ impl BdbGroupCommands {
128128
let response: serde_json::Value = client
129129
.put(&format!("/v1/bdb_groups/{}", uid), &json_data)
130130
.await
131-
.context("Failed to update database group")?;
131+
.map_err(|e| RedisCtlError::from(e))?;
132132

133133
let output_data = if let Some(q) = query {
134134
super::utils::apply_jmespath(&response, q)?
@@ -148,7 +148,7 @@ impl BdbGroupCommands {
148148
client
149149
.delete(&format!("/v1/bdb_groups/{}", uid))
150150
.await
151-
.context("Failed to delete database group")?;
151+
.map_err(|e| RedisCtlError::from(e))?;
152152

153153
println!("Database group {} deleted successfully", uid);
154154
}
@@ -161,7 +161,7 @@ impl BdbGroupCommands {
161161
let mut group_data: serde_json::Value = client
162162
.get(&format!("/v1/bdb_groups/{}", group_uid))
163163
.await
164-
.context("Failed to get database group")?;
164+
.map_err(|e| RedisCtlError::from(e))?;
165165

166166
// Add database to the group
167167
if let Some(bdbs) = group_data["bdbs"].as_array_mut() {
@@ -179,7 +179,7 @@ impl BdbGroupCommands {
179179
let response: serde_json::Value = client
180180
.put(&format!("/v1/bdb_groups/{}", group_uid), &group_data)
181181
.await
182-
.context("Failed to add database to group")?;
182+
.map_err(|e| RedisCtlError::from(e))?;
183183

184184
println!("Database {} added to group {}", database, group_uid);
185185

@@ -199,7 +199,7 @@ impl BdbGroupCommands {
199199
let mut group_data: serde_json::Value = client
200200
.get(&format!("/v1/bdb_groups/{}", group_uid))
201201
.await
202-
.context("Failed to get database group")?;
202+
.map_err(|e| RedisCtlError::from(e))?;
203203

204204
// Remove database from the group
205205
if let Some(bdbs) = group_data["bdbs"].as_array_mut() {
@@ -218,7 +218,7 @@ impl BdbGroupCommands {
218218
let response: serde_json::Value = client
219219
.put(&format!("/v1/bdb_groups/{}", group_uid), &group_data)
220220
.await
221-
.context("Failed to remove database from group")?;
221+
.map_err(|e| RedisCtlError::from(e))?;
222222

223223
println!("Database {} removed from group {}", database, group_uid);
224224

@@ -234,7 +234,7 @@ impl BdbGroupCommands {
234234
let response: serde_json::Value = client
235235
.get(&format!("/v1/bdb_groups/{}", group_uid))
236236
.await
237-
.context("Failed to get database group")?;
237+
.map_err(|e| RedisCtlError::from(e))?;
238238

239239
// Extract just the databases list
240240
let databases = &response["bdbs"];

crates/redisctl/src/commands/enterprise/bootstrap.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ async fn handle_bootstrap_status(
7575
let response = client
7676
.get::<Value>("/v1/bootstrap")
7777
.await
78-
.context("Failed to get bootstrap status")?;
78+
.map_err(|e| RedisCtlError::from(e))?;
7979

8080
let result = if let Some(q) = query {
8181
utils::apply_jmespath(&response, q)?
@@ -102,7 +102,7 @@ async fn handle_create_cluster(
102102
let response = client
103103
.post_raw("/v1/bootstrap/create_cluster", payload)
104104
.await
105-
.context("Failed to create cluster")?;
105+
.map_err(|e| RedisCtlError::from(e))?;
106106

107107
let result = if let Some(q) = query {
108108
utils::apply_jmespath(&response, q)?
@@ -129,7 +129,7 @@ async fn handle_join_cluster(
129129
let response = client
130130
.post_raw("/v1/bootstrap/join_cluster", payload)
131131
.await
132-
.context("Failed to join cluster")?;
132+
.map_err(|e| RedisCtlError::from(e))?;
133133

134134
let result = if let Some(q) = query {
135135
utils::apply_jmespath(&response, q)?

crates/redisctl/src/commands/enterprise/cluster_impl.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use crate::cli::OutputFormat;
66
use crate::connection::ConnectionManager;
7+
use crate::error::RedisCtlError;
78
use crate::error::Result as CliResult;
89
use anyhow::Context;
910
use redis_enterprise::alerts::AlertHandler;
@@ -164,7 +165,7 @@ pub async fn bootstrap_cluster(
164165
let result = client
165166
.post_raw("/v1/bootstrap", bootstrap_data)
166167
.await
167-
.context("Failed to bootstrap cluster")?;
168+
.map_err(|e| RedisCtlError::from(e))?;
168169
let data = handle_output(result, output_format, query)?;
169170
print_formatted_output(data, output_format)?;
170171
Ok(())

crates/redisctl/src/commands/enterprise/cm_settings.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::error::RedisCtlError;
12
use anyhow::Context;
23
use clap::Subcommand;
34

@@ -100,7 +101,7 @@ impl CmSettingsCommands {
100101
let response: serde_json::Value = client
101102
.get("/v1/cm_settings")
102103
.await
103-
.context("Failed to get cluster manager settings")?;
104+
.map_err(|e| RedisCtlError::from(e))?;
104105

105106
let output_data = if let Some(s) = setting {
106107
// Use the setting parameter as a JMESPath query
@@ -124,7 +125,7 @@ impl CmSettingsCommands {
124125
client
125126
.put("/v1/cm_settings", &json_data)
126127
.await
127-
.context("Failed to update cluster manager settings")?;
128+
.map_err(|e| RedisCtlError::from(e))?;
128129

129130
println!("Cluster manager settings updated successfully");
130131

@@ -146,7 +147,7 @@ impl CmSettingsCommands {
146147
let mut settings: serde_json::Value = client
147148
.get("/v1/cm_settings")
148149
.await
149-
.context("Failed to get current settings")?;
150+
.map_err(|e| RedisCtlError::from(e))?;
150151

151152
// Parse value as JSON if possible, otherwise as string
152153
let parsed_value: serde_json::Value =
@@ -173,7 +174,7 @@ impl CmSettingsCommands {
173174
let response: serde_json::Value = client
174175
.put("/v1/cm_settings", &settings)
175176
.await
176-
.context("Failed to update setting")?;
177+
.map_err(|e| RedisCtlError::from(e))?;
177178

178179
println!("Setting '{}' updated to: {}", name, value);
179180

@@ -198,7 +199,7 @@ impl CmSettingsCommands {
198199
let response: serde_json::Value = client
199200
.put("/v1/cm_settings", &serde_json::json!({}))
200201
.await
201-
.context("Failed to reset settings")?;
202+
.map_err(|e| RedisCtlError::from(e))?;
202203

203204
println!("Cluster manager settings reset to defaults");
204205

@@ -214,7 +215,7 @@ impl CmSettingsCommands {
214215
let settings: serde_json::Value = client
215216
.get("/v1/cm_settings")
216217
.await
217-
.context("Failed to get settings for export")?;
218+
.map_err(|e| RedisCtlError::from(e))?;
218219

219220
if output == "-" {
220221
// Output to stdout
@@ -240,7 +241,7 @@ impl CmSettingsCommands {
240241
let response: serde_json::Value = client
241242
.put("/v1/cm_settings", &json_data)
242243
.await
243-
.context("Failed to import settings")?;
244+
.map_err(|e| RedisCtlError::from(e))?;
244245

245246
println!("Settings imported successfully");
246247

@@ -279,7 +280,7 @@ impl CmSettingsCommands {
279280
let settings: serde_json::Value = client
280281
.get("/v1/cm_settings")
281282
.await
282-
.context("Failed to get settings")?;
283+
.map_err(|e| RedisCtlError::from(e))?;
283284

284285
// Extract top-level keys as categories
285286
let categories = if let Some(obj) = settings.as_object() {
@@ -301,7 +302,7 @@ impl CmSettingsCommands {
301302
let settings: serde_json::Value = client
302303
.get("/v1/cm_settings")
303304
.await
304-
.context("Failed to get settings")?;
305+
.map_err(|e| RedisCtlError::from(e))?;
305306

306307
// Extract specific category
307308
let category_data = &settings[category];

0 commit comments

Comments
 (0)