Skip to content

Commit

Permalink
Add error handling for invalid format
Browse files Browse the repository at this point in the history
  • Loading branch information
ashishajr committed Jan 21, 2025
1 parent 7d7357b commit fee90a8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
7 changes: 7 additions & 0 deletions packages/hurl/src/runner/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ pub enum RunnerErrorKind {
FilterDecode(String),
FilterInvalidEncoding(String),
FilterInvalidInput(String),
FilterInvalidFormatSpecifier(String),
FilterMissingInput,
Http(HttpError),
InvalidJson {
Expand Down Expand Up @@ -143,6 +144,7 @@ impl DisplaySourceError for RunnerError {
RunnerErrorKind::FilterDecode { .. } => "Filter error".to_string(),
RunnerErrorKind::FilterInvalidEncoding { .. } => "Filter error".to_string(),
RunnerErrorKind::FilterInvalidInput { .. } => "Filter error".to_string(),
RunnerErrorKind::FilterInvalidFormatSpecifier { .. } => "Filter error".to_string(),
RunnerErrorKind::FilterMissingInput => "Filter error".to_string(),
RunnerErrorKind::Http(http_error) => http_error.description(),
RunnerErrorKind::InvalidJson { .. } => "Invalid JSON".to_string(),
Expand Down Expand Up @@ -244,6 +246,11 @@ impl DisplaySourceError for RunnerError {
let message = error::add_carets(message, self.source_info, content);
color_red_multiline_string(&message)
}
RunnerErrorKind::FilterInvalidFormatSpecifier(format) => {
let message = &format!("<{format}> format is not supported");
let message = error::add_carets(message, self.source_info, content);
color_red_multiline_string(&message)
}
RunnerErrorKind::FilterMissingInput => {
let message = "missing value to apply filter";
let message = error::add_carets(message, self.source_info, content);
Expand Down
18 changes: 14 additions & 4 deletions packages/hurl/src/runner/filter/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use hurl_core::ast::{SourceInfo, Template};

use crate::runner::template::eval_template;
use crate::runner::{RunnerError, RunnerErrorKind, Value, VariableSet};
use std::fmt::Write;

/// Formats a date `value` to a string given a specification `format`.
/// See <https://docs.rs/chrono/latest/chrono/format/strftime/index.html>
Expand All @@ -33,8 +34,14 @@ pub fn eval_format(

match value {
Value::Date(value) => {
let formatted = format!("{}", value.format(format.as_str()));
Ok(Some(Value::String(formatted)))
let mut formatted = String::new();
match write!(formatted, "{}", value.format(format.as_str())) {
Ok(_) => Ok(Some(Value::String(formatted))),
Err(_) => {
let kind = RunnerErrorKind::FilterInvalidFormatSpecifier(format);
Err(RunnerError::new(source_info, kind, assert))
}
}
}
v => {
let kind = RunnerErrorKind::FilterInvalidInput(v.kind().to_string());
Expand Down Expand Up @@ -107,12 +114,15 @@ mod tests {
}

#[test]
#[should_panic]
fn eval_filter_format_ko_invalid_format() {
let variables = VariableSet::new();

let date = Utc.with_ymd_and_hms(2025, 1, 1, 0, 0, 0).unwrap();
let filter = new_format_filter("%%%");
let _ = eval_filter(&filter, &Value::Date(date), &variables, false);
let ret = eval_filter(&filter, &Value::Date(date), &variables, false);
assert_eq!(
ret.unwrap_err().kind,
RunnerErrorKind::FilterInvalidFormatSpecifier("%%%".to_string())
);
}
}

0 comments on commit fee90a8

Please sign in to comment.