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

jPtr operation does not throw #1569

Closed
olcydvt opened this issue Apr 16, 2019 · 4 comments
Closed

jPtr operation does not throw #1569

olcydvt opened this issue Apr 16, 2019 · 4 comments

Comments

@olcydvt
Copy link

olcydvt commented Apr 16, 2019

  • What is the issue you have?

When I use jptr like below( it is a bad request), I can't catch any throw. Is there any problem on my operation or Is this a bug?

 try
 {
     // try to use the array index '-'
     json::reference ref = j.at("/array"_json_pointer);
 } catch (json::out_of_range& e)
   {
     std::cout << e.what() << '\n';
   } catch (json::out_of_range& e) {
     std::cout << e.what() << '\n';
   }

Thanks a lot

@nickaein
Copy link
Contributor

nickaein commented Apr 16, 2019

Can you provide a working sample that this error could be reproduced?

Also, in addition to json::out_of_range, you might try other exception types, or base json::exception or even std::exception types, to see whether some other exception is being thrown.

@olcydvt
Copy link
Author

olcydvt commented Apr 16, 2019

of course,

I will try your suggestion

here is my sample

    nlohmann::json val;
    json::json_pointer jsonPtr("/services/2/serviceName");
    try {
        val =  (std::string)j.at(jsonPtr);
    } catch (json::parse_error& e) {
        val = 0;
    } catch (json::out_of_range& e)
    {
        val = 0;
    }

@nickaein
Copy link
Contributor

There was a typo in the sample code. I've cleaned up a bit:

#include "json.hpp"
#include <iostream>

using namespace std;
using namespace nlohmann;

void check()
{
    nlohmann::json val;

    json::json_pointer jsonPtr("/services/2/serviceName");
    try 
    {
        cout << "Checking..." << endl;
        val =  (std::string)val.at(jsonPtr);
        cout << "no exception." << endl;
    }
    catch (json::parse_error& e) 
    {
        val = 0;
        cout << "thrown parse_error: " << e.what() << endl;
    }
    catch (json::out_of_range& e)
    {
        val = 0;
        cout << "thrown out_of_range: " << e.what() << endl;
    }
    catch (std::exception& e)
    {
        val = 0;
        cout << "thrown unknown exception: " << e.what() << endl;        
    }
}

int main(void)
{
    check();
    cout << "Done." << endl;
    return 0;
}

This outputs

Checking...
thrown out_of_range: [json.exception.out_of_range.404] unresolved reference token 'services'
Done.

which is expected (tested on version 3.6.1).

@olcydvt
Copy link
Author

olcydvt commented Apr 17, 2019

By the way, I try something like that;

what if wrong path was came from like "services" (it should be "services/2/serviceName"). I know it is wrong request but I assume that client side sent it.

json::json_pointer jsonPtr("/services");

Then, I tried to catch it with json::exception and it works.

Thanks a lot.

@olcydvt olcydvt closed this as completed Apr 17, 2019
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

2 participants