mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-29 21:51:59 +02:00
Handle CLI send errors without panicking
This commit is contained in:
@@ -19,18 +19,42 @@ use yaak_plugins::events::{PluginContext, RenderPurpose};
|
|||||||
use yaak_plugins::template_callback::PluginTemplateCallback;
|
use yaak_plugins::template_callback::PluginTemplateCallback;
|
||||||
use yaak_templates::{RenderOptions, parse_and_render, render_json_value_raw};
|
use yaak_templates::{RenderOptions, parse_and_render, render_json_value_raw};
|
||||||
|
|
||||||
pub async fn run(ctx: &CliContext, args: RequestArgs, environment: Option<&str>, verbose: bool) {
|
pub async fn run(
|
||||||
|
ctx: &CliContext,
|
||||||
|
args: RequestArgs,
|
||||||
|
environment: Option<&str>,
|
||||||
|
verbose: bool,
|
||||||
|
) -> i32 {
|
||||||
match args.command {
|
match args.command {
|
||||||
RequestCommands::List { workspace_id } => list(ctx, &workspace_id),
|
RequestCommands::List { workspace_id } => {
|
||||||
RequestCommands::Show { request_id } => show(ctx, &request_id),
|
list(ctx, &workspace_id);
|
||||||
|
0
|
||||||
|
}
|
||||||
|
RequestCommands::Show { request_id } => {
|
||||||
|
show(ctx, &request_id);
|
||||||
|
0
|
||||||
|
}
|
||||||
RequestCommands::Send { request_id } => {
|
RequestCommands::Send { request_id } => {
|
||||||
send_request_by_id(ctx, &request_id, environment, verbose).await;
|
match send_request_by_id(ctx, &request_id, environment, verbose).await {
|
||||||
|
Ok(()) => 0,
|
||||||
|
Err(error) => {
|
||||||
|
eprintln!("Error: {error}");
|
||||||
|
1
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
RequestCommands::Create { workspace_id, name, method, url, json } => {
|
RequestCommands::Create { workspace_id, name, method, url, json } => {
|
||||||
create(ctx, workspace_id, name, method, url, json)
|
create(ctx, workspace_id, name, method, url, json);
|
||||||
|
0
|
||||||
|
}
|
||||||
|
RequestCommands::Update { json, json_input } => {
|
||||||
|
update(ctx, json, json_input);
|
||||||
|
0
|
||||||
|
}
|
||||||
|
RequestCommands::Delete { request_id, yes } => {
|
||||||
|
delete(ctx, &request_id, yes);
|
||||||
|
0
|
||||||
}
|
}
|
||||||
RequestCommands::Update { json, json_input } => update(ctx, json, json_input),
|
|
||||||
RequestCommands::Delete { request_id, yes } => delete(ctx, &request_id, yes),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -151,13 +175,14 @@ pub async fn send_request_by_id(
|
|||||||
request_id: &str,
|
request_id: &str,
|
||||||
environment: Option<&str>,
|
environment: Option<&str>,
|
||||||
verbose: bool,
|
verbose: bool,
|
||||||
) {
|
) -> Result<(), String> {
|
||||||
let request = ctx.db().get_http_request(request_id).expect("Failed to get request");
|
let request =
|
||||||
|
ctx.db().get_http_request(request_id).map_err(|e| format!("Failed to get request: {e}"))?;
|
||||||
|
|
||||||
let environment_chain = ctx
|
let environment_chain = ctx
|
||||||
.db()
|
.db()
|
||||||
.resolve_environments(&request.workspace_id, request.folder_id.as_deref(), environment)
|
.resolve_environments(&request.workspace_id, request.folder_id.as_deref(), environment)
|
||||||
.unwrap_or_default();
|
.map_err(|e| format!("Failed to resolve environments: {e}"))?;
|
||||||
|
|
||||||
let plugin_context = PluginContext::new(None, Some(request.workspace_id.clone()));
|
let plugin_context = PluginContext::new(None, Some(request.workspace_id.clone()));
|
||||||
let template_callback = PluginTemplateCallback::new(
|
let template_callback = PluginTemplateCallback::new(
|
||||||
@@ -174,7 +199,7 @@ pub async fn send_request_by_id(
|
|||||||
&RenderOptions::throw(),
|
&RenderOptions::throw(),
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.expect("Failed to render request templates");
|
.map_err(|e| format!("Failed to render request templates: {e}"))?;
|
||||||
|
|
||||||
if verbose {
|
if verbose {
|
||||||
println!("> {} {}", rendered_request.method, rendered_request.url);
|
println!("> {} {}", rendered_request.method, rendered_request.url);
|
||||||
@@ -185,7 +210,7 @@ pub async fn send_request_by_id(
|
|||||||
SendableHttpRequestOptions::default(),
|
SendableHttpRequestOptions::default(),
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.expect("Failed to build request");
|
.map_err(|e| format!("Failed to build request: {e}"))?;
|
||||||
|
|
||||||
let (event_tx, mut event_rx) = mpsc::channel(100);
|
let (event_tx, mut event_rx) = mpsc::channel(100);
|
||||||
|
|
||||||
@@ -200,8 +225,11 @@ pub async fn send_request_by_id(
|
|||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
let sender = ReqwestSender::new().expect("Failed to create HTTP client");
|
let sender = ReqwestSender::new().map_err(|e| format!("Failed to create HTTP client: {e}"))?;
|
||||||
let response = sender.send(sendable, event_tx).await.expect("Failed to send request");
|
let response = sender
|
||||||
|
.send(sendable, event_tx)
|
||||||
|
.await
|
||||||
|
.map_err(|e| format!("Failed to send request: {e}"))?;
|
||||||
|
|
||||||
if let Some(handle) = verbose_handle {
|
if let Some(handle) = verbose_handle {
|
||||||
let _ = handle.await;
|
let _ = handle.await;
|
||||||
@@ -219,8 +247,10 @@ pub async fn send_request_by_id(
|
|||||||
println!();
|
println!();
|
||||||
}
|
}
|
||||||
|
|
||||||
let (body, _stats) = response.text().await.expect("Failed to read response body");
|
let (body, _stats) =
|
||||||
|
response.text().await.map_err(|e| format!("Failed to read response body: {e}"))?;
|
||||||
println!("{}", body);
|
println!("{}", body);
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Render an HTTP request with template variables and plugin functions.
|
/// Render an HTTP request with template variables and plugin functions.
|
||||||
|
|||||||
@@ -2,6 +2,17 @@ use crate::cli::SendArgs;
|
|||||||
use crate::commands::request;
|
use crate::commands::request;
|
||||||
use crate::context::CliContext;
|
use crate::context::CliContext;
|
||||||
|
|
||||||
pub async fn run(ctx: &CliContext, args: SendArgs, environment: Option<&str>, verbose: bool) {
|
pub async fn run(
|
||||||
request::send_request_by_id(ctx, &args.request_id, environment, verbose).await;
|
ctx: &CliContext,
|
||||||
|
args: SendArgs,
|
||||||
|
environment: Option<&str>,
|
||||||
|
verbose: bool,
|
||||||
|
) -> i32 {
|
||||||
|
match request::send_request_by_id(ctx, &args.request_id, environment, verbose).await {
|
||||||
|
Ok(()) => 0,
|
||||||
|
Err(error) => {
|
||||||
|
eprintln!("Error: {error}");
|
||||||
|
1
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,16 +30,14 @@ async fn main() {
|
|||||||
|
|
||||||
let exit_code = match command {
|
let exit_code = match command {
|
||||||
Commands::Send(args) => {
|
Commands::Send(args) => {
|
||||||
commands::send::run(&context, args, environment.as_deref(), verbose).await;
|
commands::send::run(&context, args, environment.as_deref(), verbose).await
|
||||||
0
|
|
||||||
}
|
}
|
||||||
Commands::Workspace(args) => {
|
Commands::Workspace(args) => {
|
||||||
commands::workspace::run(&context, args);
|
commands::workspace::run(&context, args);
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
Commands::Request(args) => {
|
Commands::Request(args) => {
|
||||||
commands::request::run(&context, args, environment.as_deref(), verbose).await;
|
commands::request::run(&context, args, environment.as_deref(), verbose).await
|
||||||
0
|
|
||||||
}
|
}
|
||||||
Commands::Folder(args) => {
|
Commands::Folder(args) => {
|
||||||
commands::folder::run(&context, args);
|
commands::folder::run(&context, args);
|
||||||
|
|||||||
Reference in New Issue
Block a user