Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve JSON body parsing error reporting #2096

Merged
merged 3 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions integration/tests_error_parser/json.err
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
error: Parsing JSON
--> tests_error_parser/json.hurl:2:10
--> tests_error_parser/json.hurl:3:1
|
2 | { "name":
| ^ JSON error
3 | HTTP 200
| ^ expecting a boolean, number, string, array, object or null
|

Original file line number Diff line number Diff line change
@@ -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
|

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
POST http://localhost:8000/data
{
"name": ["c", x
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Set-StrictMode -Version latest
$ErrorActionPreference = 'Stop'
hurl tests_error_parser/json_list_expecting_element.hurl
3 changes: 3 additions & 0 deletions integration/tests_error_parser/json_list_expecting_element.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
set -Eeuo pipefail
hurl tests_error_parser/json_list_expecting_element.hurl
7 changes: 7 additions & 0 deletions integration/tests_error_parser/json_list_trailing_comma.err
Original file line number Diff line number Diff line change
@@ -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
|

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2
4 changes: 4 additions & 0 deletions integration/tests_error_parser/json_list_trailing_comma.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
POST http://localhost:8000/data
{
"name": ["c", "d", ]
}
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions integration/tests_error_parser/json_list_trailing_comma.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
set -Eeuo pipefail
hurl tests_error_parser/json_list_trailing_comma.hurl
Original file line number Diff line number Diff line change
@@ -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
|

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Set-StrictMode -Version latest
$ErrorActionPreference = 'Stop'
hurl tests_error_parser/json_object_expecting_element.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
set -Eeuo pipefail
hurl tests_error_parser/json_object_expecting_element.hurl
7 changes: 7 additions & 0 deletions integration/tests_error_parser/json_object_trailing_comma.err
Original file line number Diff line number Diff line change
@@ -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
|

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
POST http://localhost:8000/data
{
"toto": {
"a": "b",
"c": "d",
}
}
3 changes: 3 additions & 0 deletions integration/tests_error_parser/json_object_trailing_comma.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Set-StrictMode -Version latest
$ErrorActionPreference = 'Stop'
hurl tests_error_parser/json_object_trailing_comma.hurl
3 changes: 3 additions & 0 deletions integration/tests_error_parser/json_object_trailing_comma.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
set -Eeuo pipefail
hurl tests_error_parser/json_object_trailing_comma.hurl
7 changes: 0 additions & 7 deletions integration/tests_error_parser/json_unexpected_character.err

This file was deleted.

3 changes: 0 additions & 3 deletions integration/tests_error_parser/json_unexpected_character.sh

This file was deleted.

2 changes: 1 addition & 1 deletion integration/tests_error_parser/json_unexpected_eof.err
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ error: Parsing JSON
--> tests_error_parser/json_unexpected_eof.hurl:2:10
|
2 | { "name":
| ^ JSON error
| ^ expecting an element; found empty element instead
|

12 changes: 9 additions & 3 deletions packages/hurl_core/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
use core::cmp;

use crate::ast::SourceInfo;
use crate::parser;
use crate::parser::ParseError;
use crate::parser::{self, JsonErrorVariant};

pub trait Error {
fn source_info(&self) -> SourceInfo;
Expand Down Expand Up @@ -48,7 +48,7 @@ impl Error for parser::Error {
ParseError::JsonPathExpr => "Parsing JSONPath expression".to_string(),
ParseError::XPathExpr => "Parsing XPath expression".to_string(),
ParseError::TemplateVariable => "Parsing template variable".to_string(),
ParseError::Json => "Parsing JSON".to_string(),
ParseError::Json(_) => "Parsing JSON".to_string(),
ParseError::Predicate => "Parsing predicate".to_string(),
ParseError::PredicateValue => "Parsing predicate value".to_string(),
ParseError::RegexExpr { .. } => "Parsing regex".to_string(),
Expand Down Expand Up @@ -94,7 +94,13 @@ impl Error for parser::Error {
ParseError::JsonPathExpr => "expecting a JSONPath expression".to_string(),
ParseError::XPathExpr => "expecting a XPath expression".to_string(),
ParseError::TemplateVariable => "expecting a variable".to_string(),
ParseError::Json => "JSON error".to_string(),
ParseError::Json(variant) => {
match variant {
JsonErrorVariant::TrailingComma => "trailing comma is not allowed".to_string(),
JsonErrorVariant::EmptyElement => "expecting an element; found empty element instead".to_string(),
JsonErrorVariant::ExpectingElement => "expecting a boolean, number, string, array, object or null".to_string(),
}
}
ParseError::Predicate => "expecting a predicate".to_string(),
ParseError::PredicateValue => "invalid predicate value".to_string(),
ParseError::RegexExpr { message } => format!("invalid Regex expression: {message}"),
Expand Down
2 changes: 1 addition & 1 deletion packages/hurl_core/src/parser/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ mod tests {
error.inner,
ParseError::Expecting {
value: "\"".to_string()
}
},
);
}

Expand Down
10 changes: 8 additions & 2 deletions packages/hurl_core/src/parser/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,12 @@ pub enum ParseError {
JsonPathExpr,
XPathExpr,
TemplateVariable,
Json,
Json(JsonErrorVariant),
Xml,
Predicate,
PredicateValue,
RegexExpr { message: String },

Unexpected { character: String },
Eof,
Url,

Expand All @@ -66,6 +65,13 @@ pub enum ParseError {
GraphQlVariables,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum JsonErrorVariant {
TrailingComma,
ExpectingElement,
EmptyElement,
}

impl Error {
pub fn recoverable(&self) -> Error {
Error {
Expand Down
Loading
Loading