From da59dfe7d2810a2a8d4acc41f8cfa5144396142a Mon Sep 17 00:00:00 2001 From: Eric Hodel Date: Sun, 19 Nov 2023 12:32:11 -0800 Subject: [PATCH] Convert ShellError::NetworkFailure to named fields (#11093) # Description Part of #10700 --- crates/nu-command/src/network/http/client.rs | 30 ++++++-------------- crates/nu-protocol/src/shell_error.rs | 6 +++- 2 files changed, 14 insertions(+), 22 deletions(-) diff --git a/crates/nu-command/src/network/http/client.rs b/crates/nu-command/src/network/http/client.rs index 6f554e453..9365f6261 100644 --- a/crates/nu-command/src/network/http/client.rs +++ b/crates/nu-command/src/network/http/client.rs @@ -363,38 +363,26 @@ pub fn request_add_custom_headers( fn handle_response_error(span: Span, requested_url: &str, response_err: Error) -> ShellError { match response_err { - Error::Status(301, _) => ShellError::NetworkFailure( - format!("Resource moved permanently (301): {requested_url:?}"), - span, - ), + Error::Status(301, _) => ShellError::NetworkFailure { msg: format!("Resource moved permanently (301): {requested_url:?}"), span }, Error::Status(400, _) => { - ShellError::NetworkFailure(format!("Bad request (400) to {requested_url:?}"), span) + ShellError::NetworkFailure { msg: format!("Bad request (400) to {requested_url:?}"), span } } Error::Status(403, _) => { - ShellError::NetworkFailure(format!("Access forbidden (403) to {requested_url:?}"), span) + ShellError::NetworkFailure { msg: format!("Access forbidden (403) to {requested_url:?}"), span } } - Error::Status(404, _) => ShellError::NetworkFailure( - format!("Requested file not found (404): {requested_url:?}"), - span, - ), + Error::Status(404, _) => ShellError::NetworkFailure { msg: format!("Requested file not found (404): {requested_url:?}"), span }, Error::Status(408, _) => { - ShellError::NetworkFailure(format!("Request timeout (408): {requested_url:?}"), span) + ShellError::NetworkFailure { msg: format!("Request timeout (408): {requested_url:?}"), span } } - Error::Status(_, _) => ShellError::NetworkFailure( - format!( + Error::Status(_, _) => ShellError::NetworkFailure { msg: format!( "Cannot make request to {:?}. Error is {:?}", requested_url, response_err.to_string() - ), - span, - ), + ), span }, Error::Transport(t) => match t { - t if t.kind() == ErrorKind::ConnectionFailed => ShellError::NetworkFailure( - format!("Cannot make request to {requested_url}, there was an error establishing a connection.",), - span, - ), - t => ShellError::NetworkFailure(t.to_string(), span), + t if t.kind() == ErrorKind::ConnectionFailed => ShellError::NetworkFailure { msg: format!("Cannot make request to {requested_url}, there was an error establishing a connection.",), span }, + t => ShellError::NetworkFailure { msg: t.to_string(), span }, }, } } diff --git a/crates/nu-protocol/src/shell_error.rs b/crates/nu-protocol/src/shell_error.rs index 476eb47b3..bc48ea05c 100644 --- a/crates/nu-protocol/src/shell_error.rs +++ b/crates/nu-protocol/src/shell_error.rs @@ -676,7 +676,11 @@ pub enum ShellError { /// It's always DNS. #[error("Network failure")] #[diagnostic(code(nu::shell::network_failure))] - NetworkFailure(String, #[label("{0}")] Span), + NetworkFailure { + msg: String, + #[label("{msg}")] + span: Span, + }, /// Help text for this command could not be found. ///