hjson/hjson-cpp

Exception Handling in Unmarshall

maldag opened this issue · 2 comments

When reading and parsing a hjson-file without curly brackets in the beginning, ill formatted text will not throw an exception due to ignoring it.

This behaviour is internally correct, so you will identify ill formatted files in your code correctly, but you don't pass it to the outer layer. The attached example will not print the error and silently continues.

// hjson_decode: 495
// Braces for the root object are optional
static Value _rootValue(Parser *p) {
 //[...]

  // assume we have a root object without braces
  try {
    res = _readObject(p, true);
    if (!_hasTrailing(p)) {
      return res;
    }
  } catch(syntax_error e) {} // This is problematic

  // test if we are dealing with a single JSON value instead (true/false/null/num/"")
  _resetAt(p);
  res = _readValue(p);
  if (!_hasTrailing(p)) {
    return res;
  }

  throw syntax_error(_errAt(p, "Syntax error, found trailing characters"));
}

You can test this using this file:

Global:
{
   MyGroup:
    {
        Key1: 1000		// Comment 1
		Key2: 2000		// Comment 2
		Key3:"Tests		// This line is not correct
		Key4: 1234
    }
}

My suggestion would be to rethrow the error. Why are you ignoring it?

Thanks for the detailed description. An error is thrown at the end of the function _rootValue for your example Hjson input, but I agree with you that it would be better to keep the message from the error thrown by _readObject. I'll create a PR.

The error thrown at the end of the function is not necessarily identical with the error thrown in the first place, since another error might also be thrown later on in that function so you loose the first error.