diff --git a/integration/tests_error_parser/json.err b/integration/tests_error_parser/json.err index 3a17914b7..bc87bdfa4 100644 --- a/integration/tests_error_parser/json.err +++ b/integration/tests_error_parser/json.err @@ -1,7 +1,7 @@ error: Parsing JSON - --> tests_error_parser/json.hurl:2:1 + --> tests_error_parser/json.hurl:3:1 | - 2 | { "name": - | ^ this brace is not closed later + 3 | HTTP 200 + | ^ expecting a boolean, number, string, array, object or null | diff --git a/integration/tests_error_parser/json_list_expecting_element.err b/integration/tests_error_parser/json_list_expecting_element.err new file mode 100644 index 000000000..fe899cc5f --- /dev/null +++ b/integration/tests_error_parser/json_list_expecting_element.err @@ -0,0 +1,7 @@ +error: Parsing JSON + --> tests_error_parser/json_list_expecting_element.hurl:3:17 + | + 3 | "name": ["c", x + | ^ expecting a boolean, number, string, array, object or null + | + diff --git a/integration/tests_error_parser/json_unexpected_character.exit b/integration/tests_error_parser/json_list_expecting_element.exit similarity index 100% rename from integration/tests_error_parser/json_unexpected_character.exit rename to integration/tests_error_parser/json_list_expecting_element.exit diff --git a/integration/tests_error_parser/json_list_expecting_element.hurl b/integration/tests_error_parser/json_list_expecting_element.hurl new file mode 100644 index 000000000..53e7203d9 --- /dev/null +++ b/integration/tests_error_parser/json_list_expecting_element.hurl @@ -0,0 +1,4 @@ +POST http://localhost:8000/data +{ + "name": ["c", x +} diff --git a/integration/tests_error_parser/json_list_expecting_element.ps1 b/integration/tests_error_parser/json_list_expecting_element.ps1 new file mode 100644 index 000000000..3cfb3805b --- /dev/null +++ b/integration/tests_error_parser/json_list_expecting_element.ps1 @@ -0,0 +1,3 @@ +Set-StrictMode -Version latest +$ErrorActionPreference = 'Stop' +hurl tests_error_parser/json_list_expecting_element.hurl diff --git a/integration/tests_error_parser/json_list_expecting_element.sh b/integration/tests_error_parser/json_list_expecting_element.sh new file mode 100755 index 000000000..0ece98d2e --- /dev/null +++ b/integration/tests_error_parser/json_list_expecting_element.sh @@ -0,0 +1,3 @@ +#!/bin/bash +set -Eeuo pipefail +hurl tests_error_parser/json_list_expecting_element.hurl diff --git a/integration/tests_error_parser/json_list_trailing_comma.err b/integration/tests_error_parser/json_list_trailing_comma.err new file mode 100644 index 000000000..0de1474a7 --- /dev/null +++ b/integration/tests_error_parser/json_list_trailing_comma.err @@ -0,0 +1,7 @@ +error: Parsing JSON + --> tests_error_parser/json_list_trailing_comma.hurl:3:20 + | + 3 | "name": ["c", "d", ] + | ^ trailing comma is not allowed + | + diff --git a/integration/tests_error_parser/json_list_trailing_comma.exit b/integration/tests_error_parser/json_list_trailing_comma.exit new file mode 100644 index 000000000..d8263ee98 --- /dev/null +++ b/integration/tests_error_parser/json_list_trailing_comma.exit @@ -0,0 +1 @@ +2 \ No newline at end of file diff --git a/integration/tests_error_parser/json_list_trailing_comma.hurl b/integration/tests_error_parser/json_list_trailing_comma.hurl new file mode 100644 index 000000000..5d99d5ba1 --- /dev/null +++ b/integration/tests_error_parser/json_list_trailing_comma.hurl @@ -0,0 +1,4 @@ +POST http://localhost:8000/data +{ + "name": ["c", "d", ] +} diff --git a/integration/tests_error_parser/json_unexpected_character.ps1 b/integration/tests_error_parser/json_list_trailing_comma.ps1 similarity index 53% rename from integration/tests_error_parser/json_unexpected_character.ps1 rename to integration/tests_error_parser/json_list_trailing_comma.ps1 index bca154ac0..23a8d765f 100644 --- a/integration/tests_error_parser/json_unexpected_character.ps1 +++ b/integration/tests_error_parser/json_list_trailing_comma.ps1 @@ -1,3 +1,3 @@ Set-StrictMode -Version latest $ErrorActionPreference = 'Stop' -hurl tests_error_parser/json_unexpected_character.hurl +hurl tests_error_parser/json_list_trailing_comma.hurl diff --git a/integration/tests_error_parser/json_list_trailing_comma.sh b/integration/tests_error_parser/json_list_trailing_comma.sh new file mode 100755 index 000000000..0301dc5ce --- /dev/null +++ b/integration/tests_error_parser/json_list_trailing_comma.sh @@ -0,0 +1,3 @@ +#!/bin/bash +set -Eeuo pipefail +hurl tests_error_parser/json_list_trailing_comma.hurl diff --git a/integration/tests_error_parser/json_object_expecting_element.err b/integration/tests_error_parser/json_object_expecting_element.err new file mode 100644 index 000000000..eb27c411c --- /dev/null +++ b/integration/tests_error_parser/json_object_expecting_element.err @@ -0,0 +1,7 @@ +error: Parsing JSON + --> tests_error_parser/json_object_expecting_element.hurl:2:11 + | + 2 | { "name": x + | ^ expecting a boolean, number, string, array, object or null + | + diff --git a/integration/tests_error_parser/json_object_expecting_element.exit b/integration/tests_error_parser/json_object_expecting_element.exit new file mode 100644 index 000000000..d8263ee98 --- /dev/null +++ b/integration/tests_error_parser/json_object_expecting_element.exit @@ -0,0 +1 @@ +2 \ No newline at end of file diff --git a/integration/tests_error_parser/json_unexpected_character.hurl b/integration/tests_error_parser/json_object_expecting_element.hurl similarity index 100% rename from integration/tests_error_parser/json_unexpected_character.hurl rename to integration/tests_error_parser/json_object_expecting_element.hurl diff --git a/integration/tests_error_parser/json_object_expecting_element.ps1 b/integration/tests_error_parser/json_object_expecting_element.ps1 new file mode 100644 index 000000000..a7f6c1651 --- /dev/null +++ b/integration/tests_error_parser/json_object_expecting_element.ps1 @@ -0,0 +1,3 @@ +Set-StrictMode -Version latest +$ErrorActionPreference = 'Stop' +hurl tests_error_parser/json_object_expecting_element.hurl diff --git a/integration/tests_error_parser/json_object_expecting_element.sh b/integration/tests_error_parser/json_object_expecting_element.sh new file mode 100755 index 000000000..fa3ec4a4f --- /dev/null +++ b/integration/tests_error_parser/json_object_expecting_element.sh @@ -0,0 +1,3 @@ +#!/bin/bash +set -Eeuo pipefail +hurl tests_error_parser/json_object_expecting_element.hurl diff --git a/integration/tests_error_parser/json_object_trailing_comma.err b/integration/tests_error_parser/json_object_trailing_comma.err new file mode 100644 index 000000000..ede6cfd92 --- /dev/null +++ b/integration/tests_error_parser/json_object_trailing_comma.err @@ -0,0 +1,7 @@ +error: Parsing JSON + --> tests_error_parser/json_object_trailing_comma.hurl:5:13 + | + 5 | "c": "d", + | ^ trailing comma is not allowed + | + diff --git a/integration/tests_error_parser/json_object_trailing_comma.exit b/integration/tests_error_parser/json_object_trailing_comma.exit new file mode 100644 index 000000000..d8263ee98 --- /dev/null +++ b/integration/tests_error_parser/json_object_trailing_comma.exit @@ -0,0 +1 @@ +2 \ No newline at end of file diff --git a/integration/tests_error_parser/json_object_trailing_comma.hurl b/integration/tests_error_parser/json_object_trailing_comma.hurl new file mode 100644 index 000000000..42373ab03 --- /dev/null +++ b/integration/tests_error_parser/json_object_trailing_comma.hurl @@ -0,0 +1,7 @@ +POST http://localhost:8000/data +{ + "toto": { + "a": "b", + "c": "d", + } +} diff --git a/integration/tests_error_parser/json_object_trailing_comma.ps1 b/integration/tests_error_parser/json_object_trailing_comma.ps1 new file mode 100644 index 000000000..1f1089230 --- /dev/null +++ b/integration/tests_error_parser/json_object_trailing_comma.ps1 @@ -0,0 +1,3 @@ +Set-StrictMode -Version latest +$ErrorActionPreference = 'Stop' +hurl tests_error_parser/json_object_trailing_comma.hurl diff --git a/integration/tests_error_parser/json_object_trailing_comma.sh b/integration/tests_error_parser/json_object_trailing_comma.sh new file mode 100755 index 000000000..b7676d6d4 --- /dev/null +++ b/integration/tests_error_parser/json_object_trailing_comma.sh @@ -0,0 +1,3 @@ +#!/bin/bash +set -Eeuo pipefail +hurl tests_error_parser/json_object_trailing_comma.hurl diff --git a/integration/tests_error_parser/json_unexpected_character.err b/integration/tests_error_parser/json_unexpected_character.err deleted file mode 100644 index 46b427baa..000000000 --- a/integration/tests_error_parser/json_unexpected_character.err +++ /dev/null @@ -1,7 +0,0 @@ -error: Parsing JSON - --> tests_error_parser/json_unexpected_character.hurl:2:1 - | - 2 | { "name": x - | ^ this brace is not closed later - | - diff --git a/integration/tests_error_parser/json_unexpected_character.sh b/integration/tests_error_parser/json_unexpected_character.sh deleted file mode 100755 index 69f5c40cf..000000000 --- a/integration/tests_error_parser/json_unexpected_character.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash -set -Eeuo pipefail -hurl tests_error_parser/json_unexpected_character.hurl diff --git a/integration/tests_error_parser/json_unexpected_eof.err b/integration/tests_error_parser/json_unexpected_eof.err index 26aab6541..9a36287fc 100644 --- a/integration/tests_error_parser/json_unexpected_eof.err +++ b/integration/tests_error_parser/json_unexpected_eof.err @@ -1,7 +1,7 @@ error: Parsing JSON - --> tests_error_parser/json_unexpected_eof.hurl:2:1 + --> tests_error_parser/json_unexpected_eof.hurl:2:10 | 2 | { "name": - | ^ this brace is not closed later + | ^ expecting an element; found empty element instead | diff --git a/packages/hurl_core/src/error/mod.rs b/packages/hurl_core/src/error/mod.rs index cf25e8b3c..9910ebac5 100644 --- a/packages/hurl_core/src/error/mod.rs +++ b/packages/hurl_core/src/error/mod.rs @@ -96,12 +96,9 @@ impl Error for parser::Error { ParseError::TemplateVariable => "expecting a variable".to_string(), ParseError::Json(variant) => { match variant { - JsonErrorVariant::UnexpectedCharcter { character } => format!("unexpected character: '{character}'"), + JsonErrorVariant::TrailingComma => "trailing comma is not allowed".to_string(), JsonErrorVariant::EmptyElement => "expecting an element; found empty element instead".to_string(), - JsonErrorVariant::UnclosedBrace => "this brace is not closed later".to_string(), - JsonErrorVariant::CannotResolve { name } => { - format!("failed to resolve '{name}'. If it's a variable, try enclosing it with double braces, e.g. {{{{{name}}}}}") - } + JsonErrorVariant::ExpectingElement => "expecting a boolean, number, string, array, object or null".to_string(), } } ParseError::Predicate => "expecting a predicate".to_string(), diff --git a/packages/hurl_core/src/parser/bytes.rs b/packages/hurl_core/src/parser/bytes.rs index 98a4f720b..cb9bdf947 100644 --- a/packages/hurl_core/src/parser/bytes.rs +++ b/packages/hurl_core/src/parser/bytes.rs @@ -148,10 +148,12 @@ mod tests { fn test_bytes_json_error() { let mut reader = Reader::new("{ x "); let error = bytes(&mut reader).err().unwrap(); - assert_eq!(error.pos, Pos { line: 1, column: 1 }); + assert_eq!(error.pos, Pos { line: 1, column: 3 }); assert_eq!( error.inner, - ParseError::Json(JsonErrorVariant::UnclosedBrace) + ParseError::Expecting { + value: "\"".to_string() + }, ); } diff --git a/packages/hurl_core/src/parser/error.rs b/packages/hurl_core/src/parser/error.rs index 67176c028..744a69227 100644 --- a/packages/hurl_core/src/parser/error.rs +++ b/packages/hurl_core/src/parser/error.rs @@ -67,10 +67,9 @@ pub enum ParseError { #[derive(Clone, Debug, PartialEq, Eq)] pub enum JsonErrorVariant { - UnexpectedCharcter { character: String }, - CannotResolve { name: String }, + TrailingComma, + ExpectingElement, EmptyElement, - UnclosedBrace, } impl Error { diff --git a/packages/hurl_core/src/parser/json.rs b/packages/hurl_core/src/parser/json.rs index 8ab9b1d21..e70c66de4 100644 --- a/packages/hurl_core/src/parser/json.rs +++ b/packages/hurl_core/src/parser/json.rs @@ -57,23 +57,16 @@ fn parse_in_json(reader: &mut Reader) -> ParseResult { Err(e) => match e { Error { recoverable: true, .. - } => { - return Err(error::Error { - pos: e.pos, - recoverable: false, - inner: error::ParseError::Json(JsonErrorVariant::CannotResolve { - name: reader - .read_while(|c| !c.is_whitespace() && !c.is_ascii_punctuation()), - }), - }) - } - _ => { - return Err(Error { - recoverable: false, - pos: e.pos, - inner: e.inner, - }) - } + } => Err(error::Error { + pos: e.pos, + recoverable: false, + inner: error::ParseError::Json(JsonErrorVariant::ExpectingElement), + }), + _ => Err(Error { + recoverable: false, + pos: e.pos, + inner: e.inner, + }), }, } } @@ -304,9 +297,7 @@ fn list_value(reader: &mut Reader) -> ParseResult { return Err(Error { pos: save, recoverable: false, - inner: ParseError::Json(JsonErrorVariant::UnexpectedCharcter { - character: ','.to_string(), - }), + inner: ParseError::Json(JsonErrorVariant::TrailingComma), }); } let element = list_element(reader)?; @@ -330,9 +321,7 @@ fn list_element(reader: &mut Reader) -> ParseResult { } pub fn object_value(reader: &mut Reader) -> ParseResult { - let save = reader.state.clone(); try_literal("{", reader)?; - peek_until_close_brace(reader, save)?; let space0 = whitespace(reader); let mut elements = vec![]; if reader.peek() != Some('}') { @@ -358,9 +347,7 @@ pub fn object_value(reader: &mut Reader) -> ParseResult { return Err(Error { pos: save, recoverable: false, - inner: ParseError::Json(JsonErrorVariant::UnexpectedCharcter { - character: ','.to_string(), - }), + inner: ParseError::Json(JsonErrorVariant::TrailingComma), }); } let element = object_element(reader)?; @@ -388,6 +375,7 @@ fn key(reader: &mut Reader) -> ParseResult