improve error messages

This commit is contained in:
Loïc Riegel 2025-03-30 14:26:26 +02:00
parent 99f85e19e2
commit f5bce5a19b

View File

@ -302,7 +302,7 @@ fn merge_record(record: &Record, head: Span, span: Span) -> Value {
dbg!(other); dbg!(other);
return Value::error( return Value::error(
ShellError::OnlySupportsThisInputType { ShellError::OnlySupportsThisInputType {
exp_input_type: "record<int>".to_string(), exp_input_type: "int".to_string(),
wrong_type: other.get_type().to_string(), wrong_type: other.get_type().to_string(),
dst_span: head, dst_span: head,
src_span: other.span(), src_span: other.span(),
@ -332,7 +332,7 @@ fn merge_record(record: &Record, head: Span, span: Span) -> Value {
dbg!(other); dbg!(other);
return Value::error( return Value::error(
ShellError::OnlySupportsThisInputType { ShellError::OnlySupportsThisInputType {
exp_input_type: "record<int>".to_string(), exp_input_type: "int".to_string(),
wrong_type: other.get_type().to_string(), wrong_type: other.get_type().to_string(),
dst_span: head, dst_span: head,
src_span: other.span(), src_span: other.span(),
@ -362,7 +362,7 @@ fn merge_record(record: &Record, head: Span, span: Span) -> Value {
dbg!(other); dbg!(other);
return Value::error( return Value::error(
ShellError::OnlySupportsThisInputType { ShellError::OnlySupportsThisInputType {
exp_input_type: "record<int>".to_string(), exp_input_type: "int".to_string(),
wrong_type: other.get_type().to_string(), wrong_type: other.get_type().to_string(),
dst_span: head, dst_span: head,
src_span: other.span(), src_span: other.span(),
@ -392,7 +392,7 @@ fn merge_record(record: &Record, head: Span, span: Span) -> Value {
dbg!(other); dbg!(other);
return Value::error( return Value::error(
ShellError::OnlySupportsThisInputType { ShellError::OnlySupportsThisInputType {
exp_input_type: "record<int>".to_string(), exp_input_type: "int".to_string(),
wrong_type: other.get_type().to_string(), wrong_type: other.get_type().to_string(),
dst_span: head, dst_span: head,
src_span: other.span(), src_span: other.span(),
@ -422,7 +422,7 @@ fn merge_record(record: &Record, head: Span, span: Span) -> Value {
dbg!(other); dbg!(other);
return Value::error( return Value::error(
ShellError::OnlySupportsThisInputType { ShellError::OnlySupportsThisInputType {
exp_input_type: "record<int>".to_string(), exp_input_type: "int".to_string(),
wrong_type: other.get_type().to_string(), wrong_type: other.get_type().to_string(),
dst_span: head, dst_span: head,
src_span: other.span(), src_span: other.span(),
@ -452,7 +452,7 @@ fn merge_record(record: &Record, head: Span, span: Span) -> Value {
dbg!(other); dbg!(other);
return Value::error( return Value::error(
ShellError::OnlySupportsThisInputType { ShellError::OnlySupportsThisInputType {
exp_input_type: "record<int>".to_string(), exp_input_type: "int".to_string(),
wrong_type: other.get_type().to_string(), wrong_type: other.get_type().to_string(),
dst_span: head, dst_span: head,
src_span: other.span(), src_span: other.span(),
@ -482,7 +482,7 @@ fn merge_record(record: &Record, head: Span, span: Span) -> Value {
dbg!(other); dbg!(other);
return Value::error( return Value::error(
ShellError::OnlySupportsThisInputType { ShellError::OnlySupportsThisInputType {
exp_input_type: "record<int>".to_string(), exp_input_type: "int".to_string(),
wrong_type: other.get_type().to_string(), wrong_type: other.get_type().to_string(),
dst_span: head, dst_span: head,
src_span: other.span(), src_span: other.span(),
@ -512,7 +512,7 @@ fn merge_record(record: &Record, head: Span, span: Span) -> Value {
dbg!(other); dbg!(other);
return Value::error( return Value::error(
ShellError::OnlySupportsThisInputType { ShellError::OnlySupportsThisInputType {
exp_input_type: "record<int>".to_string(), exp_input_type: "int".to_string(),
wrong_type: other.get_type().to_string(), wrong_type: other.get_type().to_string(),
dst_span: head, dst_span: head,
src_span: other.span(), src_span: other.span(),
@ -530,7 +530,7 @@ fn merge_record(record: &Record, head: Span, span: Span) -> Value {
dbg!(other); dbg!(other);
return Value::error( return Value::error(
ShellError::OnlySupportsThisInputType { ShellError::OnlySupportsThisInputType {
exp_input_type: "record<int>".to_string(), exp_input_type: "int".to_string(),
wrong_type: other.get_type().to_string(), wrong_type: other.get_type().to_string(),
dst_span: head, dst_span: head,
src_span: other.span(), src_span: other.span(),
@ -541,14 +541,29 @@ fn merge_record(record: &Record, head: Span, span: Span) -> Value {
}, },
None => 0, None => 0,
}; };
let timezone: &str = match record.get("timezone") { let offset = match record.get("timezone") {
Some(val) => match val { Some(val) => match val {
Value::String { val, .. } => val, Value::String { val, internal_span } => {
let offset: FixedOffset = match val.parse() {
Ok(offset) => offset,
Err(_) => {
return Value::error(
ShellError::IncorrectValue {
msg: "invalid timezone".to_string(),
val_span: head,
call_span: *internal_span,
},
span,
)
}
};
offset
}
other => { other => {
dbg!(other); dbg!(other);
return Value::error( return Value::error(
ShellError::OnlySupportsThisInputType { ShellError::OnlySupportsThisInputType {
exp_input_type: "timezone: string".to_string(), exp_input_type: "string".to_string(),
wrong_type: other.get_type().to_string(), wrong_type: other.get_type().to_string(),
dst_span: head, dst_span: head,
src_span: other.span(), src_span: other.span(),
@ -557,7 +572,7 @@ fn merge_record(record: &Record, head: Span, span: Span) -> Value {
); );
} }
}, },
None => "+00:00", None => FixedOffset::east_opt(0).unwrap(),
}; };
dbg!(&nanosecond); dbg!(&nanosecond);
@ -569,7 +584,7 @@ fn merge_record(record: &Record, head: Span, span: Span) -> Value {
dbg!(&day); dbg!(&day);
dbg!(&month); dbg!(&month);
dbg!(&year); dbg!(&year);
dbg!(timezone); dbg!(&offset);
let total_nanoseconds = nanosecond + microsecond * 1_000 + millisecond * 1_000_000; let total_nanoseconds = nanosecond + microsecond * 1_000 + millisecond * 1_000_000;
@ -603,20 +618,6 @@ fn merge_record(record: &Record, head: Span, span: Span) -> Value {
}; };
let date_time = NaiveDateTime::new(date, time); let date_time = NaiveDateTime::new(date, time);
let offset: FixedOffset = match timezone.parse() {
Ok(val) => val,
Err(_) => {
return Value::error(
ShellError::IncorrectValue {
msg: "invalid timezone".to_string(),
val_span: head,
call_span: span,
},
span,
)
}
};
dbg!(&date); dbg!(&date);
dbg!(&time); dbg!(&time);
dbg!(&date_time); dbg!(&date_time);
@ -659,25 +660,26 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value {
Some(tz) => { Some(tz) => {
return Value::error( return Value::error(
ShellError::IncompatibleParameters { ShellError::IncompatibleParameters {
left_message: "left message".into(), left_message: "got a record as input".into(),
left_span: head, left_span: head,
right_message: "right message".into(), right_message: "the timezone should be included in the record".into(),
right_span: tz.span, right_span: tz.span,
}, },
head, head,
); );
} }
} }
// TODO // TODO
// match dateformat { // match dateformat {
// None => (), // None => (),
// Some(dt) => { // Some(_) => {
// return Value::error( // return Value::error(
// ShellError::IncompatibleParameters { // ShellError::IncompatibleParameters {
// left_message: "left message".into(), // left_message: "got a record as input".into(),
// left_span: head, // left_span: head,
// right_message: "right message".into(), // right_message:"cannot be used with records".into(),
// right_span: dt, // right_span: input.span(),
// }, // },
// head, // head,
// ); // );