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

Json exception message on parse invalid Json string throws onRange error #805

Closed
NesterovMaxim opened this issue Sep 2, 2014 · 0 comments

Comments

@NesterovMaxim
Copy link

When I tried to parse following invalid Json string

string bad_request_nothing = `{"content": "123"`;

my program has crashed with message

core.exception.RangeError at vibe.data.json(864): Range violation
0x00500CA0 in onRangeError
...

When I check out the string in data/json.d I recognized following line with issue

enforceJson(!range.empty && (range.front == ',' || range.front == '}'), "Expected '}' or ',' - got '"~range[0]~"'.");

It checks range on empty and throws error message with data from empty range simultaneously.

I can suggest the following solution:

enforceJson(!range.empty && (range.front == ',' || range.front == '}'), "Expected '}' or ',' - got "~(range.empty?"nothing.":"'"~range[0]~"'."));

and check it by following unittests

import vibe.vibe;
import vibe.data.json : parseJsonString;

unittest
{
  string bad_request_nothing = `{"content": "123"`;
  try
  {
    Json request = parseJsonString(bad_request_nothing);
  }
  catch(JSONException e)
  {
    enforce(e.msg == "JSON format error at byte 17: Expected '}' or ',' - got nothing.");
  }
}

unittest
{
  string bad_request_k = `{"content": "123"k`;
  try
  {
    Json request = parseJsonString(bad_request_k);
  }
  catch(JSONException e)
  {
    enforce(e.msg == "JSON format error at byte 17: Expected '}' or ',' - got 'k'.");
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant