From 3e074bc447fe5394e365a918a0e46b5905c3a6bd Mon Sep 17 00:00:00 2001 From: xonas Date: Sat, 7 Sep 2024 18:57:34 -0300 Subject: [PATCH] Refining error handling in http post (#13805) Related to #13701 # Description Refining some of the error handling related to http post command --- crates/nu-command/src/network/http/client.rs | 32 ++++++++++++-------- crates/nu-protocol/src/errors/shell_error.rs | 9 ------ 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/crates/nu-command/src/network/http/client.rs b/crates/nu-command/src/network/http/client.rs index 3729b37a43..e53a1f4413 100644 --- a/crates/nu-command/src/network/http/client.rs +++ b/crates/nu-command/src/network/http/client.rs @@ -259,7 +259,7 @@ fn send_json_request( signals: &Signals, ) -> Result { match body { - Value::Int { .. } | Value::List { .. } | Value::Record { .. } => { + Value::Int { .. } | Value::Float { .. } | Value::List { .. } | Value::Record { .. } => { let data = value_to_json_value(&body)?; send_cancellable_request(request_url, Box::new(|| req.send_json(data)), span, signals) } @@ -284,8 +284,11 @@ fn send_json_request( } } _ => Err(ShellErrorOrRequestError::ShellError( - ShellError::UnsupportedHttpBody { - msg: format!("Accepted types: [Int, List, String, Record]. Check: {HTTP_DOCS}"), + ShellError::TypeMismatch { + err_message: format!( + "Accepted types: [int, float, list, string, record]. Check: {HTTP_DOCS}" + ), + span: body.span(), }, )), } @@ -308,10 +311,12 @@ fn send_form_request( }; match body { - Value::List { vals, .. } => { + Value::List { ref vals, .. } => { if vals.len() % 2 != 0 { - return Err(ShellErrorOrRequestError::ShellError(ShellError::UnsupportedHttpBody { - msg: "Body type 'List' for form requests requires paired values. E.g.: [value, 10]".into(), + return Err(ShellErrorOrRequestError::ShellError(ShellError::IncorrectValue { + msg: "Body type 'list' for form requests requires paired values. E.g.: [foo, 10]".into(), + val_span: body.span(), + call_span: span, })); } @@ -334,8 +339,9 @@ fn send_form_request( send_cancellable_request(request_url, request_fn, span, signals) } _ => Err(ShellErrorOrRequestError::ShellError( - ShellError::UnsupportedHttpBody { - msg: format!("Accepted types: [List, Record]. Check: {HTTP_DOCS}"), + ShellError::TypeMismatch { + err_message: format!("Accepted types: [list, record]. Check: {HTTP_DOCS}"), + span: body.span(), }, )), } @@ -388,8 +394,9 @@ fn send_multipart_request( } _ => { return Err(ShellErrorOrRequestError::ShellError( - ShellError::UnsupportedHttpBody { - msg: format!("Accepted types: [Record]. Check: {HTTP_DOCS}"), + ShellError::TypeMismatch { + err_message: format!("Accepted types: [record]. Check: {HTTP_DOCS}"), + span: body.span(), }, )) } @@ -418,8 +425,9 @@ fn send_default_request( signals, ), _ => Err(ShellErrorOrRequestError::ShellError( - ShellError::UnsupportedHttpBody { - msg: format!("Accepted types: [Binary, String]. Check: {HTTP_DOCS}"), + ShellError::TypeMismatch { + err_message: format!("Accepted types: [binary, string]. Check: {HTTP_DOCS}"), + span: body.span(), }, )), } diff --git a/crates/nu-protocol/src/errors/shell_error.rs b/crates/nu-protocol/src/errors/shell_error.rs index 0784c91fb4..4bed5abd07 100644 --- a/crates/nu-protocol/src/errors/shell_error.rs +++ b/crates/nu-protocol/src/errors/shell_error.rs @@ -682,15 +682,6 @@ pub enum ShellError { span: Span, }, - /// An unsupported body input was used for the respective application body type in 'http' command - /// - /// ## Resolution - /// - /// This error is fairly generic. Refer to the specific error message for further details. - #[error("Unsupported body for current content type")] - #[diagnostic(code(nu::shell::unsupported_body), help("{msg}"))] - UnsupportedHttpBody { msg: String }, - /// An operation was attempted with an input unsupported for some reason. /// /// ## Resolution