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

Informative error messages: Add line numbers #10

Closed
traversaro opened this issue Feb 20, 2018 · 6 comments
Closed

Informative error messages: Add line numbers #10

traversaro opened this issue Feb 20, 2018 · 6 comments

Comments

@traversaro
Copy link

Hi @pantor , thanks for the great library!

I am trying it for a ~100 lines template, and I am experiencing errors such as:

 what():  Unknown function in renderer.

or

 what(): Unknown loop statement.

Do you think there is a way to enrich this error with some information that would help to debug the problem, for example the line number in the original template file? Thanks in advance.

@pantor
Copy link
Owner

pantor commented Feb 21, 2018

That's true, the current error messages are really a drawback of this library. I think line numbers are a good start, so I'll have a look at this later 👍

@ludekvodicka
Copy link
Contributor

ludekvodicka commented Feb 23, 2018

Hi, better error messages would be great!

But also would be great to do more checks before calling Json library. For example when eval_expression() is trying to get non-existing values via following command

const json result = data[json::json_pointer(input)];

Json library asserted in this code:

const_reference operator[](const typename object_t::key_type& key) const
{
    // const operator[] only works for objects
    if (JSON_LIKELY(is_object()))
    {
        assert(m_value.object->find(key) != m_value.object->end());
        return m_value.object->find(key)->second;
    }

    JSON_THROW(type_error::create(305, "cannot use operator[] with " + std::string(type_name())));
}

Unfortunately, such assertion isn't possible to catch via Exception handler and will cause the application crash (and when assertion is disabled via NDEBUG macro, assert will be executed from STL).

Fix for such issue is very simple. Instead of using operator [], you can use function value which allows you to pass default value.

const json result = data.value(json::json_pointer(input), json());

After that you will not get assert and also your next row will work correctly:

if (result.is_null()) { throw std::runtime_error("Did not found json element: " + element.command); }

If you're interested I can create another PR.

@pantor
Copy link
Owner

pantor commented Feb 23, 2018

The latest commit already uses at in the eval_expression function. So that specific check should be solved. There are still a few problems with the CI and error checking, so I need to work on that first...

@ludekvodicka
Copy link
Contributor

Perfect. I already updated it.

I wasn't sure if you're interested in PRs so I'm really grateful you merged. Now I don't do that manually on every new version ;-).

Thanks for good work!

@pantor pantor changed the title Informative error messages Informative error messages: Add line numbers Feb 28, 2018
@pantor
Copy link
Owner

pantor commented Mar 21, 2020

This is now implemented for parser errors.

@pantor
Copy link
Owner

pantor commented Jun 27, 2020

With the latest commit, this is now implemented for render erros as well.

There are some minor issues with included templates, but please open a seperate issue for that. Thanks!

@pantor pantor closed this as completed Jun 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants