Skip to content

Commit

Permalink
Don't panic when http_client fails (nushell#11422)
Browse files Browse the repository at this point in the history
# Description

This PR makes `http_client` return `Result<ureq::Agent, ShellError>`, so
errors can be propagated to the caller.
  • Loading branch information
nibon7 authored and dmatos2012 committed Feb 20, 2024
1 parent 0a4f5c8 commit a6132d3
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 12 deletions.
14 changes: 9 additions & 5 deletions crates/nu-command/src/network/http/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,21 @@ pub enum BodyType {
Unknown,
}

// Only panics if the user agent is invalid but we define it statically so either
// it always or never fails
pub fn http_client(
allow_insecure: bool,
engine_state: &EngineState,
stack: &mut Stack,
) -> ureq::Agent {
) -> Result<ureq::Agent, ShellError> {
let tls = native_tls::TlsConnector::builder()
.danger_accept_invalid_certs(allow_insecure)
.build()
.expect("Failed to build network tls");
.map_err(|e| ShellError::GenericError {
error: format!("Failed to build network tls: {}", e),
msg: String::new(),
span: None,
help: None,
inner: vec![],
})?;

let mut agent_builder = ureq::builder()
.user_agent("nushell")
Expand All @@ -48,7 +52,7 @@ pub fn http_client(
}
};

agent_builder.build()
Ok(agent_builder.build())
}

pub fn http_parse_url(
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-command/src/network/http/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ fn helper(
let ctrl_c = engine_state.ctrlc.clone();
let (requested_url, _) = http_parse_url(call, span, args.url)?;

let client = http_client(args.insecure, engine_state, stack);
let client = http_client(args.insecure, engine_state, stack)?;
let mut request = client.delete(&requested_url);

request = request_set_timeout(args.timeout, request)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-command/src/network/http/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ fn helper(
let ctrl_c = engine_state.ctrlc.clone();
let (requested_url, _) = http_parse_url(call, span, args.url)?;

let client = http_client(args.insecure, engine_state, stack);
let client = http_client(args.insecure, engine_state, stack)?;
let mut request = client.get(&requested_url);

request = request_set_timeout(args.timeout, request)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-command/src/network/http/head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ fn helper(
let span = args.url.span();
let (requested_url, _) = http_parse_url(call, span, args.url)?;

let client = http_client(args.insecure, engine_state, stack);
let client = http_client(args.insecure, engine_state, stack)?;
let mut request = client.head(&requested_url);

request = request_set_timeout(args.timeout, request)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-command/src/network/http/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ fn helper(
let ctrl_c = engine_state.ctrlc.clone();
let (requested_url, _) = http_parse_url(call, span, args.url)?;

let client = http_client(args.insecure, engine_state, stack);
let client = http_client(args.insecure, engine_state, stack)?;
let mut request = client.request("OPTIONS", &requested_url);

request = request_set_timeout(args.timeout, request)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-command/src/network/http/patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ fn helper(
let ctrl_c = engine_state.ctrlc.clone();
let (requested_url, _) = http_parse_url(call, span, args.url)?;

let client = http_client(args.insecure, engine_state, stack);
let client = http_client(args.insecure, engine_state, stack)?;
let mut request = client.patch(&requested_url);

request = request_set_timeout(args.timeout, request)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-command/src/network/http/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ fn helper(
let ctrl_c = engine_state.ctrlc.clone();
let (requested_url, _) = http_parse_url(call, span, args.url)?;

let client = http_client(args.insecure, engine_state, stack);
let client = http_client(args.insecure, engine_state, stack)?;
let mut request = client.post(&requested_url);

request = request_set_timeout(args.timeout, request)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-command/src/network/http/put.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ fn helper(
let ctrl_c = engine_state.ctrlc.clone();
let (requested_url, _) = http_parse_url(call, span, args.url)?;

let client = http_client(args.insecure, engine_state, stack);
let client = http_client(args.insecure, engine_state, stack)?;
let mut request = client.put(&requested_url);

request = request_set_timeout(args.timeout, request)?;
Expand Down

0 comments on commit a6132d3

Please sign in to comment.