Refining error handling in http post (#13805)

Related to #13701

# Description
Refining some of the error handling related to http post command
This commit is contained in:
xonas 2024-09-07 18:57:34 -03:00 committed by GitHub
parent 3d008e2c4e
commit 3e074bc447
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 20 additions and 21 deletions

View file

@ -259,7 +259,7 @@ fn send_json_request(
signals: &Signals,
) -> Result<Response, ShellErrorOrRequestError> {
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(),
},
)),
}

View file

@ -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