Skip to content

Commit e6c9ec6

Browse files
authored
Merge pull request #7 from casperisfine/better-error-handling
Better ParseError messages
2 parents 001a8bc + 9456b98 commit e6c9ec6

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

ext/fast_jsonparser/fast_jsonparser.cpp

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,11 @@ static VALUE rb_fast_jsonparser_parse(VALUE self, VALUE arg)
6969

7070
dom::parser parser;
7171
auto [doc, error] = parser.parse(RSTRING_PTR(arg), RSTRING_LEN(arg));
72-
if (error == SUCCESS)
72+
if (error != SUCCESS)
7373
{
74-
return make_ruby_object(doc);
74+
rb_raise(rb_eFastJsonparserParseError, "%s", error_message(error));
7575
}
76-
// TODO better error handling
77-
rb_raise(rb_eFastJsonparserParseError, "parse error");
78-
return Qnil;
76+
return make_ruby_object(doc);
7977
}
8078

8179
static VALUE rb_fast_jsonparser_load(VALUE self, VALUE arg)
@@ -84,13 +82,11 @@ static VALUE rb_fast_jsonparser_load(VALUE self, VALUE arg)
8482

8583
dom::parser parser;
8684
auto [doc, error] = parser.load(RSTRING_PTR(arg));
87-
if (error == SUCCESS)
85+
if (error != SUCCESS)
8886
{
89-
return make_ruby_object(doc);
87+
rb_raise(rb_eFastJsonparserParseError, "%s", error_message(error));
9088
}
91-
// TODO better error handling
92-
rb_raise(rb_eFastJsonparserParseError, "parse error");
93-
return Qnil;
89+
return make_ruby_object(doc);
9490
}
9591

9692
static VALUE rb_fast_jsonparser_load_many(VALUE self, VALUE arg, VALUE batch_size)
@@ -101,15 +97,16 @@ static VALUE rb_fast_jsonparser_load_many(VALUE self, VALUE arg, VALUE batch_siz
10197
try {
10298
dom::parser parser;
10399
auto [docs, error] = parser.load_many(RSTRING_PTR(arg), FIX2INT(batch_size));
104-
if (error == SUCCESS)
100+
if (error != SUCCESS)
105101
{
106-
for (dom::element doc : docs)
107-
{
108-
rb_yield(make_ruby_object(doc));
109-
}
110-
return Qnil;
102+
rb_raise(rb_eFastJsonparserParseError, "%s", error_message(error));
111103
}
112-
rb_raise(rb_eFastJsonparserParseError, "parse error");
104+
105+
for (dom::element doc : docs)
106+
{
107+
rb_yield(make_ruby_object(doc));
108+
}
109+
113110
return Qnil;
114111
} catch (simdjson::simdjson_error error) {
115112
rb_raise(rb_eFastJsonparserUnknownError, "%s", error.what());

test/fast_jsonparser_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ def test_file_stream_is_working
2222
assert_nil FastJsonparser.load_many('./benchmark/nginx_json_logs.json') {}
2323
end
2424

25+
def test_parse_errors
26+
error = assert_raises FastJsonparser::ParseError do
27+
FastJsonparser.parse('{')
28+
end
29+
assert_equal "The JSON document has an improper structure: missing or superfluous commas, braces, missing keys, etc.", error.message
30+
31+
error = assert_raises FastJsonparser::ParseError do
32+
FastJsonparser.parse('{"')
33+
end
34+
assert_equal "A string is opened, but never closed.", error.message
35+
end
36+
2537
def test_load_many_batch_size
2638
Tempfile.create('documents') do |f|
2739
f.write({foo: "a" * 5_000}.to_json)

0 commit comments

Comments
 (0)