Skip to content

Commit

Permalink
Stop including the parser source __LINE__ in exceptions
Browse files Browse the repository at this point in the history
It makes testing for JSON errors very tedious. You either have
to use a Regexp or to regularly update all your assertions
when JSON is upgraded.
  • Loading branch information
byroot committed Dec 6, 2021
1 parent 75ada77 commit de9eb1d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions ext/json/ext/generator/generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -997,10 +997,10 @@ static void generate_json_float(FBuffer *buffer, VALUE Vstate, JSON_Generator_St
if (!allow_nan) {
if (isinf(value)) {
fbuffer_free(buffer);
rb_raise(eGeneratorError, "%u: %"PRIsVALUE" not allowed in JSON", __LINE__, RB_OBJ_STRING(tmp));
rb_raise(eGeneratorError, "%"PRIsVALUE" not allowed in JSON", RB_OBJ_STRING(tmp));
} else if (isnan(value)) {
fbuffer_free(buffer);
rb_raise(eGeneratorError, "%u: %"PRIsVALUE" not allowed in JSON", __LINE__, RB_OBJ_STRING(tmp));
rb_raise(eGeneratorError, "%"PRIsVALUE" not allowed in JSON", RB_OBJ_STRING(tmp));
}
}
fbuffer_append_str(buffer, tmp);
Expand Down
14 changes: 7 additions & 7 deletions ext/json/ext/parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *resul

{p = p - 1; } {p+= 1; cs = 29; goto _out;}
} else {
rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p);
rb_enc_raise(EXC_ENCODING eParserError, "unexpected token at '%s'", p);
}
}
np = JSON_parse_float(json, p, pe, result);
Expand Down Expand Up @@ -990,7 +990,7 @@ static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *resul
if (json->allow_nan) {
*result = CInfinity;
} else {
rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p - 8);
rb_enc_raise(EXC_ENCODING eParserError, "unexpected token at '%s'", p - 8);
}
}

Expand All @@ -1002,7 +1002,7 @@ static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *resul
if (json->allow_nan) {
*result = CNaN;
} else {
rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p - 2);
rb_enc_raise(EXC_ENCODING eParserError, "unexpected token at '%s'", p - 2);
}
}

Expand Down Expand Up @@ -2348,7 +2348,7 @@ static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *resul
if(cs >= JSON_array_first_final) {
return p + 1;
} else {
rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p);
rb_enc_raise(EXC_ENCODING eParserError, "unexpected token at '%s'", p);
return NULL;
}
}
Expand Down Expand Up @@ -2405,7 +2405,7 @@ static VALUE json_string_unescape(char *string, char *stringEnd, int intern, int
}
rb_enc_raise(
EXC_ENCODING eParserError,
"%u: incomplete unicode character escape sequence at '%s'", __LINE__, p
"incomplete unicode character escape sequence at '%s'", p
);
} else {
UTF32 ch = unescape_unicode((unsigned char *) ++pe);
Expand All @@ -2418,7 +2418,7 @@ static VALUE json_string_unescape(char *string, char *stringEnd, int intern, int
}
rb_enc_raise(
EXC_ENCODING eParserError,
"%u: incomplete surrogate pair at '%s'", __LINE__, p
"incomplete surrogate pair at '%s'", p
);
}
if (pe[0] == '\\' && pe[1] == 'u') {
Expand Down Expand Up @@ -3217,7 +3217,7 @@ static VALUE cParser_parse(VALUE self)
if (cs >= JSON_first_final && p == pe) {
return result;
} else {
rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p);
rb_enc_raise(EXC_ENCODING eParserError, "unexpected token at '%s'", p);
return Qnil;
}
}
Expand Down
14 changes: 7 additions & 7 deletions ext/json/ext/parser/parser.rl
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,14 @@ static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *resu
if (json->allow_nan) {
*result = CNaN;
} else {
rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p - 2);
rb_enc_raise(EXC_ENCODING eParserError, "unexpected token at '%s'", p - 2);
}
}
action parse_infinity {
if (json->allow_nan) {
*result = CInfinity;
} else {
rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p - 8);
rb_enc_raise(EXC_ENCODING eParserError, "unexpected token at '%s'", p - 8);
}
}
action parse_string {
Expand All @@ -245,7 +245,7 @@ static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *resu
fexec p + 10;
fhold; fbreak;
} else {
rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p);
rb_enc_raise(EXC_ENCODING eParserError, "unexpected token at '%s'", p);
}
}
np = JSON_parse_float(json, fpc, pe, result);
Expand Down Expand Up @@ -447,7 +447,7 @@ static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *resul
if(cs >= JSON_array_first_final) {
return p + 1;
} else {
rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p);
rb_enc_raise(EXC_ENCODING eParserError, "unexpected token at '%s'", p);
return NULL;
}
}
Expand Down Expand Up @@ -504,7 +504,7 @@ static VALUE json_string_unescape(char *string, char *stringEnd, int intern, int
}
rb_enc_raise(
EXC_ENCODING eParserError,
"%u: incomplete unicode character escape sequence at '%s'", __LINE__, p
"incomplete unicode character escape sequence at '%s'", p
);
} else {
UTF32 ch = unescape_unicode((unsigned char *) ++pe);
Expand All @@ -517,7 +517,7 @@ static VALUE json_string_unescape(char *string, char *stringEnd, int intern, int
}
rb_enc_raise(
EXC_ENCODING eParserError,
"%u: incomplete surrogate pair at '%s'", __LINE__, p
"incomplete surrogate pair at '%s'", p
);
}
if (pe[0] == '\\' && pe[1] == 'u') {
Expand Down Expand Up @@ -856,7 +856,7 @@ static VALUE cParser_parse(VALUE self)
if (cs >= JSON_first_final && p == pe) {
return result;
} else {
rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p);
rb_enc_raise(EXC_ENCODING eParserError, "unexpected token at '%s'", p);
return Qnil;
}
}
Expand Down

0 comments on commit de9eb1d

Please sign in to comment.