Webclient callback client exception when service is unavailable

Apr 1, 2015 at 2:44 AM
When I try to make a connection to a non-reachable server I am getting an exception. I have try catching the exception using a method similar to that documented in https://casablanca.codeplex.com/discussions/570082
cbclient.connect(host).then([host](pplx::task<void> t)
{       
    try
    {
        t.get();
        connected = true;
    ::log("Connected to %ls", host.c_str());
    }
    catch (...) 
    {};
}).wait();
but it always seems to generate an exception. Any smack in the right direction would be helpful.

thanks

Chris
Coordinator
Apr 1, 2015 at 5:31 PM
Hi Chris,

You are calling wait on the task returned from the connect(...) function call. Any time you call wait/get on a task if that task didn't complete and threw an exception, the exception will be re-thrown. If you are going to synchronously wait for the connect, as done in your code snippet, you'll need to have a try catch around the wait() call. Something like the following:
try
{
    cbclient.connect(host).then([host](pplx::task<void> t)
    {
        t.get();
        connected = true;
        ::log("Connected to %ls", host.c_str());
    }).wait();
} catch(const websocket_exception &e)
{
    // Handle connection error...
}
If you are new to programming with tasks I highly recommend you take a look at the following on msdn about using tasks.

Steve
Apr 1, 2015 at 11:43 PM
Steve,

Thanks for the reply/ The read was informing.

I changed the code as recommended and found I was still getting an unhandled exception...rather an abort. The first time I made the connect call I would catch the exception and get the following error via e.what().

Error connecting. Error= set_fail_handler: 3: Underlying Transport Error

I found it was the second connect that caused the abort. I then changed the code to new the cbclient so that it was fresh each time I called connect. This seems to have made the abort go away. I will continue to test to make sure that this really addresses the issue I was having.

Thanks for you time and input.

Chris
Coordinator
Apr 2, 2015 at 11:51 PM
Hi Chris,

Ah yes I see now, technically we don't allow the websocket_client to connection be closed and reopened. Perhaps support to allow it could be added in the future but I'm not sure how much value it adds to the API.

Let me know if you have any other issues.

Steve