Reputation: 1137
My target is to send async http request using cpprest without waiting for response (it's for logging purposes I don't care if its failed and I don't want to wait). My "main thread" (it's not exactly main, it's specific thread that triggers this request) will be done by the time the answer arrives and in case of exception my application crushes. I'm not sure what would be the correct way to handle it.
I tried:
client.request(request).then([](http_response response)
{
try
{
// Handle the response here
if (response.status_code() == status_codes::OK)
{
std::cout << "Request sent successfully." << std::endl;
}
else
{
std::cout << "Request failed with status code " << response.status_code() << std::endl;
}
}
catch (const std::exception& e)
{
// Handle any exceptions that occur while processing the response
std::cerr << "Error: " << e.what() << std::endl;
}
});
Empty exception is thrown here:
~_ExceptionHolder()
{
if (_M_exceptionObserved == 0)
{
// If you are trapped here, it means an exception thrown in task chain didn't get handled.
// Please add task-based continuation to handle all exceptions coming from tasks.
// this->_M_stackTrace keeps the creation callstack of the task generates this exception.
_REPORT_PPLTASK_UNOBSERVED_EXCEPTION();
}
}
any assistance will be appreciated, thanks
Upvotes: 0
Views: 456
Reputation: 36379
You need to use a task based continuation in order to catch exceptions. E.g.:
client.request(request).then([](task<http_response> previousTask)
{
try
{
http_response response = previousTask.get();
// Handle the response here
if (response.status_code() == status_codes::OK)
{
std::cout << "Request sent successfully." << std::endl;
}
else
{
std::cout << "Request failed with status code " << response.status_code() << std::endl;
}
}
catch (const std::exception& e)
{
// Handle any exceptions that occur while processing the response
std::cerr << "Error: " << e.what() << std::endl;
}
});
Upvotes: 1